小例子,100%JAVA,有ALT+KEY

package downtimemodule;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingApplication {

    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',这样设置有下划线,ALT+键
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                numClicks++;
                label.setText(labelPrefix + numClicks);
            }
        });
        label.setLabelFor(button);
        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");
        SwingApplication app = new SwingApplication();
        Component contents = app.createComponents();
        frame.getContentPane().add(contents, BorderLayout.CENTER);

//窗口设置结束,开始显示
        frame.addWindowListener(new WindowAdapter() {
//匿名类用于注册监听器

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}

你可能感兴趣的:(java,swing)