gpt4 book ai didi

java - 从按钮获取文本到选定的文本字段

转载 作者:行者123 更新时间:2023-12-01 14:20:27 25 4
gpt4 key购买 nike

我是java编程新手。我想执行一项任务,当按下按钮时,即附加代码中JFrame上显示的0-9,该按钮的值必须分配给之前选择的JField按钮被按下。如何操作?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class calci2 extends JFrame implements ActionListener {
JFrame f1;
JPanel p;
JButton b[] = new JButton[10];
JButton btnadd;
JButton btnmul;
JButton btndiv;
JButton btnsub;
public static JTextField t1;
JTextField t2;
JTextField t3;
JLabel no1;
JLabel no2;
JLabel res;

calci2() {
f1 = new JFrame();
p = new JPanel();
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
no1 = new JLabel("Enter 1st number");
no2 = new JLabel("Enter 2nd number");
res = new JLabel(" Result is ");
btnadd = new JButton("ADD");
btnmul = new JButton("MUL");
btndiv = new JButton("DIV");
btnsub = new JButton("SUB");
for (int i = 0; i < 10; i++) {
b[i] = new JButton("" + i);
}
btnadd.addActionListener(this);
btnmul.addActionListener(this);
btndiv.addActionListener(this);
btnsub.addActionListener(this);

for (int i = 0; i < 10; i++) {
b[i].addActionListener(this);
}
p.add(no1);
p.add(t1);
p.add(no2);
p.add(t2);
p.add(res);
p.add(t3);
p.add(btnadd);
p.add(btnmul);
p.add(btndiv);
p.add(btnsub);

for (int i = 0; i < 10; i++) {
p.add(b[i]);
}
this.add(p);
}

public static void main(String args[]) {
calci2 c = new calci2();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setSize(300, 300);
c.setVisible(true);
c.setResizable(false);
c.setLocationRelativeTo(null);
}

public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
if (str.equals("ADD")) {
int c = a + b;
s3 = String.valueOf(c);
t3.setText(s3);
}

else if (str.equals("SUB")) {
int c = a - b;
s3 = String.valueOf(c);
t3.setText(s3);
}

else if (str.equals("MUL")) {
int c = a * b;
s3 = String.valueOf(c);
t3.setText(s3);
}

else if (str.equals("DIV")) {
int c = a / b;
s3 = String.valueOf(c);
t3.setText(s3);
}
}
};

最佳答案

要检查现在选择了哪个文本字段,您可以在类中为 t1 和 t2 创建两个 boolean 值,我认为您不需要为 t3 创建第三个 boolean 值

比方说:

 boolean t1_selected = false;
boolean t2_selected = false;

然后在 Const 中添加另一个监听器。到作为焦点监听器的文本字段,当文本字段获得焦点时焦点监听器将触发,然后您可以在此处将此文本的 boolean 值更改为 true

        t1.addFocusListener(new FocusListener() {

@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
t1_selected = true;
t2_selected = false;
}
});
t2.addFocusListener(new FocusListener() {

@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub

}

@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
t1_selected = false;
t2_selected = true;
}
});

现在对于按钮,您需要在函数actionPerformed中检查事件源,对所有按钮使用e.getSource()而不是e.getActionCommand()。

例如:

    if(e.getSource() == this.b[0]){
if(t1_selected)
{
t1.setText("0");
}
if(t2_selected)
{
t2.setText("0");
}
}
else if(e.getSource() == this.b[1]){
if(t1_selected)
{
t1.setText("1");
}
if(t2_selected)
{
t2.setText("1");
}
}
//rest of cases

也不要将这些行放在函数的开头

        String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);

如果文本字段为空,它们将异常(exception),仅将它们放在 ADD、SUB、DIV 和 MUL 的情况下

例如:

  else if (e.getSource() == btnadd) {
String s1 = new String(t1.getText());
String s2 = new String(t2.getText());
String s3 = new String();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);

int c = a + b;
s3 = String.valueOf(c);
t3.setText(s3);
}

顺便说一句,您必须在发布之前缩进代码,让您的问题变得干净是您的工作

关于java - 从按钮获取文本到选定的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17627207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com