Swing的简要学习记录



说明

swing是 java GUI 开发的好工具。

这个简要学习,主要是为了做一个聊天室的项目需要用到,所以只记录一些我现在需要用到的知识,肯定不全,知识结构也不会很清晰。以后如果需要用更多的swing知识,再补充吧。

update:聊天室的小项目在此 socket(java) 搭建一个多人在线聊天室



基本组件

  • JFrame:窗体组件,每个窗体都需要它;
  • Pane系列:包括 JPanel, JScrollPane 等等,如英文 pane 所示,相当于一个一个的画板;
  • 基本组件:包括 JTextArea, JLabel, JButton 等等;

大概的层次结构就是,窗体包括画板,画板里面放组件,当然窗体也可以直接放组件。



基本使用方法

  • 构建窗体
frame = new JFrame("对话");								// 新窗体
frame.setBounds(600, 300, width, height);				// 设置(位置,大小)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	// 设置默认退出操作
frame.setResizable(false);								// 设置是否可以调整窗体大小
frame.setLayout(null);									// 空布局

这里的布局方式有好多种,流式布局、边界布局(东西南北中)、网格布局等等,空布局就没有任何限制,通过定义每个组件的位置和大小(setBounds方法)来确定布局,这里要注意,每个组件必须要定义位置和大小,否则就不会显示。

  • 定义常用组件
JTextArea textArea = new JTextArea();
textArea.setEditable(false);							// textArea不可编辑
JScrollPane scrollPane = new JScrollPane(textArea);		// 把textArea放在scrollPane里面,相当于给textArea加了一个滚轮
JButton button = new JButton("click");
  • 事件监听
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    	// 当button被点击时要做的事情
    }
});

事件监听有好多种,上述的仅仅是按键点击的监听,还有鼠标、键盘等等,需要用的时候再学(可能键盘之后需要用到)

  • 对组件设定位置和大小
scrollPane.setBounds(x, y, width, height);
  • 给窗体添加组件
frame.getContentPane().add(scrollPane);
frame.getContentPane().add(button);

我这里写的是定义了一个 JFrame 类型的 frame,然后向里面添加组件,实际上也可以直接让需要实现 GUI 的 java 类去继承 JFrame,这样可能更方便一些。

  • 设置窗体可见性
frame.setVisible(true);


例子

登录界面
  • 代码:
package windows;


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

public class Login {
    private JFrame frame;

    public Login() {
        frame = new JFrame("Login");
        frame.setBounds(600, 300, 350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 350, 200);
        frame.getContentPane().add(panel);
        place(panel);
    }

    private void place(JPanel panel) {
        panel.setLayout(null);

        JLabel userLabel = new JLabel("用户名:");
        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("密码:");
        passwordLabel.setBounds(10, 50, 80, 25);
        panel.add(passwordLabel);
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 50, 165, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("登录");
        loginButton.setBounds(100, 100, 80, 25);
        panel.add(loginButton);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = userText.getText();
                String password = String.copyValueOf(passwordText.getPassword());
                System.out.println(name + "\t" + password);
            }
        });
    }

    public void show() {
        frame.setVisible(true);
    }

    public void hide() {
        frame.setVisible(false);
    }

    public void close() {
        frame.dispose();
    }

    public static void main(String[] args) {
        Login login = new Login();
        login.show();
    }
}
  • 实际效果:
聊天框
  • 代码:
package windows;

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

public class Dialog {
    private JFrame frame;
    private final int width = 600;
    private final int height = 400;

    public Dialog() {
        frame = new JFrame("对话");
        frame.setBounds(600, 300, width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);

        JTextArea textArea1 = new JTextArea();
        textArea1.setEditable(false);
        JScrollPane scrollPane1 = new JScrollPane(textArea1);
        JTextArea textArea2 = new JTextArea();
        JScrollPane scrollPane2 = new JScrollPane(textArea2);
        JButton button = new JButton("发送");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s = textArea2.getText();
                textArea1.append(s + "\r\n");
                textArea2.setText("");
            }
        });

        int showHeight = height * 3 / 4;
        int buttonWidth = height - (showHeight + 2);
        scrollPane1.setBounds(0, 0, width, showHeight);
        scrollPane2.setBounds(0, showHeight + 2, width - buttonWidth - 2, buttonWidth);
        button.setBounds(width - buttonWidth, height - buttonWidth,
                buttonWidth * 2 / 3, buttonWidth / 3);

        frame.getContentPane().add(scrollPane1);
        frame.getContentPane().add(scrollPane2);
        frame.getContentPane().add(button);
    }

    public void show() {
        frame.setVisible(true);
    }

    public void hide() {
        frame.setVisible(false);
    }

    public void close() {
        frame.dispose();
    }

    public static void main(String[] args) {
        Dialog dialog = new Dialog();
        dialog.show();
    }
}
  • 实际效果:

猜你喜欢

转载自blog.csdn.net/dragonylee/article/details/107028182