0

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.

user987339
  • 10,519
  • 8
  • 40
  • 45
blasthash
  • 98
  • 1
  • 9
  • 2
    `button7.setBounds(50,75,x,y);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Feb 24 '14 at 09:00
  • For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). It looks vaguely like this GUI needs a `GridLayout`, but to be sure, I'd need to see it. – Andrew Thompson Feb 24 '14 at 09:01
  • The main reason I did my coding in this manner is that I'm starting out, and explicitly declaring what's where made more sense to me at the time. It feels primitive even to me; but I'd rather start with what's intuitive and work my way up to better, more 'kosher' methods. – blasthash Feb 24 '14 at 09:02
  • @AndrewThompson MCTaRE as in full source of the class file? – blasthash Feb 24 '14 at 09:03
  • 1
    1) *"more 'kosher' methods."* Forget 'kosher' and think workable. In case my first comment failed to communicate the fact, `setBounds` is not workable. It is likely the reason behind the problems. 2) You might start with this [simple calculator](http://stackoverflow.com/a/7441804/418556).. – Andrew Thompson Feb 24 '14 at 09:04
  • *"MCTaRE as in full source of the class file?"* MCTaRE as described in the document at the end of the link. – Andrew Thompson Feb 24 '14 at 09:05
  • Short answer: `setBounds` isn't liked that much. `setMinimum/MaximumSize` for the smallest elements in the hierarchy (i.e. the buttons) and then wrap them in `JPanel` in a `LayoutManager` as linked above. GUI writing in Swing can be a pain, but it does work eventually :) – Gorbles Feb 24 '14 at 09:26
  • @AndrewThompson ...mind explaining why not, for future reference? If you didn't read the opening lines of the question, I'm not exactly experienced in Java, thus you can't expect me to understand why one particular method works or doesn't work. That being said, the simple calc you posted does work, and `GridLayout` works intuitively enough to be understood, even by me. – blasthash Feb 24 '14 at 09:35
  • Swing (& AWT) is entirely based on using layouts. They are at the heart of satisfying the criteria mentioned in my first comment (different platforms, screen sizes, PLAFs). Not using layouts.. all bets are off. **Expect problems.** I have not meticulously documented all the problems you might see when trying to set the size/position of components, because.. I could not be bothered documenting the ways in which **not** laying out a GUI will f**k up. But there are many, and you have stumbled across one of those many ways. – Andrew Thompson Feb 24 '14 at 09:40

1 Answers1

0

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.

VGR
  • 40,506
  • 4
  • 48
  • 63