JAVA将磅转换为千克,反之亦然.动作监听器



我的程序有一个计算器布局,有9个按钮,分别为clear、negative和convert。以及两个单选按钮,当您选择其中一个时,它会转换您输入的数字。我已经设计好了,但我不知道如何做动作监听器的部分。我想我只是需要帮助开始编写代码。

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

// class
public class Lab31Panel extends JPanel implements ActionListener
{
    // data declarations
    private JRadioButton k2pButton;
    private JRadioButton p2kButton;
    private ButtonGroup weight;
    private JPanel selectConversion;
    private JButton jb0,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jbminus,jbclear,jbconvert;
    private JTextArea display;
    //constructor to initiate data and set up GUI
    public Lab31Panel()
    {
        setLayout( new BorderLayout() );
        // organizing radio buttons and their behaviours
        k2pButton = new JRadioButton( "Kilograms to Pounds" );
        p2kButton = new JRadioButton( "Pounds to Kilograms" );
        weight = new ButtonGroup();
        weight.add( k2pButton );
        weight.add( p2kButton );

        // adding components to panel to be south of the GUI
        selectConversion = new JPanel();
        selectConversion.add( k2pButton );
        selectConversion.add( p2kButton );

        add( selectConversion, BorderLayout.SOUTH );
        //setting up west area of GUI
        JPanel westPanel = new JPanel();
        JPanel convert = new JPanel();
        // setting up components for the  west of the GUI
        westPanel.setLayout(new GridLayout(5,3));
        westPanel.add(jb0 = new JButton("0"));
        westPanel.add(jb1 = new JButton("1"));
        westPanel.add(jb2 = new JButton("2"));
        westPanel.add(jb3 = new JButton("3"));
        westPanel.add(jb4 = new JButton("4"));
        westPanel.add(jb5 = new JButton("5"));
        westPanel.add(jb6 = new JButton("6"));
        westPanel.add(jb7 = new JButton("7"));
        westPanel.add(jb8 = new JButton("8"));
        westPanel.add(jb9 = new JButton("9"));
        westPanel.add(jbminus = new JButton("-"));
        westPanel.add(jbclear = new JButton("clear"));
        westPanel.add(jbconvert = new JButton("convert"));
        add(westPanel, BorderLayout.WEST);
        //setting up east components
        JPanel eastPanel = new JPanel();
        eastPanel.setLayout(new BorderLayout());
        display = new JTextArea();
        eastPanel.add(display,BorderLayout.CENTER);
        add(eastPanel);
        // add listeners to radio buttons
        p2kButton.addActionListener(this);
        k2pButton.addActionListener(this);

        //add listeners to all textfields
    } //end constructor
    public void actionPerformed(ActionEvent e)
    {

    }
} // end class

建议:

  • 首先,不要对所有按钮使用相同的ActionListener
  • 在这种情况下,不要让你的程序实现ActionListener
  • 相反,为数字按钮创建一个ActionListener
  • 还有一个或者更好的,三个,用于操作按钮
  • 在数字按钮中,代码应该是清晰的——将数字放入显示屏中
  • 在操作按钮监听器中,操作将取决于按下的操作
  • 如果按下转换按钮,则从保存该按钮的字段中提取数字数据
  • 使用解析方法将文本转换为数字
  • 计算您的转换
  • 然后显示它

此外

  • 我不会将任何ActionListener添加到JRadioButtons中,因为您真的不希望在按下时发生任何操作
  • 相反,请检查convert按钮的ActionListener,以查看选择了哪个JRadioButton,并在此选择的基础上创建转换公式

类似于:

    // add listeners to radio buttons  // **** no, don't do this
    // p2kButton.addActionListener(this);
    // k2pButton.addActionListener(this);
    jbclear.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO code to clear the JTextField
            // usually this will involve setText("")
        }
    });
    jbconvert.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO code to convert
            // first get JRadioButton selection -- which one is set
            // then get data from text component
            // convert it to number (but catching for NumberFormatException)
            // do conversion based on the selected JRadioButton
            // and display usually with a call to setText(...)
        }
    });

最新更新