import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestDemo { private JFrame frame=new JFrame("GUI_XB"); private JLabel label1,label2; private JPanel panel1,panel2; private JTextField tf1,tf2; private JButton jb1,jb2,jb3; public static void main(String args[]) { TestDemo sun=new TestDemo(); sun.go(); } public void go(){ //实现界面如下: Container contentPane=frame.getContentPane(); contentPane.setBackground(Color.PINK); label1=new JLabel("Source"); label2=new JLabel("Target"); panel1=new JPanel(); panel1.setLayout(new GridLayout(2,2)); tf1=new JTextField("请在此输入文本",10); tf2=new JTextField("显示区域",20); tf2.setEditable(false); panel1.add(label1); panel1.add(tf1); panel1.add(label2); panel1.add(tf2); contentPane.add(panel1,BorderLayout.NORTH); jb1=new JButton("Clear");jb2=new JButton("Copy");jb3=new JButton("Close"); panel2=new JPanel(); panel2.setLayout(new FlowLayout()); panel2.add(jb1); panel2.add(jb2); panel2.add(jb3); contentPane.add(panel2,BorderLayout.SOUTH); //实现方法: jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ tf2.setText(""); } }); jb2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(tf1.getSelectedText()!=null) tf2.setText(tf1.getSelectedText()); else tf2.setText(tf1.getText()); } }); jb3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,200); frame.setVisible(true); } }