I am new to GUI in Java (and coding in gneral I guess), so I am confused on why the JButtons on my JPanel only show up if I initialize and add the JButtons on the class that has the JFrame. What I am trying to do is create a phone keypad with JBUttons and a GridLayout on a JPanel.
If I initialize and add the JButtons to the JPanel on the class that has the JFrame in it. I can not add the JButtons to the JPanel in the class that extends the JPanel and then add the JPanel object to the JFrame.
This works:
public static void main (String[] argv){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.gray);
DialPanel keypad = new DialPanel();
panel.setLayout(new GridLayout(3, 3));
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
keypad.add(one);
keypad.add(two);
keypad.add(three);
keypad.add(four);
keypad.add(five);
keypad.add(six);
keypad.add(seven);
keypad.add(eight);
keypad.add(nine);
frame.getContentPane().add(keypad);
keypad.setVisible(true);
frame.pack();
frame.setVisible(true);
}
and
public class DialPanel extends JPanel {
DialPanel(){
JPanel panel = new JPanel();
}
}
But this does not work, and I am not sure why:
public static void main (String[] argv){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.gray);
DialPanel keypad = new DialPanel();
frame.getContentPane().add(keypad);
keypad.setVisible(true);
frame.pack();
frame.setVisible(true);
}
and
public class DialPanel extends JPanel {
DialPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
panel.setBackground(Color.gray);
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
}
}
With the second option the window is simply just blank, but with everything in the main method it works as intended. Why won't having the buttons in the constructor work, and how can I get it to work (if I can get it to work like that at all)?
You don't need to create one more JPanel
inside DialPanel
(first line in DialPanel
constructor).
DialPanel
already extends JPanel
by class definition:
public class DialPanel extends JPanel
So you were adding all your buttons to a JPanel
that was never be added to any other container.
You must add all your buttons to DialPanel
itself.
Correct code is:
public class DialPanel extends JPanel {
DialPanel(){
setLayout(new GridLayout(3, 3));
setBackground(Color.gray);
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
}
}