一个java编程题

编写一个frame,包含一个文本框及一个按钮,点击按钮能够将文本框内的输入内容回显在命令行提示符窗口内,并将frame的关闭按钮实现关闭功能。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Day13_C extends JFrame implements Serializable,ActionListener{
private static final long serialVersionUID = 1L;
private JButton bt1;
private JPanel jp1;
private JTextField t1,t2;
Day13_C(){
this.setTitle("基本测试");
this.setResizable(false);
this.setBounds(300,300,300,300);
this.setLayout(new BorderLayout());
init();
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
private void init() {
jp1=new JPanel(new GridLayout(2,1));
t1=new JTextField();
t2=new JTextField();
bt1=new JButton("OK");
bt1.addActionListener(this);
t1.setBackground(new Color(169,150,200));
t2.setBackground(new Color(109,180,100));
jp1.add(t1);
jp1.add(t2);
this.add(jp1,BorderLayout.CENTER);
this.add(bt1,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Day13_C();
}
public void actionPerformed(ActionEvent e) {
t1.requestFocus(true);
if(e.getActionCommand().equalsIgnoreCase("ok")) {
String str=t1.getText();
if(!(str.length()!=0)) {
return;
}
System.out.println(str);
t2.setText(str);
t1.setText(null);
}
}
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答