简体   繁体   中英

Having Multiple JButtons

sorry for the simple question, but I'm really new to this and can't really find an answer. I'm confused on how to add two (or more) JButtons. I can't seem to get both to show, only one ever shows, which is the "Division" one. My most recent attempt is below. How can I get both buttons to show at the button of the window?

public class Calculator implements ActionListener {
private JFrame frame;
private JTextField xfield, yfield;
private JLabel result;
private JButton subtractButton;
private JButton divideButton;
private JPanel xpanel;

public Calculator() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    xpanel = new JPanel();
    xpanel.setLayout(new GridLayout(3,2));

    xpanel.add(new JLabel("x:"));
    xfield = new JTextField("0", 5);
    xpanel.add(xfield);

    xpanel.add(new JLabel("y:"));
    yfield = new JTextField("0", 5);
    xpanel.add(yfield);

    xpanel.add(new JLabel("x*y="));
    result = new JLabel("0");
    xpanel.add(result);
    frame.add(xpanel, BorderLayout.NORTH);

    subtractButton = new JButton("Subtract");
    frame.add(subtractButton, BorderLayout.SOUTH);
    subtractButton.addActionListener(this);

    divideButton = new JButton("Division");
    frame.add(divideButton, BorderLayout.SOUTH);
    divideButton.addActionListener(this);

    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent event) {
    int x = 0;
    int y = 0;

    String xText = xfield.getText();
    String yText = yfield.getText();

    try {
        x = Integer.parseInt(xText);
      }
    catch (NumberFormatException e) {
        x = 0;
      }

    try {
        y = Integer.parseInt(yText);
      }
    catch (NumberFormatException e) {
        y = 0;
      }

    result.setText(Integer.toString(x-y));
  }
}

You need to add both buttons in a JPanel before adding that JPanel in the SOUTH of your frame.

So instead of

subtractButton = new JButton("Subtract");
frame.add(subtractButton, BorderLayout.SOUTH);
subtractButton.addActionListener(this);

divideButton = new JButton("Division");
frame.add(divideButton, BorderLayout.SOUTH);
divideButton.addActionListener(this);

You can do this

JPanel southPanel = new JPanel();

subtractButton = new JButton("Subtract");
southPanel.add(subtractButton);
subtractButton.addActionListener(this);

divideButton = new JButton("Division");
southPanel.add(divideButton);
divideButton.addActionListener(this);

frame.add(southPanel , BorderLayout.SOUTH);

Use Eclipse and WindowBuilder for your Swing interfaces and add as many buttons as you like or move them around with your mouse. Its much easier especially if you are new to this and you'll learn a lot from the generated code.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM