java 按钮控件_按钮控件JButton的使用

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值