JAVA--019 GUI

GUI

java的图形用户界面编程。虽然拥有很完善的类和其他的机制来实现。但是由于环境必须配置jre导致用的人并不多。我们也只是粗略的学了下。

窗体 类JFrame

通过继承extends 使自己的变为一个窗体

容器 类Jpanel

同样通过继承使自己的类变成一个容器

组件

一个简单 的MyFrame来看看部分组件

package com.lovo.frame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//自定义窗体类
public class MyFrame extends JFrame {

    private Container contentP;// 专门指代内容面板

    private JLabel nameLab;// 用户名标签

    private JLabel pwdLab;

    private JLabel imgLab;

    private JTextField nameTxt;// 文本框

    private JPasswordField pwdTxt;// 密码框

    private JButton loginBtn;//登录按钮

    private JButton clearBtn;

    private JRadioButton maleRadio;//单选框

    private JRadioButton femaleRadio;

    private JComboBox<String> classComb;//下拉框 

    private JTextArea msgArea;//文本域

    private JMenuBar menuBar;//菜单栏

    private JMenu firstLevelMenu1;//第一层的第一个菜单

    private JMenu firstLevelMenu2;

    private JMenu secondLevelMenu1;//第二层的第一个菜单

    private JMenuItem openMenuItem;
    private JMenuItem closeMenuItem;
    private JMenuItem saveMenuItem;
    private JMenuItem exitMenuItem;
    private JMenuItem projectMenuItem;
    private JMenuItem classMenuItem;

    public MyFrame() {
        // 工具箱类--ToolKit
        Toolkit tk = Toolkit.getDefaultToolkit();
        // 设置窗体大小
        this.setSize(400, 510);
        // 设置窗体位置
        int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕宽
        int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高
        this.setLocation((screenW - 400) / 2, (screenH - 510) / 2);
        // 设置窗体大小不可更改
        this.setResizable(false);
        // 设置窗体图标
        this.setIconImage(tk.createImage("img/hp.JPG"));
        // 设置窗体的标题
        this.setTitle("我的第一个GUI程序");
        // 设置窗体关闭即为关闭程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗体内容面板上所有的东西
        this.addContent();
        // 设置窗体可见
        this.setVisible(true);
    }

    private void addContent() {
        // 找当前窗体对象获取它的内容面板
        this.contentP = this.getContentPane();
        // 设置窗体面板的背景色
        this.contentP.setBackground(Color.WHITE);
        // 设置容器的布局管理器为空---空布局管理
        this.contentP.setLayout(null);

        // 标签
        this.nameLab = new JLabel();
        this.nameLab.setText("用户名:");
        // 设置字体
        this.nameLab.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        // 设置字体颜色(前景色)
        this.nameLab.setForeground(Color.GREEN);
        // 给标签添加线边框--本代码专用于调试
        // this.nameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.nameLab.setBounds(30, 40, 90, 30);
        this.contentP.add(this.nameLab);

        this.pwdLab = new JLabel();
        this.pwdLab.setText("密   码:");
        this.pwdLab.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        this.pwdLab.setForeground(Color.GREEN);
        // 给标签添加线边框--本代码专用于调试
        // this.nameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.pwdLab.setBounds(30, 80, 90, 30);
        this.contentP.add(this.pwdLab);

        this.imgLab = new JLabel();
        this.imgLab.setIcon(new ImageIcon("img/tx.JPG"));
        this.imgLab.setBounds(312, 0, 88, 86);
        this.contentP.add(this.imgLab);

        // 文本框
        this.nameTxt = new JTextField();
        this.nameTxt.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        this.nameTxt.setForeground(Color.BLUE);
        this.nameTxt.setBounds(125, 40, 150, 30);
        this.contentP.add(this.nameTxt);

        // 密码框
        this.pwdTxt = new JPasswordField();
        this.pwdTxt.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        this.pwdTxt.setForeground(Color.BLUE);
        this.pwdTxt.setEchoChar('*');
//      this.pwdTxt.setEditable(false);//设置可编辑性
        this.pwdTxt.setBounds(125, 80, 150, 30);
        this.contentP.add(this.pwdTxt);

        // 按钮
        this.loginBtn = new JButton();
        this.loginBtn.setText("登录");
        this.loginBtn.setIcon(new ImageIcon("img/hp.JPG"));
        this.loginBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));
        this.loginBtn.setFont(new Font("微软雅黑", Font.ITALIC, 12));
        this.loginBtn.setForeground(Color.BLUE);
        this.loginBtn.setBounds(100, 120, 100, 30);
        this.contentP.add(this.loginBtn);

        this.clearBtn = new JButton();
        this.clearBtn.setIcon(new ImageIcon("img/buttonClear.jpg"));
        this.clearBtn.setBounds(210, 120, 140, 50);
        this.contentP.add(this.clearBtn);

        //水平分隔线
        JSeparator js = new JSeparator(JSeparator.HORIZONTAL);
        js.setBounds(20, 185, 360, 1);
        js.setForeground(new Color(193, 190, 173));
        this.contentP.add(js);

        //单选框
        this.maleRadio = new JRadioButton();
        this.maleRadio.setText("男");
        this.maleRadio.setBackground(Color.WHITE);
        this.femaleRadio = new JRadioButton("女");
        this.femaleRadio.setBackground(Color.WHITE);
        this.maleRadio.setBounds(50, 200, 80, 30);
        this.femaleRadio.setBounds(150, 200, 80, 30);
        this.maleRadio.setSelected(true);//设置默认选中
        this.contentP.add(this.maleRadio);
        this.contentP.add(this.femaleRadio);
        //单选框需要逻辑分组,这样才具备互斥关系
        ButtonGroup genderGroup = new ButtonGroup();
        genderGroup.add(this.maleRadio);
        genderGroup.add(this.femaleRadio);

        //复选框--JCheckBox---自学

        //下拉框
        this.classComb = new JComboBox<String>(new String[]{"J120","J121","J122","J123","J124"});
        this.classComb.addItem("J125");//单独添加选项
        this.classComb.setEditable(true);
        this.classComb.setSelectedIndex(-1);
//      this.classComb.setSelectedItem("J125");
        this.classComb.setBounds(30, 240, 80, 30);
        this.contentP.add(this.classComb);

        //文本域---JTextArea
        this.msgArea = new JTextArea();
        JScrollPane sp = new JScrollPane(this.msgArea);
        sp.setBounds(20, 280, 360, 200);
//      this.msgArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.contentP.add(sp);

        //菜单
        this.menuBar = new JMenuBar();
        //使用窗体对象设置菜单栏,不是内容面板;
        //调用的是setJMenuBar方法,不是setMenuBar方法
        this.setJMenuBar(this.menuBar);

        this.firstLevelMenu1 = new JMenu("文件");
        this.firstLevelMenu2 = new JMenu("编辑");
        this.menuBar.add(this.firstLevelMenu1);
        this.menuBar.add(this.firstLevelMenu2);

        this.secondLevelMenu1 = new JMenu("新建");
        this.firstLevelMenu1.add(this.secondLevelMenu1);

        this.openMenuItem = new JMenuItem("打开");
        this.closeMenuItem = new JMenuItem("关闭");
        this.saveMenuItem = new JMenuItem("保存");
        this.exitMenuItem = new JMenuItem("退出");
        this.firstLevelMenu1.addSeparator();
        this.firstLevelMenu1.add(this.openMenuItem);
        this.firstLevelMenu1.add(this.closeMenuItem);
        this.firstLevelMenu1.add(this.saveMenuItem);
        this.firstLevelMenu1.addSeparator();//添加菜单分隔线
        this.firstLevelMenu1.add(this.exitMenuItem);

        this.projectMenuItem = new JMenuItem("工程");
        this.classMenuItem = new JMenuItem("类");
        this.secondLevelMenu1.add(this.projectMenuItem);
        this.secondLevelMenu1.add(this.classMenuItem);
    }

}

关于布局和其他部分均在下面的代码中。

GUI

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值