Java Swing button simple use count hits

From , 5 Years ago, written in Java, viewed 124 times.
URL https://pastebin.vip/view/e49b8b40
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SwingApplication {
  6.         private static String labelPrefix = "Number of button clicks: ";
  7.         private int numClicks = 0; // 计数器,计算点击次数
  8.  
  9.         public Component createComponents() {
  10.                 final JLabel label = new JLabel(labelPrefix + "0 ");
  11.  
  12.                 JButton button = new JButton("I'm a Swing button!");
  13.                 button.setMnemonic(KeyEvent.VK_I); // 设置按钮的热键为'I'
  14.                 button.addActionListener(new ActionListener() {
  15.                         public void actionPerformed(ActionEvent e) {
  16.                                 numClicks++;
  17.                                 label.setText(labelPrefix + numClicks);
  18.                                 // 显示按钮被点击的次数
  19.                         }
  20.                 });
  21.                 label.setLabelFor(button);
  22.  
  23.                 /* 在顶层容器及其内容之间放置空间的常用办法是把内容添加到Jpanel上,而Jpanel本身没有边框的。 */
  24.  
  25.                 JPanel pane = new JPanel();
  26.                 pane.setBorder(BorderFactory.createEmptyBorder(30, // top
  27.                                 30, // left
  28.                                 10, // bottom
  29.                                 30) // right
  30.                 );
  31.                 pane.setLayout(new GridLayout(0, 1)); // 单列多行
  32.                 pane.add(button);
  33.                 pane.add(label);
  34.                 return pane;
  35.         }
  36.  
  37.         public static void main(String[] args) {
  38.                 try {
  39.                         UIManager.setLookAndFeel(UIManager
  40.                                         .getCrossPlatformLookAndFeelClassName());
  41.                         // 设置窗口风格
  42.                 } catch (Exception e) {
  43.                 }
  44.  
  45.                 // 创建顶层容器并添加内容.
  46.                 JFrame frame = new JFrame("SwingApplication");
  47.                 SwingApplication app = new SwingApplication();
  48.                 Component contents = app.createComponents();
  49.                 frame.getContentPane().add(contents, BorderLayout.CENTER);
  50.  
  51.                 // 窗口设置结束,开始显示
  52.                 frame.addWindowListener(new WindowAdapter() {
  53.                         // 匿名类用于注册监听器
  54.                         public void windowClosing(WindowEvent e) {
  55.                                 System.exit(0);
  56.                         }
  57.                 });
  58.                 frame.pack();
  59.                 frame.setVisible(true);
  60.         }
  61. }
  62.  

Reply to "Java Swing button simple use count hits"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website