一、编写HelloWorld
private static void createAndShowGUI() {
// 确保一个漂亮的外观风格
JFrame.setDefaultLookAndFeelDecorated(true);
// 创建及设置窗口
JFrame frame = new JFrame("Swing"); //窗口名称
frame.setSize(200, 100); //窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口关闭,退出程序
// 添加 "Hello World" 内容
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
// 显示窗口
//frame.pack(); //该句控制窗口自适应输入的内容,修改设置的窗口大小
frame.setVisible(true); //显示
}
public static void main(String[] args) {
createAndShowGUI();
}
二、控件
public static void main(String[] args) {
// 确保一个漂亮的外观风格
JFrame.setDefaultLookAndFeelDecorated(true);
// 创建 JFrame 实例
JFrame frame = new JFrame("Login Example");
frame.setSize(750, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
/*
* 调用用户定义的方法并添加组件到面板
*/
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
String[] data = {
"one", "two", "three", "four"};
panel.setLayout(null);
// 创建 JLabel
JLabel userLabel = new JLabel("User:");
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
//创建文本域用于用户输入
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
// 输入密码的文本域
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(400,20,80,25);
panel.add(passwordLabel);
/*
*这个类似用于输入的文本域
* 但是输入的信息会以点号代替,用于包含密码的安全性
*/
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(500,20,165,25);
panel.add(passwordText);
// 输入富文本域
JLabel kinderLabel = new JLabel("textArea:");
kinderLabel.setBounds(10,70,80,25);
panel.add(kinderLabel);
// 富文本域
JTextArea textArea = new JTextArea();
textArea.setBounds(100,70,550,100);
panel.add(textArea);
// 图形化的组件JRadioButton
JRadioButton radioButton = new JRadioButton("radio");
radioButton.setBounds(10, 200, 80, 25);
panel.add(radioButton);
// 创建登录按钮
JButton loginButton = new JButton("login");
loginButton.setBounds(120, 200, 80, 25);
panel.add(loginButton);
//图形化的组件JCheckBox
JCheckBox checkBox = new JCheckBox("check");
checkBox.setBounds(300, 200, 80, 25);
panel.add(checkBox);
//下拉框
JComboBox jComboBox = new JComboBox(data);
jComboBox.setBounds(450, 200, 200, 30);
panel.add(jComboBox);
JColorChooser colorChooser = new JColorChooser(Color.CYAN);
colorChooser.setBounds(10, 250, 600, 200);
panel.add(colorChooser);
JList<String> jList = new JList<>(data);
jList.setBounds(10, 500, 600, 100);
panel.add(jList);
JScrollBar jScrollBar = new JScrollBar(1);
jScrollBar.setBounds(10, 650, 600, 100);
panel.add(jScrollBar);
JSlider jSlider = new JSlider();
jSlider.setBounds(10, 800, 600, 100);
panel.add(jSlider);
}
三、事件监听
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showEventDemo();
}
private void prepareGUI(){
JFrame.setDefaultLookAndFeelDecorated(true);
mainFrame = new JFrame("登录");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1)); //布局 几行几列
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
//监听
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("确定");
JButton submitButton = new JButton("提交");
JButton cancelButton = new JButton("取消");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("确定按钮.");
}
else if( command.equals( "Submit" ) ) {
statusLabel.setText("提交按钮.");
}
else {
statusLabel.setText("取消按钮.");
}
}
}
}
学习地址: https://www.w3cschool.cn/swing/7cxu1imh.html