0

I need to arrange my buttons in a specific order but whatever modification I do it only changes the size of the buttons not the placement. Sorry if this is a easy question, this is my first Java class and I am having trouble understanding how to arrange stuff and event handling.

public class SwingCalculator extends JFrame {
    private JButton jbtReset;
    private JButton jbtAdd;
    private JButton jbtSubtract;

    private double TEMP;
    private double SolveTEMP;
    private JTextField jtfResult;

    Boolean addBool = false;
    Boolean subBool = false;

    String display = "";

    public SwingCalculator() 
    {
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4, 3));
        p1.add(jbtReset = new JButton("Reset"));

        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(jtfResult = new JTextField(10));
        jtfResult.setEditable(true);

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(5, 1));
        p3.add(jbtAdd = new JButton("+"));
        p3.add(jbtSubtract = new JButton("-"));

        JPanel p = new JPanel();
        p.setLayout(new GridLayout());
        p.add(p2);
        p.add(p1);
        p.add(p3);

        add(p);

        jbtAdd.addActionListener(new ListenToAdd());
        jbtSubtract.addActionListener(new ListenToSubtract());
        jbtReset.addActionListener(new ListenToReset());
    } //JavaCaluclator()

    class ListenToReset implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //display = jtfResult.getText();
            jtfResult.setText("");
            addBool = false;
            subBool = false;
            TEMP = 0;
            SolveTEMP = 0;
        }
    }

    class ListenToAdd implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
            jtfResult.setText("");
            addBool = true;
            SolveTEMP = SolveTEMP + TEMP;
            jtfResult.setText(  Double.toString(SolveTEMP));
            addBool = false;
            subBool = false;
        }
    }

    class ListenToSubtract implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            TEMP = Double.parseDouble(jtfResult.getText());
            jtfResult.setText("");
            subBool = true;
            SolveTEMP = SolveTEMP - TEMP;
            jtfResult.setText(  Double.toString(SolveTEMP));
            addBool = false;
            subBool = false;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingCalculator calc = new SwingCalculator();
        calc.pack();
        calc.setLocationRelativeTo(null);
        calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calc.setVisible(true);
    }

} //JavaCalculator

My result:

enter image description here

Expected result:

enter image description here

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Alpha
  • 67
  • 1
  • 5
  • 1
    go with setbound i already answered below – Kiran Mistry Jun 29 '19 at 10:19
  • **Side Note:** If you are just getting started learning Java, I would highly recommend learning JavaFX instead of Swing for your UIs. JavaFX replaced Swing over a decade ago. It is more powerful, easier to use, and is much more commonly used. While Swing is not *dead*, unless you have a specific need to learn it, my opinion is that your time would be better spent learning the current technology. – Zephyr Jun 29 '19 at 13:46
  • Thank you for that recommendation. I am learning Swing because it is for my current class in college but, I will defiantly start poking around JavaFX – Alpha Jun 29 '19 at 15:37

1 Answers1

2

You can use GridBagLayout with various options.

Eg:

GridBagLayout grid = new GridBagLayout();  
GridBagConstraints gbc = new GridBagConstraints();  
setLayout(grid); 
//add TF
gbc.gridx=1;
gbc.gridy=0;
this.add(new JTextField(10),gbc);
gbc.gridx=0;
gbc.gridy=1;
this.add(new JButton("+"),gbc);
gbc.gridx=1;
this.add(new JButton("-"),gbc);
gbc.gridx=2;
this.add(new JButton("Reset"),gbc);

//also for another display (first TF will be on the same x as - Button) 
//(and maybe you want TF to cover the space for all 3 Button +,-,Reset)
//you could add 2 panels one over other (x=0 : y=0,y=1), place TF on P1 and all Buttons on P2
Traian GEICU
  • 1,750
  • 3
  • 14
  • 26