【问题标题】:Basic Multiply and Divide Java GUI基本乘除法 Java GUI
【发布时间】:2014-04-24 03:16:26
【问题描述】:

我试图让用户输入到框架中的数字乘以 2 或除以 3,具体取决于他们决定单击的按钮。我很难找出这样做的逻辑。我知道这需要在 actionperformed 方法中进行。

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


public class Quiz4 extends JFrame ActionListener
{

 // Global Variable Declarations
 // Our list input fields
 private JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
 private JTextField valueField = new JTextField(25);

 // create action buttons
 private JButton multiButton = new JButton("x2");
 private JButton divideButton = new JButton("/3");

 private JScrollPane displayScrollPane;
 private JTextArea display = new JTextArea(10,5);

// input number
private BufferedReader infirst;
// output number
private NumberWriter outNum;

 public Quiz4()
{
     //super("List Difference Tool");
     getContentPane().setLayout( new BorderLayout() );

     // create our input panel
     JPanel inputPanel = new JPanel(new GridLayout(1,1));
     inputPanel.add(valueLabel);
     inputPanel.add(valueField);

     getContentPane().add(inputPanel,"Center");

     // create and populate our diffPanel
     JPanel diffPanel = new JPanel(new GridLayout(1,2,1,1));
     diffPanel.add(multiButton);
     diffPanel.add(divideButton);
     getContentPane().add(diffPanel, "South");

    //diffButton.addActionListener(this);

} // Quiz4()

public void actionPerformed(ActionEvent ae)
{

} // actionPerformed()

public static void main(String args[])
{
     Quiz4 f = new Quiz4();
     f.setSize(1200, 200);
     f.setVisible(true);
     f.addWindowListener(new WindowAdapter()
 { // Quit the application
     public void windowClosing(WindowEvent e)
     {
     System.exit(0);
 }
 });
 } // main()
} // end of class

【问题讨论】:

  • 为每个按钮添加一个 ActionListener,然后在 actionPerformed(ActionEvent arg0) 方法中添加您想要完成的操作。
  • 在 actionPerformed 中,我如何将输入编码为乘以 2 或除以 3?
  • 我给你举个更简单的例子,但对按钮使用相同的逻辑,可以吗?

标签: java user-interface division multiplication


【解决方案1】:

这里有一些更简单的东西,但它基本上可以满足您对程序的要求。我为每个按钮添加了一个 ActionListener 来处理我想要的,即响应输入到文本框中的内容。我只是将 ActionListener 附加到按钮上,然后在 actionPerformed 方法中定义我想要发生的事情。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Quizx extends JFrame {
private JPanel panel;
private JTextField textfield;
private JLabel ansLabel;

public Quizx() {
    panel = new JPanel(new FlowLayout());
    this.getContentPane().add(panel);
    addLabel();
    addTextField();
    addButtons();
    addAnswerLabel();
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setTitle("Quiz 4");
    this.setSize(220, 150);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setVisible(true);
}

private void addTextField() {
    textfield = new JTextField();
    textfield.setColumns(9);
    panel.add(textfield);
}

private void addButtons() {
    JButton multButton = new JButton("x2");
    JButton divButton = new JButton("/3");
    panel.add(multButton);
    panel.add(divButton);
    addMultListener(multButton);
    addDivListener(divButton);
}

private void addLabel() {
    JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
    panel.add(valueLabel);
}

private void addAnswerLabel() {
    ansLabel = new JLabel();
    panel.add(ansLabel);
}

private void addMultListener(JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ansLabel.setText(String.valueOf(Integer.parseInt(textfield.getText().trim()) * 2));

        }
    });
}

private void addDivListener(JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ansLabel.setText(String.valueOf(Double.parseDouble(textfield.getText().trim()) /3));

        }
    });
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Quizx();
        }
    });
}
}

希望对您有所帮助。

【讨论】:

  • 这帮助很大,现在更有意义了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多