⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingaplication.java

📁 JAVA学习源代码,大家可以好好参考,请多提宝贵意见
💻 JAVA
字号:
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 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'
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numClicks++;
Label.setText(labelPrefix + numClicks);
//显示按钮被点击的次数
}
});
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");
SwingApplication app = new SwingApplication();
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);
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -