java - 如何向该程序添加退出按钮? "clear"怎么样?

标签 java swing user-interface jbutton layout-manager

我需要在 butPanel 上添加一个“清除计算器”的按钮以及一个退出程序的按钮。它还需要是非常基本的 Java 代码,因为我是初学者并且有一个糟糕的 comp.sci。老师。我有一个带有退出按钮的代码示例,但我不确定如何将它放入我的程序中。我已经尝试了很多。 此外,如果有更好的“错误检查”方法,我们将不胜感激。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator2 extends JFrame implements ActionListener
{
    JLabel heading = new JLabel ("2. Calculator");
    JLabel intro1 = new JLabel ("This program stimulates a four-function calculator.");
    JLabel intro2 = new JLabel ("It takes an operator and a number. It can add, subtract,");
    JLabel intro3 = new JLabel (" multiply and divide. Enter in the format eg. '+35'. ");
    JLabel inLabel = new JLabel ("        Operation: ");
    JLabel outLabel = new JLabel ("       Total:           ");
    JTextField inOper = new JTextField (7);
    JTextField outTota = new JTextField (7); // intro

//panels
JPanel titlePanel = new JPanel ();
JPanel intro1Panel = new JPanel ();
JPanel intro2Panel = new JPanel ();
JPanel intro3Panel = new JPanel ();
JPanel operPanel = new JPanel ();
JPanel totaPanel = new JPanel ();
JPanel butPanel = new JPanel ();

String operTemp;
String totaTemp;

public Calculator2 ()
{
    setTitle ("C - 2.");
    inOper.addActionListener (this);
    outTota.setEditable (false);
    getContentPane ().setLayout (new FlowLayout ());

    titlePanel.add (heading);
    intro1Panel.add (intro1);
    intro2Panel.add (intro2);
    intro3Panel.add (intro3);
    operPanel.add (inLabel);
    operPanel.add (inOper);
    totaPanel.add (outLabel);
    totaPanel.add (outTota); //adds components to panels

    getContentPane ().add (titlePanel);
    getContentPane ().add (intro1Panel);
    getContentPane ().add (intro2Panel);
    getContentPane ().add (intro3Panel);
    getContentPane ().add (operPanel);
    getContentPane ().add (totaPanel); //Adds panels to Frame

    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}


public static int isInteger (String input)
{
    try
    {
        Integer.parseInt (input);
        return Integer.parseInt (input);
    }
    catch (NumberFormatException nfe)
    {
        return 0;
    }
} //isInteger method


// The application
public String calculate (String operation, String newtotal)
{
    int total = isInteger (newtotal);
    String totalS;
    char operator;
    int number = 0;
    operator = operation.charAt (0);

    if (operator == '+')
    {
        number = isInteger (operation.substring (1));
        total = total + number;
        totalS = Integer.toString (total);
    }


    else if (operator == '-')
    {
        number = isInteger (operation.substring (1));
        total = total - number;
        totalS = Integer.toString (total);
    }


    else if (operator == '*')
    {
        number = isInteger (operation.substring (1));
        total = total * number;
        totalS = Integer.toString (total);
    }


    else if (operator == '/')
    {
        number = isInteger (operation.substring (1));
        total = total / number;
        totalS = Integer.toString (total);
    }
    else
    {
        totalS = ("ERROR");
    }

    if (number == 0)
    {
        totalS = ("ERROR");
    }
    return totalS;
} // calculate method


public void actionPerformed (ActionEvent evt)
{
    String userIn = inOper.getText ();
    String totalIn = outTota.getText ();
    try
    {
        totaTemp = calculate (userIn, totalIn);
        outTota.setText (totaTemp + "");
    }
    catch (Exception ex)
    {
        outTota.setText ("ERROR");
    }
    repaint ();
}


public static void main (String[] args)
{
    Calculator2 calc = new Calculator2 ();
    calc.setSize (350, 350);
    calc.setResizable (false);
    calc.setVisible (true);
}
}

最佳答案

首先声明按钮并初始化它们:

JButton exitButton = new JButton("Exit");
JButton clearButton = new JButton("Clear");

然后,确保 butPanel 有布局并且面板被添加到内容 Pane :

butPanel.setLayout(new FlowLayout());
getContentPane().add(butPanel);

然后在新按钮上添加 Action 监听器:

exitButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.exit(0);
  }
});
clearButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // TODO: clear the stuff here
  }
});

关于java - 如何向该程序添加退出按钮? "clear"怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14429310/

相关文章:

Java:将 JFileChooser 强制到一个目录及其子文件夹

java - 安卓 Java : changing image in listview depending on content

java - While 循环中递减运算符的问题

java - Uncaught Error : No polyfill registered for object Facebook JDK

java - jComboBox 的显示区域

java - 按钮放置和位置不起作用

c++ - SFML 2D 游戏——流畅的宇宙飞船运动

windows - Windows 有类似 xvfb 或 xnest 的东西吗?

java - 如何退出 Java 中的 do while 循环?

Javafx:如何获取单击的文本元素的ID