拆分窗口及聊天窗口的代码实现

2017-12-12 10:37:44

拆分窗口

在一些场景中,我们需要对窗口进行拆分,下面是一段简单的示例代码

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

public class Test extends JFrame {

    JSplitPane cf;
    JList lb;
    JLabel bq;

    public static void main(String[] args)
    {
        Test test = new Test();
    }
    public Test()
    {
        String[] jsj = {"软件开发","游戏开发","平面设计","动画制作","室内设计"};
        lb = new JList(jsj);
        bq = new JLabel(new ImageIcon("image/bg.jpg"));
//        cf = new JSplitPane(JSplitPane.VERTICAL_SPLIT,lb,bq);//上下拆分
        cf = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,lb,bq); //左右拆分
        cf.setOneTouchExpandable(true); //设置伸缩特性

        this.add(cf);
        this.setTitle("计算机科学分类");
        this.setSize(640,480);
        this.setLocation(300,280);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

运行结果如下:

下面是一个聊天窗口的简单代码

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

public class Test extends JFrame {

    JTextArea wby;
    JPanel mb;
    JComboBox xlk;
    JButton an;
    JTextField wbk;
    JScrollPane gd;

    public static void main(String[] args)
    {
        Test test = new Test();
    }

    public Test()
    {
        wby = new JTextArea();
        mb = new JPanel();
        String[] lt = {"悟空","唐僧","小白龙"};
        xlk = new JComboBox(lt);
        wbk = new JTextField(10);
        an = new JButton("发送");
        gd = new JScrollPane(wby);

        mb.add(xlk);
        mb.add(wbk);
        mb.add(an);
        this.add(gd);
        this.add(mb,BorderLayout.SOUTH);

        this.setTitle("聊天窗口");
        this.setSize(300,200);
        this.setIconImage((new ImageIcon("image/qq.png")).getImage());
        this.setLocation(300,280);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

}

运行结果如下