Reputation: 1
I am a beginner in programming and I'm trying to create a calculator and I have just started about 1 to 2 hours ago but I have ran into a problem where all of ther JButton
controls have disappeared. Please help, I have been trying for so long and I just have no idea what's wrong with my code.
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Main {
static void gui() {
JPanel p = new JPanel();
JFrame f = new JFrame("Calculator");
f.add(p);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("YEET");
f.pack();
f.setSize(1930, 1090);
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
p.setLayout(null);
b0.setBackground(new Color(156,207,245));
b1.setBackground(new Color(156,207,245));
b2.setBackground(new Color(156,207,245));
b3.setBackground(new Color(156,207,245));
b4.setBackground(new Color(156,207,245));
b5.setBackground(new Color(156,207,245));
b6.setBackground(new Color(156,207,245));
b7.setBackground(new Color(156,207,245));
b8.setBackground(new Color(156,207,245));
b9.setBackground(new Color(156,207,245));
p.add(b1);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
b0.setVisible(true);
b1.setVisible(true);
b2.setVisible(true);
b3.setVisible(true);
b4.setVisible(true);
b5.setVisible(true);
b6.setVisible(true);
b7.setVisible(true);
b8.setVisible(true);
b9.setVisible(true);
p.setVisible(true);
f.setVisible((true));
}
public static void main(String[] args) {
gui();
}
}
Upvotes: 0
Views: 340
Reputation: 347184
null
layouts are just a bad idea. "Pixel perfect" layouts are an illusion. Take the time to learn to use the layout management APIs. See Laying Out Components Within a Container for more details
GridBagLayout
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton b0 = new JButton("0");
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JButton b3 = new JButton("3");
private JButton b4 = new JButton("4");
private JButton b5 = new JButton("5");
private JButton b6 = new JButton("6");
private JButton b7 = new JButton("7");
private JButton b8 = new JButton("8");
private JButton b9 = new JButton("9");
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 3;
gbc.gridx = 0;
gbc.weightx = 1;
gbc.fill = gbc.BOTH;
gbc.gridwidth = 3;
add(b0, gbc);
JButton[] buttons = new JButton[]{
b7, b8, b9,
b4, b5, b6,
b1, b2, b3
};
int row = 0;
int col = 0;
gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.weightx = 0.3;
for (JButton btn : buttons) {
gbc.gridx = col;
gbc.gridy = row;
add(btn, gbc);
col += 1;
if (col > 2) {
row++;
col = 0;
}
}
}
}
}
Or, if GridBagLayout
is little to much, make use of more then one, for example, a BorderLayout
and a GridLayout
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton b0 = new JButton("0");
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JButton b3 = new JButton("3");
private JButton b4 = new JButton("4");
private JButton b5 = new JButton("5");
private JButton b6 = new JButton("6");
private JButton b7 = new JButton("7");
private JButton b8 = new JButton("8");
private JButton b9 = new JButton("9");
public TestPane() {
setLayout(new BorderLayout());
add(b0, BorderLayout.SOUTH);
JPanel innerPane = new JPanel(new GridLayout(3, 3));
JButton[] buttons = new JButton[]{
b7, b8, b9,
b4, b5, b6,
b1, b2, b3
};
for (JButton btn : buttons) {
innerPane.add(btn);
}
add(innerPane);
}
}
}
Upvotes: 2