【问题标题】:Inputting numbers in a textfield via button on GUI通过 GUI 上的按钮在文本字段中输入数字
【发布时间】:2013-03-13 13:47:16
【问题描述】:

我做了一个简单的计算器

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
GridLayout layout = new GridLayout(5, 1);
JLabel l1 = new JLabel("Number 1:");
JLabel l2 = new JLabel("Number 2:");
JLabel l3 = new JLabel("Answer:"); 
JTextField t1 = new JTextField(30);
JTextField t2 = new JTextField(30);
JTextField t3 = new JTextField(30);
JButton add = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div= new JButton("/");
Float ans; 

public Calculator()
  {
    super("Calculator");
    setSize(250, 200);
    add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(l3);
    add(t3);
    add(add);
    add(sub);
    add(mul);
    add(div);
    setLayout(layout);
    add.addActionListener(this);
    sub.addActionListener(this);
    mul.addActionListener(this);
    div.addActionListener(this);
    setVisible(true);
  }

public void actionPerformed(ActionEvent e)
  {
String n1 = t1.getText();
String n2 = t2.getText();
Float num1 = Float.parseFloat(n1);
Float num2 = Float.parseFloat(n2);   
Object clicked = e.getSource();

if(add == clicked)
{
  t3.setText(String.valueOf(num1+num2));
}

else if(sub == clicked)
{
  t3.setText(String.valueOf(num1-num2));
}

else if(mul == clicked)
{
  t3.setText(String.valueOf(num1*num2));
}

else
{
if(num2 == 0)
t3.setText("Can't Divide By Zero");
else
t3.setText(String.valueOf(num1/num2));
  }
 }
}

还有一个阅读它的课程

public class UseMyFrame
{
  public static void main(String[] args)
  {
    Calculator calc = new Calculator();
    calc.setVisible(true);
  }
}

我的问题是我想添加另一个功能并放置 9 个按钮 1-9 按下时会将它们各自的数字放在文本字段上,但我不知道如何将它们设置为出现在文本字段中,我首先想做 set.text 但我意识到按钮如何知道把它的数字放在哪里,因为如果我做 set.text 我需要把它放在 textfield1 或 textfield 2 上。我想让数字首先出现在 textfield1 中,然后出现在 textfield2 上(如果有)已经是 textfield1 上的一个数字

【问题讨论】:

    标签: java swing user-interface textfield calculator


    【解决方案1】:

    但我不知道如何将它们设置为出现在文本字段中

    您添加到 JButton 的 Action 应该扩展 TextAction。 TextAction 可以访问最后一个焦点文本组件(因此您不必自己跟踪此信息)。您的代码将类似于:

    public class AddDigitAction extends TextAction
    {
        public void actionPerformed(ActionEvent e)
        {
            JButton button = (JButton)e.getSource();
            String digit = button.getActionCommand();
            JTextComponent target = getTextComponent(e);
            target.replaceSelection(digit);
    }
    

    您可以对所有按钮使用相同的操作。 replaceSelection() 方法是一种将文本添加到文本字段的简单方法。它将在文本字段中插入符号的最后一个位置插入文本。

    【讨论】:

      【解决方案2】:

      因此,您需要创建一个布尔变量,以帮助您跟踪将数字放入哪个字段。然后在按钮的操作中您可以使用它来决定。

         if(first){
            textField1.setText("1");
            first = false;
         }else{
            textField2.setText("1");
            first = true;
         }
      

      现在,这个sn-p很简单,没有考虑所有的可能性。它只是在两个字段之间切换。您可以将其扩展到您需要的内容。

      【讨论】:

      • 谢谢,我会试试博士的建议,如果不适合我,再试试你的,非常感谢
      【解决方案3】:

      如果你只想有单个数字:

      if(t1.getText().length() == 0)
          t1.setText(...);
      else
          t2.setText(...);
      

      更好:找出哪个文本字段具有当前的focus(请参阅 javadocs)并将数字放在该文本的末尾:

      tFocused.setText(tFocused.getText() + digit)
      

      【讨论】:

      • 谢谢,我目前正在制作按钮,在尝试了您的所有建议后,我会告诉你们我的进度,非常感谢
      【解决方案4】:

      首先设置全局int curs = 0 和全局字符串screen 然后在每个数字按钮上输入该代码(所有关于字符串的连接)

      if(curs==0){
          screen="1";  // change the number for each button
          jTextField1.setText(screen);
          a=Double.parseDouble(screen);
          curs++;
          }else{
          screen=screen+"1";  //  // change the number for each button
          jTextField1.setText(screen);
          a=Double.parseDouble(screen);
          curs++;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-16
        • 2018-09-23
        • 1970-01-01
        • 2015-07-08
        相关资源
        最近更新 更多