import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextDemo extends JFrame implements ActionListener{
JButton y1;
JTextField t;
JPanel p;
TextDemo(){
Container c=getContentPane();
FlowLayout gl=new FlowLayout();
c.setLayout(gl);
p=new JPanel();
t=new JTextField(20);
t.addActionListener(this);
y1=new JButton("1");
y1.addActionListener(this);
c.add(p);
p.add(t);
p.add(y1);
}
public static void main(String args[]){
JFrame jf=new TextDemo();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
}
我想通过单击按钮,在文本框中显示内容,请问各位大侠:该怎么实现?谢谢
import java.awt.event.*;
import javax.swing.*;
public class TextDemo extends JFrame implements ActionListener{
JButton y1;
JTextField t;
JPanel p;
TextDemo(){
Container c=getContentPane();
FlowLayout gl=new FlowLayout();
c.setLayout(gl);
p=new JPanel();
t=new JTextField(20);
t.addActionListener(this);
y1=new JButton("1");
y1.addActionListener(this);
c.add(p);
p.add(t);
p.add(y1);
}
public static void main(String args[]){
JFrame jf=new TextDemo();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
}
我想通过单击按钮,在文本框中显示内容,请问各位大侠:该怎么实现?谢谢
public class TextDemo extends JFrame implements ActionListener{
JButton y1;
JTextField t;
JPanel p; TextDemo(){
Container c = getContentPane();
FlowLayout gl = new FlowLayout();
c.setLayout(gl);
p = new JPanel();
t = new JTextField(20);
t.addActionListener(this);
y1 = new JButton("1");
y1.addActionListener(this);
c.add(p);
p.add(t);
p.add(y1);
} public void actionPerformed(ActionEvent e){
if(e.getSource() == y1){
t.setText("测试");
}
}
public static void main(String args[]){
JFrame jf = new TextDemo();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
}
import java.awt.event.*;import javax.swing.*;
public class TextDemo extends JFrame implements ActionListener{
JButton y1;
JTextField t;
JPanel p;
TextDemo(){
Container c=getContentPane();
FlowLayout gl=new FlowLayout();
c.setLayout(gl);
p=new JPanel();
t=new JTextField(20);
t.addActionListener(this);
y1=new JButton("1");
y1.addActionListener(this);
c.add(p);
p.add(t);
p.add(y1);
}
public static void main(String args[]){
JFrame jf=new TextDemo();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
public void actionPerformed(ActionEvent e) {
t.setText("TextDemo");
}
}
最基本的事件处理啊