packagecom.siwuxie095.ui;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.EventQueue;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
importjavax.swing.border.EmptyBorder;
public classTestButton extendsJFrame {
privateJPanel contentPane;
privateJTextField textField;
/**
* Launch the application.
*/
public staticvoidmain(String[] args) {
EventQueue.invokeLater(newRunnable() {
publicvoidrun() {
try{
TestButton frame = newTestButton();
frame.setVisible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
publicTestButton() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = newJPanel();
contentPane.setBorder(newEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(newBorderLayout(0, 0));
setContentPane(contentPane);
textField = newJTextField();
contentPane.add(textField, BorderLayout.NORTH);
textField.setColumns(10);
JTextArea textArea = newJTextArea();
contentPane.add(textArea, BorderLayout.CENTER);
JButton btnNewButton = newJButton("确定");
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBackground(Color.LIGHT_GRAY);
//添加鼠标监听事件,当鼠标点击时将上方的TextField中
//的内容显示到中间的TextArea
btnNewButton.addMouseListener(newMouseAdapter() {
@Override
publicvoidmouseClicked(MouseEvent arg0) {
//先从上方的文本框获取用户输入的文本,然后显示到中间的文本框
textArea.setText(textField.getText());
}
});
contentPane.add(btnNewButton, BorderLayout.SOUTH);
}
}