Java GUI AWT如何搭建一个基本的GUI界面.

Java :swing and AWT 抽象窗口工具组(Abstract Window Toolkit=AWT)


一、AWT

1.包含很多类和接口GUI
2.元素:窗口,按钮,文本框…
3. java.awt
4.组件框架.

在这里插入图片描述

二、如何搭建一个基本的GUI界面.

1. 新建一个Frame
1.1 直接 ctrl + 左键点击 Frame() 查看源码知道(比jdk好哟用),需要添加一个String类型的题目 title
Frame frame = new Frame("title");
2. 设置可见性 ,(也可以在最后设置) frame.setVisiable(true);
2.1 使用实例 frame. 然后会弹出实例可以用的方法,这也是查看源码的一种方法,比jdk好用。
3. 设置大小 setSize(); 单位像素
3.1 使用 2.1 的查看源码的方法得知由2个参数 int width 和 int height 宽高.
frame.setSize(300,300);
4.用同样的方法设置窗体背景颜色 蓝色setBackground(Color.blue);
4.1 还可以使用 new Color(int red;int green;int blue;int a)设置色比例和a 透明度来设置颜色,详情看源码.
5. 设置初始坐标. setLoation() ;
5.1 看源码得知,需要2个参数 x ,y 最左上角的坐标,单位像素
6.设置窗体的是否可缩放 setResize(true);
7.设置退出事件监听,让程序点击关闭后能正常退出.
7.1 给frame添加窗口监听,Windowadapter 为window适配器只需要添加一个重写(WindowClosing)的方法即可.
frame1.addWindowListener( new WindowAdapter());
7.2 :在重构Override中写入退出的代码,
 System.out.println("退出成功.");
 System.exit(0);
8.总结以上为常用 方法 还有很多方法自己看源码.
二、总代码
package GUI.第一个GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * GUI 的第一个界面
 */
public class Demo {
    public static void main(String[] args) {
        //1.直接ctrl + 点击Frame()查看源码括号需要添加什么东西.
        Frame frame = new Frame("半亩方糖的第一个GUI界面");
        //2.需要设置可见性,直接使用实例 frame.然后看出现的方法来判断有哪些方法可用。
        frame.setVisible(true);
        //3. 由 2 知,可以设置大小 setSize(),看源码.
        frame.setSize(400,400);
        //4. 由 2 知道,可以设置属性,看源码.idea左边可以快捷选择颜色.
        frame.setBackground(new Color(139, 168, 20));
        //5.设置初始坐标
        frame.setLocation(500,100);
        //6.设置窗口固定
        frame.setResizable(false);
        //7.设置窗口监听,退出监听.
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {

                System.out.println("退出成功!");//打印消息
                System.exit(0);//返回为 0 的退出.
            }
        });



    }
}

输出

在这里插入图片描述

javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JarvanStack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值