import javax.swing.JFrame
import javax.swing.JTextField
public class MyJFrame {
/源码纤**
* @param args
*/
public static void main(String[] args) {
/雹仿模册/ TODO Auto-generated method stub
JFrame mainFrame = new JFrame("Cool TextField")
mainFrame.setLayout(null)
JButton jb = new JButton("ce")
JTextField jf = new JTextField()
jb.setBounds(80, 0, 20, 30)
jb.setVisible(true)
jf.setVisible(true)
jf.add(jb)
jf.setBounds(5, 50, 100, 30)
mainFrame.getContentPane().add(jf)
mainFrame.pack()
mainFrame.setVisible(true)
mainFrame.setBounds(100, 100, 200, 200)
}
}
只要文本框添加监听拆李链即扰闭可,用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")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)