JAVA一个窗体添加三个文本框具体怎么操作?
下面是监听的代码:
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
if(o==text1) //文本框一号
{
text2.requestFocus();//文本框2号,请求焦点
}
else if(o==text2)
{
text3.requestFocus();
}
}
只要文本框添加监听即可,用ActionListener即会在文本框中按下回车键时触发事件。
下面是监听的代码:
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
if(o==text1) //文本框一号
{
text2.requestFocus();//文本框2号,请求焦点
}
else if(o==text2)
{
text3.requestFocus();
}
}
程序如下:
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
private JButton button;
private JTextField textField;
private JLabel label;
public MyFrame()
{
button = new JButton("Hello world");
textField = new JTextField(15);
label = new JLabel("输入内容:");
setTitle("Simple-Frame");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2
, 500, 300);
setLayout(new FlowLayout());
add(label);
add(textField);
add(button);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
this.textField.setText("Hello World");
}
}
}