zoukankan      html  css  js  c++  java
  • Swing计算按钮点击次数 代码 简单飞扬

    计算按钮点击次数

    package swing;
    import javax.swing.*; //引入Swing包名
      //import com.sun.java.swing.*;  
      //使用JDK 1.2 Beta 4版和所有Swing 1.1 Beta 3
      //之前的版本,引入Swing包名用此方法。
      import java.awt.*;
      import java.awt.event.*;
      public class SwingAppTest {
        private static String labelPrefix = "Number of button clicks: ";
        private int numClicks = 0; //计数器,计算点击次数
        public Component createComponents() {
          final JLabel label = new JLabel(labelPrefix + "0 ");
          JButton button = new JButton("I'm a Swing button!");
        button.setMnemonic(KeyEvent.VK_I); //设置按钮的热键为'I'
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            numClicks++;
            label.setText(labelPrefix + numClicks);
                     //显示按钮被点击的次数
          }
    //http://comic.duowan.com/viewchapter-cid-54-chapterid-1391-15.html
        });
        label.setLabelFor(button);
        /* 在顶层容器及其内容之间放置空间的常用办法是把内容添加到Jpanel上,而Jpanel本身没有边框的。*/
     
        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createEmptyBorder(
                  30, //top
                  30, //left
                  10, //bottom
                  30) //right
                  );
         pane.setLayout(new GridLayout(0, 1)); //单列多行
         pane.add(button);
         pane.add(label);
         return pane;
      }
      public static void main(String[] args) {
         try {
           UIManager.setLookAndFeel(
             UIManager.getCrossPlatformLookAndFeelClassName());
                              //设置窗口风格
         } catch (Exception e) { }
         //创建顶层容器并添加内容.
         JFrame frame = new JFrame("SwingApplication");
         SwingAppTest app = new SwingAppTest();
         Component contents = app.createComponents();
         frame.getContentPane().add(contents, BorderLayout.CENTER);
         //窗口设置结束,开始显示
         frame.addWindowListener(new WindowAdapter() {  
                          //匿名类用于注册监听器
           public void windowClosing(WindowEvent e) {
             System.exit(0);
           }
         });
         frame.pack();
         frame.setVisible(true);
       }
      }
  • 相关阅读:
    2019-9-23-dotnet-判断特定进程存在方法
    2019-7-4-win10-uwp-处理用户点击关闭按钮
    2019-7-4-win10-uwp-处理用户点击关闭按钮
    2019-9-2-如何使用本模板搭建博客
    2019-8-31-dotnet-使用-System.CommandLine-写命令行程序
    2018-11-3-WPF-内部的5个窗口之-MediaContextNotificationWindow
    2019-4-7-VisualStudio-解决方案筛选器-slnf-文件
    2019-9-2-用自动机的思想说明光速
    2019-10-26-dotnet-core-发布只有一个-exe-的方法
    2018-2-13-win10-uwp-获取按钮鼠标左键按下
  • 原文地址:https://www.cnblogs.com/jiandanfy/p/1076545.html
Copyright © 2011-2022 走看看