import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
//调用
public class kc {//主函数
javax.swing.JFrame jf = new javax.swing.JFrame();//创建窗体
public static void main(String[] args) {
kc lo = new kc();
lo.showUI();//显示UI
}
public class ButListener implements ActionListener {
private JTextField jt;
public void setJt(JTextField jt) {
this.jt = jt;
}
public void actionPerformed(ActionEvent e) {
jt.setText("joyer520");
}
}//“打印”按钮的回应
public class ButListener2 implements ActionListener {//“关闭”按钮的回应
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public void showUI() {
jf.setTitle("kc");
jf.setSize(300, 400);
jf.setLocationRelativeTo(null);
jf.setLayout(null);
jf.setResizable(false);
jf.setUndecorated(true);//设置窗体
javax.swing.JButton jbu = new javax.swing.JButton(" 打印");
jbu.setBounds(30, 30, 70, 25);
jbu.setBackground(Color.BLACK);
jbu.setForeground(Color.white);
jbu.setFocusPainted(false);
jf.add(jbu);//设置按钮
javax.swing.JButton jbu2 = new javax.swing.JButton("退出");
jbu2.setBounds(30, 130, 70, 25);
jbu2.setBackground(Color.BLACK);
jbu2.setForeground(Color.white);
jbu2.setFocusPainted(false);
jf.add(jbu2);//设置按钮
javax.swing.JTextField jtf = new javax.swing.JTextField();
java.awt.Dimension dm = new java.awt.Dimension(280, 30);
jtf.setPreferredSize(dm);
jtf.setBounds(30, 150, 150, 25);
jf.add(jtf);//设置文本框
ButListener but = new ButListener();
jbu.addActionListener(but);
ButListener2 but2 = new ButListener2();
jbu2.addActionListener(but2);//类似C++的信号槽
but.setJt(jtf);//“打印”与文本框建立联系
jf.setVisible(true);//可以显示
}
}