问题
I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time.
Here's the code for two of the JButtons:
JButton button_one=new JButton();
button_one.setPreferredSize(new Dimension(100,150));
mainpanel.add(button_one);
button_one.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent button_1_picked){
amount_of_cards_picked=amount_of_cards_picked+1;
cardamountcheck();
if(twocardspicked==true){
userpick2=0;
System.out.println(setoutcards[userpick2]);
pairdetermination();
}
else if(twocardspicked==false){
userpick1=0;
System.out.println(setoutcards[userpick1]);
}
}
});
JButton button_two = new JButton();
button_two.setPreferredSize(new Dimension(100, 150));
mainpanel.add(button_two);
button_two.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent button_2_picked){
amount_of_cards_picked=amount_of_cards_picked+1;
cardamountcheck();
if(twocardspicked==true){
userpick2=1;
System.out.println(setoutcards[userpick2]);
pairdetermination();
}
else if(twocardspicked==false){
userpick1=1;
System.out.println(setoutcards[userpick1]);
}
}
});
Basically when one of these buttons are clicked, variables in my code get changed. These buttons run fine and work exactly how I want them to, but they don't all appear when there is more than 13 of them, and I need 52.
回答1:
I want to add 52 buttons in a JPanel. All with ActionListeners.
Done. I created a GUI that displays 52 JButtons
. I made them half the size of the OP's, so the GUI would fit better in the answer.
It's fairly straightforward using a GridLayout
on a JPanel
.
Here's the complete runnable code. A minimal reproducible example.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class FiftyTwoJButtonGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new FiftyTwoJButtonGUI());
}
@Override
public void run() {
JFrame frame = new JFrame("52 JButton GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 13, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
CardListener listener = new CardListener();
for (int i = 0; i < 52; i++) {
JButton button = new JButton("" + (i + 1));
button.addActionListener(listener);
button.setPreferredSize(new Dimension(50, 75));
panel.add(button);
}
return panel;
}
public class CardListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
int cardNumber = Integer.valueOf(button.getText());
System.out.println(cardNumber);
}
}
}
来源:https://stackoverflow.com/questions/65596037/not-all-of-my-jbuttons-are-showing-up-after-i-add-a-dozen-of-them-what-am-i-doi