import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class BoxTest
{
public static void main(String[] args)
{
BoxFrame frame=new BoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class BoxFrame extends JFrame implements KeyListener,ActionListener
{
public BoxFrame()
{
setTitle("登录系统");
setSize(250,250);
setLocation(300,330);
JLabel label1=new JLabel("用户名称:");
JTextField td1=new JTextField(10);
td1.setMaximumSize(td1.getPreferredScrollableViewportSize());
Box bx1=Box.createHorizontalBox();
bx1.add(label1);
bx1.add(Box.createHorizontalStrut(10));
bx1.add(td1);
JLabel label2=new JLabel("密码:");
JPasswordField pd2=new JPasswordField(10);
pd2.setMaximumSize(pd2.getPreferredScrollableViewportSize());
Box bx2=Box.createHorizontalBox();
bx2.add(label2);
bx2.add(Box.createHorizontalStrut(30));
bx2.add(pd2);
JLabel label3=new JLabel();
Box bx3=Box.createHorizontalBox();
bx3.add(label3,BorderLayout.CENTER);
JButton button1=new JButton("确认");
JButton button2=new JButton("退出");
Box bx4=Box.createHorizontalBox();
bx4.add(button1);
bx4.add(Box.createHorizontalStrut(30));
bx4.add(button2);
Box box=Box.createVerticalBox();//创建一个垂直的容器
box.add(bx1);
// box.add(Box.createVerticalStrut(2));
box.add(bx2);
box.add(bx3);
box.add(bx4);
add(box,BorderLayout.CENTER);
//添加文本框键盘事件处理接口
td1.addKeyListener(this);
pd2.addKeyListener(this);
button1.addKeyListener(this);
button2.addKeyListener(this);
//添加按扭的鼠标单击事件
button1.addActionListener(this);
button2.addActionListener(this);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
System.out.println("1");
if(e.getSource()==td1)
{
System.out.println("zhe li you cuo wu");
// pd2.requestFocus();
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
System.out.println("2");
}
}
}
程序它是这样指出错误:
E:\java技术练习\BoxTest.java:78: 找不到符号
符号: 变量 td1
位置: 类 BoxFrame
if(e.getSource()==td1)
^
E:\java技术练习\BoxTest.java:90: 找不到符号
符号: 变量 button1
位置: 类 BoxFrame
if(e.getSource()==button1)
^
2 错误
import java.awt.event.*;
import javax.swing.*;public class BoxTest
{
public static void main(String[] args)
{
BoxFrame frame=new BoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class BoxFrame extends JFrame implements KeyListener,ActionListener
{
public BoxFrame()
{
setTitle("登录系统");
setSize(250,250);
setLocation(300,330);
JLabel label1=new JLabel("用户名称:");
JTextField td1=new JTextField(10);
td1.setMaximumSize(td1.getPreferredScrollableViewportSize());
Box bx1=Box.createHorizontalBox();
bx1.add(label1);
bx1.add(Box.createHorizontalStrut(10));
bx1.add(td1);
JLabel label2=new JLabel("密码:");
JPasswordField pd2=new JPasswordField(10);
pd2.setMaximumSize(pd2.getPreferredScrollableViewportSize());
Box bx2=Box.createHorizontalBox();
bx2.add(label2);
bx2.add(Box.createHorizontalStrut(30));
bx2.add(pd2);
JLabel label3=new JLabel();
Box bx3=Box.createHorizontalBox();
bx3.add(label3,BorderLayout.CENTER);
JButton button1=new JButton("确认");
JButton button2=new JButton("退出");
Box bx4=Box.createHorizontalBox();
bx4.add(button1);
bx4.add(Box.createHorizontalStrut(30));
bx4.add(button2);
Box box=Box.createVerticalBox();//创建一个垂直的容器
box.add(bx1);
// box.add(Box.createVerticalStrut(2));
box.add(bx2);
box.add(bx3);
box.add(bx4);
add(box,BorderLayout.CENTER);
//添加文本框键盘事件处理接口
td1.addKeyListener(this);
pd2.addKeyListener(this);
button1.addKeyListener(this);
button2.addKeyListener(this);
//添加按扭的鼠标单击事件
button1.addActionListener(this);
button2.addActionListener(this);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
System.out.println("1");
if(e.getSource()==td1)
{
System.out.println("zhe li you cuo wu");
// pd2.requestFocus();
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
System.out.println("2");
}
}
}
程序它是这样指出错误:
E:\java技术练习\BoxTest.java:78: 找不到符号
符号: 变量 td1
位置: 类 BoxFrame
if(e.getSource()==td1)
^
E:\java技术练习\BoxTest.java:90: 找不到符号
符号: 变量 button1
位置: 类 BoxFrame
if(e.getSource()==button1)
^
2 错误
你可以把这两个变量定义成类的变量(属性)。这样可以在类的任何位置访问。
JTextField td1 = new JTextField(10);
JPasswordField pd2 = new JPasswordField(10);
写到方法外边去
JButton button1 = new JButton("确认");