1

I've bee teaching myself java and following along with the problems in the book. I'm trying to make a display for my calculator. In the example(I did not attach this) the buttons were a smaller size than what mine are and I can't figure out how to reformat them. I tried using the dimension class but it had no affect. Also, I can't get my text at the top of the calculator to align left.

Here is my code:

public class Calculator extends JFrame {

   public Calculator() {

       setTitle("Calculator");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setLocationRelativeTo(null);

       setSize(300, 300);

       setLayout(new BorderLayout());

       JPanel numberPanel = new JPanel();
       add(numberPanel, BorderLayout.CENTER);

       numberPanel.setLayout(new GridLayout(4, 3, 3, 3));
       for(int i = 1; i < 10; i++) {
           JButton button = new JButton(String.valueOf(i));
           numberPanel.add(button);
       }

       JButton zero = new JButton("" + 0);
       JButton dot = new JButton(".");
       JButton clear = new JButton("C");
       numberPanel.add(zero);
       numberPanel.add(dot);
       numberPanel.add(clear);

       JPanel keyPanel = new JPanel();
       add(keyPanel, BorderLayout.EAST);
       keyPanel.setLayout(new GridLayout(4, 1, 3, 3));

       JButton plus = new JButton("+");
       JButton minus = new JButton("-");
       JButton times = new JButton("*");
       JButton divide = new JButton("/");
       keyPanel.add(plus);
       keyPanel.add(minus);
       keyPanel.add(times);
       keyPanel.add(divide);

       JPanel equalsPanel = new JPanel();
       add(equalsPanel, BorderLayout.SOUTH);
       equalsPanel.setLayout(new GridLayout(1, 1));

       JButton equals = new JButton("=");
       equalsPanel.add(equals);

       JPanel textPanel = new JPanel();
       add(textPanel, BorderLayout.NORTH);
       JTextField inputBox = new JTextField("0.0");
       inputBox.setHorizontalAlignment(JTextField.LEFT);
       inputBox.setEditable(false);

       Font font = new Font("MonoSpaced", Font.BOLD, 20);
       inputBox.setFont(font);

       textPanel.add(inputBox);

       setVisible(true);
   }

   public static void main(String[] args) {

       new Calculator();
   }
}

Imports were left off for brevity

M A
  • 71,713
  • 13
  • 134
  • 174
ab91
  • 179
  • 16
  • One simple way is to set the button's font. Please see my answer [here](http://stackoverflow.com/questions/7824162/about-layouts-in-simple-calculator/7824221#7824221). – Hovercraft Full Of Eels Aug 02 '14 at 16:05
  • Changing the size of my JFrame did precisely what I was looking to do. But how can I get my text to align left? Even after doing inputBox.setHorizontalAlignment(JTextField.LEFT); it's still aligned in the middle if the textbox – ab91 Aug 02 '14 at 16:29
  • Don't change the size of your JFrame as doing so will make it hard to update, enhance or improve your application, and may make it not work well on other platforms. Again, please see the link to my answer with code above. Consider calling `setComponentOrientation(...)` for left justified text. – Hovercraft Full Of Eels Aug 02 '14 at 16:40

2 Answers2

2

GridLayout will laugh at you when you try and set a dimension. It does respect preferred sizes. You should select a layout manager that will respect preferred sizes. Or you can simply pack() (after you add all your components) your frame instead of setSize() and all the components preferred sizes will kick in. (Disclaimer - because of GridLayout though, if you try and resize the frame after that, you components will resize again)

See more at How to use Layout Managers. For a quick view of which layout managers respect preferred sizes and which ones don't, have a look at this post.

A common approach is to nest panels with different layout managers also, as seen here


UPDATE

As mentioned preciously, you should just call pack on the frame instead of set size. With your current code, this would cause the frame to be very small because of the preferred sizes of the components. If you want the buttons to have a bigger preferred size, you can set the font to a bigger font and/or use button.setMargins(new Insets(w,x,y,x)); to make the margins bigger. But it is preferred to pack the frame.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

I would recommend using the Window Builder add-on if you’re using Eclipse. This tool will help you with many aspects of Swing. Learn by doing.

WindowBuilder Dowload Link

Damaged
  • 1
  • 1