我是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);
}
}
};
答案 0 :(得分:1)
要检查现在选择了哪个文本字段,您可以在类中为t1和t2创建两个布尔值,我认为您不需要第三个用于t3
让我们说:
boolean t1_selected = false;
boolean t2_selected = false;
然后在Const中添加另一个侦听器。对于作为焦点侦听器的文本字段,焦点侦听器将在文本字段聚焦时触发,然后您可以将此文本的布尔值更改为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);
如果文本字段为空,它们将通过异常,只将它们放在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);
}
顺便说一句,你必须在发布之前缩进你的代码,你的工作是让你的问题变得干净