【Java GUI】图形用户接口总结(1)

来源:互联网 发布:linux 删除自建路由表 编辑:程序博客网 时间:2024/05/29 08:52

一、简单窗口的显示

import java.awt.Color;import java.awt.Container;import java.awt.Label;import javax.swing.*;public class SwingWindow extends JFrame{    Label label;//窗口内部的字本质是label;    public SwingWindow(){        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //关闭进程,上次在贪吃蛇里说过;        setTitle("简单窗口");//左上角窗口名字        setSize(400,400);//窗口大小,JFrame里是setSize()        label = new Label("简单窗口实例");//label内容初始化;        Container myCP = getContentPane();        //用来获得JFrame的内容面板;        myCP.add(label);//添加组件到容器;        myCP.setBackground(Color.orange);//设置面板背景颜色    }    public static void main(String arg[])    {        SwingWindow mywin = new SwingWindow();        mywin.setVisible(true);//显示窗口    }}

运行结果

这里写图片描述

Swing软件包中包含了实现GUI的所有组件,从功能上可以分为顶层容器和Swing组件。
(1)顶层容器有三种:
JFrame(设计窗口应用程序);
JDialog(设计实现对话框);
JApplet(设计可以嵌入在网页中的Java小程序);
(2)组件分为容器组件和非容器组件:
容器组件:面板(panel)等;
非容器组件:JLabel,JButton等;
容器组件必须放在顶层容器中,非容器组件可以通过三部曲放在组件容器中:

JPanel panel = new JPanel();JLabel label = new JLabel();panel.add(label);

二、基本组件的应用

(自定义是在太麻烦了,非必要直接用边布局吧)

package GUI;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.*;import javax.swing.*;public class SwingWindow extends JFrame{    public SwingWindow(){        setLayout(null);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setTitle("历乱");        setSize(640,480);        Container myCP = getContentPane();        //注意:设置访问权限应该在构造外面定义变量,        //在构造方法内一次性完成初始化和定义的话,访问修饰符无意义;        JTextArea baseText = new JTextArea();        JTextArea copyText = new JTextArea();        JScrollPane sp1,sp2;        baseText.setLineWrap(true);        copyText.setLineWrap(true);        sp1 = new JScrollPane(baseText);        sp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        sp2 = new JScrollPane(copyText);        sp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        JLabel label = new JLabel("李智恩小仙女");        JLabel username = new JLabel("用户名:");        JLabel password = new JLabel("密码:");        JTextField displayText = new JTextField(30);        JTextField inputname = new JTextField("请输入用户名");        inputname.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {                if(e.getSource() == inputname)                    displayText.setText("用户名:"+inputname.getText());            }        });        JPasswordField inputpwd = new JPasswordField();        inputpwd.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {                    if(e.getSource() == inputpwd)                        displayText.setText("密码:"+new String(inputpwd.getPassword()));            }        });        JButton btn = new JButton("确认");        btn.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e) {                //按钮事件            }        });        sp1.setBounds(60, 300, 200, 200);        sp2.setBounds(300, 300, 200, 200);        username.setBounds(60, 100, 60, 20);        password.setBounds(60, 150, 50, 20);        inputpwd.setBounds(100, 150, 200, 20);        inputname.setBounds(100, 100, 100, 20);        displayText.setBounds(100, 200,300,20);        label.setBounds(600, 400, 100, 20);        btn.setBounds(600,450,80,20);        myCP.add(label);        myCP.add(btn);        myCP.add(inputname);        myCP.add(inputpwd);        myCP.add(username);        myCP.add(password);        myCP.add(displayText);        myCP.setBackground(Color.pink);        myCP.add(sp1);        myCP.add(sp2);}    public static void main(String arg[])    {        SwingWindow mywin = new SwingWindow();        mywin.setVisible(true);    }}

这里写图片描述

原创粉丝点击