我是新手,请教一个问题
下面这个问题是要从三个文本框输入三个数再比较大小,我想用三个变量记录三个文本框监听到的数据,请问怎样分别监听三个文本框的键盘事件啊?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
class JieMian extends KeyAdapter implements ActionListener
{
public static final int WIDTH=500;
public static final int HEIGHT=300;
JFrame mainFrame;
String string_1="";
String string_2="";
String string_3="";
DataText datatext;
Compel compel;
Res result;
String s1="";
String s2="";
String s3="";
JButton button=new JButton();
public JieMian()
{
mainFrame=new JFrame();
mainFrame.setTitle("数据比较器");
mainFrame.setSize(WIDTH,HEIGHT);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.show();
Container op=mainFrame.getContentPane();
op.setLayout(null);
datatext=new DataText();
compel=new Compel();
result=new Res();
op.add(datatext);
datatext.setBounds(0,0,350,30);
op.add(compel);
compel.setBounds(350,0,150,30);
op.add(result);
result.setBounds(0,30,400,30);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}class DataText extends JPanel
{
private JTextField t1,t2,t3;
private JLabel l1;
public DataText()
{
l1=new JLabel("请输入三个正整数:");
add(l1);
l1.setBounds(0,0,120,30);
t1=new JTextField();
add(t1);
t2=new JTextField();
add(t2);
t3=new JTextField();
add(t3);
t1.setHorizontalAlignment(t1.LEFT);
t1.setBackground(Color.WHITE);
t1.setForeground(Color.BLACK);
t2.setHorizontalAlignment(t1.LEFT);
t2.setBackground(Color.WHITE);
t2.setForeground(Color.BLACK);
t3.setHorizontalAlignment(t1.LEFT);
t3.setBackground(Color.WHITE);
t3.setForeground(Color.BLACK);
t1.setBounds(125,0,70,30);
t2.setBounds(200,0,70,30);
t3.setBounds(275,0,70,30);
setLayout(null);
}
}
class Compel extends JPanel
{
private JButton button;
public Compel()
{
button=new JButton("比较");
add(button);
button.setBounds(0,0,70,30);
}
}
class Res extends JPanel
{
private JLabel l2;
private JTextField t4;
public Res()
{
l2=new JLabel("最大数为:");
add(l2);
l2.setBounds(0,0,120,30);
t4=new JTextField();
add(t4);
t4.setHorizontalAlignment(t4.LEFT);
t4.setBackground(Color.WHITE);
t4.setForeground(Color.BLACK);
t4.setBounds(125,0,100,30);
setLayout(null);
}
}
}
public class MaxFind
{
public static void main(String[] args)
{
JieMian testJM=new JieMian();
}
}
下面这个问题是要从三个文本框输入三个数再比较大小,我想用三个变量记录三个文本框监听到的数据,请问怎样分别监听三个文本框的键盘事件啊?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
class JieMian extends KeyAdapter implements ActionListener
{
public static final int WIDTH=500;
public static final int HEIGHT=300;
JFrame mainFrame;
String string_1="";
String string_2="";
String string_3="";
DataText datatext;
Compel compel;
Res result;
String s1="";
String s2="";
String s3="";
JButton button=new JButton();
public JieMian()
{
mainFrame=new JFrame();
mainFrame.setTitle("数据比较器");
mainFrame.setSize(WIDTH,HEIGHT);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.show();
Container op=mainFrame.getContentPane();
op.setLayout(null);
datatext=new DataText();
compel=new Compel();
result=new Res();
op.add(datatext);
datatext.setBounds(0,0,350,30);
op.add(compel);
compel.setBounds(350,0,150,30);
op.add(result);
result.setBounds(0,30,400,30);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}class DataText extends JPanel
{
private JTextField t1,t2,t3;
private JLabel l1;
public DataText()
{
l1=new JLabel("请输入三个正整数:");
add(l1);
l1.setBounds(0,0,120,30);
t1=new JTextField();
add(t1);
t2=new JTextField();
add(t2);
t3=new JTextField();
add(t3);
t1.setHorizontalAlignment(t1.LEFT);
t1.setBackground(Color.WHITE);
t1.setForeground(Color.BLACK);
t2.setHorizontalAlignment(t1.LEFT);
t2.setBackground(Color.WHITE);
t2.setForeground(Color.BLACK);
t3.setHorizontalAlignment(t1.LEFT);
t3.setBackground(Color.WHITE);
t3.setForeground(Color.BLACK);
t1.setBounds(125,0,70,30);
t2.setBounds(200,0,70,30);
t3.setBounds(275,0,70,30);
setLayout(null);
}
}
class Compel extends JPanel
{
private JButton button;
public Compel()
{
button=new JButton("比较");
add(button);
button.setBounds(0,0,70,30);
}
}
class Res extends JPanel
{
private JLabel l2;
private JTextField t4;
public Res()
{
l2=new JLabel("最大数为:");
add(l2);
l2.setBounds(0,0,120,30);
t4=new JTextField();
add(t4);
t4.setHorizontalAlignment(t4.LEFT);
t4.setBackground(Color.WHITE);
t4.setForeground(Color.BLACK);
t4.setBounds(125,0,100,30);
setLayout(null);
}
}
}
public class MaxFind
{
public static void main(String[] args)
{
JieMian testJM=new JieMian();
}
}
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
Double d;
JPanel contentPane;
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton1 = new JButton();
pbulic static void main(String args[]){
new Frame1();
}
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setBounds(300,200,300,200);
setVisible(true);
setTitle("Frame Title");
jTextField1.setBounds(new Rectangle(103, 13, 166, 25));
jTextField1.addFocusListener(new Frame1_jTextField1_focusAdapter(this));
jLabel1.setText("请输入第一个值:");
jLabel1.setBounds(new Rectangle(3, 12, 96, 27));
jLabel2.setText("请输入第二个值:");
jLabel2.setBounds(new Rectangle(6, 72, 92, 25));
jTextField2.setBounds(new Rectangle(104, 70, 166, 25));
jTextField2.addFocusListener(new Frame1_jTextField2_focusAdapter(this));
jLabel3.setText("请输入第三个值:");
jLabel3.setBounds(new Rectangle(7, 137, 84, 21));
jTextField3.setBounds(new Rectangle(104, 130, 166, 25));
jTextField3.addFocusListener(new Frame1_jTextField3_focusAdapter(this));
jButton1.setBounds(new Rectangle(214, 196, 79, 31));
jButton1.setText("查看");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jTextField1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
contentPane.add(jTextField2);
contentPane.add(jLabel3);
contentPane.add(jTextField3);
contentPane.add(jButton1);
} public void jTextField1_focusLost(FocusEvent e) {
d=new Double(jTextField1.getText().trim());
} public void jTextField2_focusLost(FocusEvent e) {
Double temp=new Double(jTextField2.getText().trim());
if(temp.doubleValue()>d.doubleValue()){
d=temp;
}
} public void jTextField3_focusLost(FocusEvent e) {
Double temp=new Double(jTextField3.getText().trim());
if(temp.doubleValue()>d.doubleValue()){
d=temp;
}
} public void jButton1_actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"你输入的三个数中较大的一个是:" + d.toString() ,"比较结果",1);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame1_jTextField3_focusAdapter extends FocusAdapter {
private Frame1 adaptee;
Frame1_jTextField3_focusAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void focusLost(FocusEvent e) {
adaptee.jTextField3_focusLost(e);
}
}
class Frame1_jTextField2_focusAdapter extends FocusAdapter {
private Frame1 adaptee;
Frame1_jTextField2_focusAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void focusLost(FocusEvent e) {
adaptee.jTextField2_focusLost(e);
}
}
class Frame1_jTextField1_focusAdapter extends FocusAdapter {
private Frame1 adaptee;
Frame1_jTextField1_focusAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void focusLost(FocusEvent e) {
adaptee.jTextField1_focusLost(e);
}
}