I'm relatively new to Java and attempting to get my first exposure using Swing, and so I've decided to mock up a calculator in Java using Swing and JButtons
/ JTextFields
.
Problem being, most buttons display correctly and at the correct sizes, but there are a few abnormalities - particularly, the subtraction (-) button doesn't appear and the equation button (=) appears behind the others. Would post an image, but that should be sufficient and my rep won't allow me to inline an image; I lost an older account I had on here and have to suffer the consequences.
Without further ado, here's the code:
// Set the display JTextField as the top element in the GUI stackup:
disp.setBounds(50,25,400,50);
button7.setBounds(50,75,x,y);
button8.setBounds(150,75,x,y);
button9.setBounds(250,75,x,y);
buttonPlus.setBounds(350,75,x,y);
button4.setBounds(50,125,x,y);
button5.setBounds(150,125,x,y);
button6.setBounds(250,125,x,y);
buttonMinus.setBounds(350,75,x,y);
button1.setBounds(50,175,x,y);
button2.setBounds(150,175,x,y);
button3.setBounds(250,175,x,y);
buttonMult.setBounds(350,175,x,y);
buttonClear.setBounds(50,225,x,y);
button0.setBounds(150,225,x,y);
buttonEqual.setBounds(250,225,x,y);
buttonDiv.setBounds(350,225,x,y);
frame.add(disp);
frame.add(button0);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.add(button7);
frame.add(button8);
frame.add(button9);
frame.add(buttonClear);
frame.add(buttonPlus);
frame.add(buttonMinus);
frame.add(buttonMult);
frame.add(buttonDiv);
frame.add(buttonEqual);
//Display the window.
frame.pack();
frame.setVisible(true);
If it's relevant, I'll proceed to post the constructors as well; I'm using IntelliJ IDEA if that's of any pertinence.
In your code, buttonMinus
has exactly the same bounds as buttonPlus
. No doubt an innocent cut-and-paste error.
I don't know what you mean when you say the equals button is behind the others; is it totally obscured by them? Partly obscured? Is it appearing in the correct row? Is it too wide? Too narrow?
Ultimately, it doesn't matter. Andrew is right; using a layout manager will prevent these headaches and many others, including the aforementioned cut-and-paste error:
JPanel buttonPanel = new JPanel(new GridLayout(0, 4, 3, 3));
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonPlus);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMinus);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonMult);
buttonPanel.add(buttonClear);
buttonPanel.add(button0);
buttonPanel.add(buttonEqual);
buttonPanel.add(buttonDiv);
I get that you wanted to proceed in small steps, but the simpler LayoutManagers (GridLayout, BorderLayout, FlowLayout, and BoxLayout) are a step that you should not skip/postpone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.