如何使用 JTextField 在 JTextField 本身上使用 ActionListener 显示负面或正面结果
how to use JTextField to show a negative or positive result using ActionListener on the JTextField itself
我的程序应该要求用户在 JTextField
中写一个数字,然后在另一个已禁用的 JTextField
中显示该数字是负数还是正数。所有这些都必须在没有按钮的情况下完成。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PositiveOrNegative extends JFrame {
JLabel l1, l2 ;
JTextField t1, t2 ;
public PositiveOrNegative()
{
FlowLayout layout = new FlowLayout();
this.setLayout(layout);
l1 = new JLabel("Enter a number ");
t1 = new JTextField(10) ;
l2 = new JLabel("The number is ");
t2 = new JTextField(10) ;
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
t2.enable(false);
t2.setBackground(Color.PINK);
}
public void JavaJTextFieldActionListner() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String n = t1.getText() ;
int number = Integer.parseInt(n) ;
if (number >= 0)
t2.setText("POSITIVE") ;
else
t2.setText("NEGATIVE") ;
}
});
}
public static void main(String[] args) {
PositiveOrNegative p = new PositiveOrNegative();
p.setTitle("AWT SIGN");
p.setBounds(300, 300, 300, 120);
p.setVisible(true);
}
}
在您的示例中,您忘记调用方法 JavaJTextFieldActionListner
。我已将其删除并将代码移至构造函数中。我还添加了对错误输入的处理。这是结果:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PositiveOrNegative extends JFrame {
JLabel l1, l2 ;
JTextField t1, t2 ;
public PositiveOrNegative() {
FlowLayout layout = new FlowLayout();
this.setLayout(layout);
l1 = new JLabel("Enter a number ");
t1 = new JTextField(10);
l2 = new JLabel("The number is ");
t2 = new JTextField(10);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
t2.setEditable(false);
t2.setBackground(Color.PINK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String n = t1.getText();
try {
int number = Integer.parseInt(n);
if (number >= 0)
t2.setText("POSITIVE");
else
t2.setText("NEGATIVE");
} catch (Exception ex) {
t2.setText("Not an integer");
}
}
});
}
public static void main(String[] args) {
PositiveOrNegative p = new PositiveOrNegative();
p.setTitle("AWT SIGN");
p.setBounds(300, 300, 300, 120);
p.setVisible(true);
}
}
我的程序应该要求用户在 JTextField
中写一个数字,然后在另一个已禁用的 JTextField
中显示该数字是负数还是正数。所有这些都必须在没有按钮的情况下完成。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PositiveOrNegative extends JFrame {
JLabel l1, l2 ;
JTextField t1, t2 ;
public PositiveOrNegative()
{
FlowLayout layout = new FlowLayout();
this.setLayout(layout);
l1 = new JLabel("Enter a number ");
t1 = new JTextField(10) ;
l2 = new JLabel("The number is ");
t2 = new JTextField(10) ;
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
t2.enable(false);
t2.setBackground(Color.PINK);
}
public void JavaJTextFieldActionListner() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String n = t1.getText() ;
int number = Integer.parseInt(n) ;
if (number >= 0)
t2.setText("POSITIVE") ;
else
t2.setText("NEGATIVE") ;
}
});
}
public static void main(String[] args) {
PositiveOrNegative p = new PositiveOrNegative();
p.setTitle("AWT SIGN");
p.setBounds(300, 300, 300, 120);
p.setVisible(true);
}
}
在您的示例中,您忘记调用方法 JavaJTextFieldActionListner
。我已将其删除并将代码移至构造函数中。我还添加了对错误输入的处理。这是结果:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PositiveOrNegative extends JFrame {
JLabel l1, l2 ;
JTextField t1, t2 ;
public PositiveOrNegative() {
FlowLayout layout = new FlowLayout();
this.setLayout(layout);
l1 = new JLabel("Enter a number ");
t1 = new JTextField(10);
l2 = new JLabel("The number is ");
t2 = new JTextField(10);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
t2.setEditable(false);
t2.setBackground(Color.PINK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String n = t1.getText();
try {
int number = Integer.parseInt(n);
if (number >= 0)
t2.setText("POSITIVE");
else
t2.setText("NEGATIVE");
} catch (Exception ex) {
t2.setText("Not an integer");
}
}
});
}
public static void main(String[] args) {
PositiveOrNegative p = new PositiveOrNegative();
p.setTitle("AWT SIGN");
p.setBounds(300, 300, 300, 120);
p.setVisible(true);
}
}