本帖最后由 恩恩 于 2013-12-22 00:12 编辑
- public class GetButton extends JFrame{
-
- JButton clickButton ;
- JLabel danji , shuangji ;
-
- JTextField showCount ;
- JTextField showDoubleCount ;
- JPanel panel ;
- int count ;
- int doubleCount ;
- public GetButton(){
- setLayout(new FlowLayout());
- panel = new JPanel() ;
- danji = new JLabel("one click") ;
- shuangji = new JLabel("double click") ;
- showCount = new JTextField(10) ;
- showDoubleCount = new JTextField(10) ;
- clickButton = new JButton("click");
- panel.add(clickButton) ;
- panel.add(danji) ;
- panel.add(showCount) ;
- panel.add(shuangji) ;
- panel.add(showDoubleCount) ;
- add(panel) ;
- setBounds(100, 300, 500, 500);
- setVisible(true);
-
- clickButton.addMouseListener(new MouseAdapter() { //add the mouse action
- public void mouseClicked(MouseEvent e){
- if(e.getClickCount() == 1){ //get the mouse action
- System.out.println("you click the mouse!!!");
- count += 1 ;
- String a = String.valueOf(count) ;
- showCount.setText(a);
- System.out.println(count);
- }
- else if(e.getClickCount() == 2){
- System.out.println("you double click the mouse");
- doubleCount += 1 ;
-
- String dCount = String.valueOf(doubleCount) ;
- showDoubleCount.setText(dCount);
- System.out.println(doubleCount);
- }
- }
- });
- }
-
- public static void main(String[] args) {
- new GetButton() ;
- }
- }
- 这个我又重新改了一边,这里有一个click按钮,当你点击这个按钮一次,它就会在第一个文本框显示你单击的次数,如果你双击,它就会在第二个文本框里面显示你双击的次数。你看看是否符合你的需要。
复制代码 |