GUI编程
概述
GUI(Graphical Uers Interface)全称图形用户界面
swing指javax.swing包,该包中包含实现界面的类,这些类都可称为组件
组件可分为两大类:
容器组件
窗口
import javax.swing.*;
public class LoginFrame extends JFrame {
//在构造方法中对窗口进行设置
public LoginFrame(){
//设置窗口大小
this.setSize(300,300);
//设置窗口标题
this.setTitle("登录");
// //设置窗口生成位置
// this.setLocation(600,250);
//设置窗口生成位置水平居中
this.setLocationRelativeTo(null);
//设置窗口不可调整大小
this.setResizable(false);
//关闭窗口选项
//JFrame.EXIT_ON_CLOSE-->关闭窗口时程序停止运行
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗口,一般在完成设置后在最后一行
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new LoginFrame();
}
}
面板
轻量级的容器,可以布局-->流式布局、边界布局、网格布局...
import javax.swing.*;
import java.awt.*;
//面板的流式布局
public class LoginFrame3 extends JFrame{
//在构造方法中对窗口进行设置
public LoginFrame3(){
//设置窗口大小
this.setSize(300,300);
//设置窗口标题
this.setTitle("登录");
//自定义设置窗口生成位置
//this.setLocation(600,250);
//设置窗口生成位置水平居中
this.setLocationRelativeTo(null);
//设置窗口不可调整大小
this.setResizable(false);
//关闭窗口选项
//JFrame.EXIT_ON_CLOSE-->关闭窗口时程序停止运行
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
面板可以设置布局管理器
流式布局:组件从面板上从左到右排列,满时换行
*/
//创建面板 流式布局-->默认水平居中,上下左右5个像素间距
JPanel jPanel = new JPanel(new FlowLayout());
//流式布局-->水平居左
//JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
//
//JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
//创建功能组件-->按钮组件
JButton jButton1 = new JButton("进 入");
JButton jButton2 = new JButton("进 入");
JButton jButton3 = new JButton("进 入");
//把组件添加到面板上
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
//把面板添加到窗口上
this.add(jPanel);
//显示窗口,一般在完成设置后在最后一行
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new LoginFrame3();
}
}
import javax.swing.*;
import java.awt.*;
//面板的边界布局
public class LoginFrame4 extends JFrame{
//在构造方法中对窗口进行设置
public LoginFrame4(){
//设置窗口大小
this.setSize(300,300);
//设置窗口标题
this.setTitle("登录");
//自定义设置窗口生成位置
//this.setLocation(600,250);
//设置窗口生成位置水平居中
this.setLocationRelativeTo(null);
//设置窗口不可调整大小
this.setResizable(false);
//关闭窗口选项
//JFrame.EXIT_ON_CLOSE-->关闭窗口时程序停止运行
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
面板可以设置布局管理器
边界布局:
NORTH
WEST
CENTER-->必须有
EAST
SOUTH
*/
//创建面板 边界布局
JPanel jPanel = new JPanel(new BorderLayout());
//创建功能组件-->按钮组件
JButton jButton1 = new JButton("进 入1");
JButton jButton2 = new JButton("进 入2");
JButton jButton3 = new JButton("进 入3");
JButton jButton4 = new JButton("进 入4");
JButton jButton5 = new JButton("进 入5");
//把组件添加到面板上
jPanel.add(jButton1,BorderLayout.NORTH);
jPanel.add(jButton2,BorderLayout.WEST);
jPanel.add(jButton3,BorderLayout.CENTER);//居中
jPanel.add(jButton4,BorderLayout.EAST);
jPanel.add(jButton5,BorderLayout.SOUTH);
//把面板添加到窗口上
this.add(jPanel);
//显示窗口,一般在完成设置后在最后一行
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new LoginFrame4();
}
}
import javax.swing.*;
import java.awt.*;
//面板的网格布局
public class LoginFrame5 extends JFrame{
//在构造方法中对窗口进行设置
public LoginFrame5(){
//设置窗口大小
this.setSize(300,300);
//设置窗口标题
this.setTitle("登录");
//自定义设置窗口生成位置
//this.setLocation(600,250);
//设置窗口生成位置水平居中
this.setLocationRelativeTo(null);
//设置窗口不可调整大小
this.setResizable(false);
//关闭窗口选项
//JFrame.EXIT_ON_CLOSE-->关闭窗口时程序停止运行
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
面板可以设置布局管理器
网格布局
*/
//创建面板 网格布局
JPanel jPanel = new JPanel(new GridLayout(2,1));
//创建功能组件-->按钮组件
JButton jButton1 = new JButton("进 入1");
JButton jButton2 = new JButton("进 入2");
JButton jButton3 = new JButton("进 入3");
JButton jButton4 = new JButton("进 入4");
JButton jButton5 = new JButton("进 入5");
//把组件添加到面板上
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(jButton4);
jPanel.add(jButton5);
//把面板添加到窗口上
this.add(jPanel);
//显示窗口,一般在完成设置后在最后一行
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new LoginFrame5();
}
}
对话框
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Module4 extends JFrame{
public Module4(){
this.setSize(300,300);
this.setTitle("");
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel(new FlowLayout());
JButton jButton = new JButton("哈哈");
jPanel.add(jButton);
this.add(jPanel);
this.setVisible(true);
jButton.addActionListener(new ActionListener() {
@Override
//对话框
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(null, "哈哈");
//JOptionPane.showMessageDialog(null, "哈哈","哈哈",JOptionPane.WARNING_MESSAGE);
//JOptionPane.showMessageDialog(null, "哈哈","哈哈",JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null, "哈哈","哈哈",JOptionPane.QUESTION_MESSAGE);
//JOptionPane.showMessageDialog(null, "哈哈","哈哈",JOptionPane.ERROR_MESSAGE);
//选择对话框 是0 否1 取消2
int num = JOptionPane.showConfirmDialog(null, "哈哈");
System.out.println(num);
//是0 取消2
int num = JOptionPane.showConfirmDialog(null, "哈哈","哈哈",JOptionPane.OK_CANCEL_OPTION);
System.out.println(num);
//输入对话框
String str = JOptionPane.showInputDialog(null,"哈哈");
System.out.println(str);
}
});
}
public static void main(String[] args) {
//创建一个窗口
new Module4();
}
}
功能组件
按钮
import javax.swing.*;
import java.awt.*;
//添加面板
public class LoginFrame2 extends JFrame{
//在构造方法中对窗口进行设置
public LoginFrame2(){
//设置窗口大小
this.setSize(300,300);
//设置窗口标题
this.setTitle("登录");
//自定义设置窗口生成位置
//this.setLocation(600,250);
//设置窗口生成位置水平居中
this.setLocationRelativeTo(null);
//设置窗口不可调整大小
this.setResizable(false);
//关闭窗口选项
//JFrame.EXIT_ON_CLOSE-->关闭窗口时程序停止运行
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建面板
JPanel jPanel = new JPanel();
jPanel.setBackground(Color.CYAN);
//自定义颜色
jPanel.setBackground(new Color(157, 253, 150, 255));
//创建功能组件-->按钮组件
JButton jButton = new JButton("进 入");
//把组件添加到面板上
jPanel.add(jButton);
//把面板添加到窗口上
this.add(jPanel);
//显示窗口,一般在完成设置后在最后一行
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new LoginFrame2();
}
}
菜单
import javax.swing.*;
import java.awt.*;
public class Module3 extends JFrame{
public Module3(){
this.setSize(500,500);
this.setTitle("记事本");
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单栏
JMenuBar jMenuBar = new JMenuBar();
//把菜单栏添加到窗口上
this.setJMenuBar(jMenuBar);
//创建菜单
JMenu jMenu1 = new JMenu("文件");
JMenu jMenu2 = new JMenu("编辑");
JMenu jMenu3 = new JMenu("帮助");
//创建菜单选项
JMenuItem jMenuItem1 = new JMenuItem("新建");
JMenuItem jMenuItem2 = new JMenuItem("保存");
JMenuItem jMenuItem3 = new JMenuItem("关于");
jMenu1.add(jMenuItem1);
jMenu2.add(jMenuItem2);
jMenu3.add(jMenuItem3);
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
JPanel jPanel = new JPanel(new FlowLayout());
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new Module3();
}
}
输入框
import javax.swing.*;
import java.awt.*;
public class Module1 extends JFrame{
public Module1(){
this.setSize(300,300);
this.setTitle("");
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel(new FlowLayout());
//标签组件
JLabel jLabelZh = new JLabel("账号");
//设置名字
//jLabel.setText();
//设置字体
jLabelZh.setFont(new Font("楷体",Font.BOLD,15));
//输入框组件
JTextField jTextField = new JTextField(20);
//获得输入框内容
jTextField.getText();
JLabel jLabelMm = new JLabel("密码");
jLabelMm.setFont(new Font("楷体",Font.BOLD,15));
//密码框
JPasswordField jPasswordField = new JPasswordField(20);
//获得密码框内容
String str = new String(jPasswordField.getPassword());
jPanel.add(jLabelZh);
jPanel.add(jTextField);
jPanel.add(jLabelMm);
jPanel.add(jPasswordField);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
//创建一个窗口
new Module1();
}
}
事件处理
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Module3 extends JFrame{
public Module3(){
this.setSize(500,500);
this.setTitle("记事本");
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单栏
JMenuBar jMenuBar = new JMenuBar();
//把菜单栏添加到窗口上
this.setJMenuBar(jMenuBar);
//创建菜单
JMenu jMenu1 = new JMenu("文件");
JMenu jMenu2 = new JMenu("编辑");
JMenu jMenu3 = new JMenu("帮助");
//创建菜单选项
JMenuItem jMenuItem1 = new JMenuItem("新建");
JMenuItem jMenuItem2 = new JMenuItem("保存");
JMenuItem jMenuItem3 = new JMenuItem("关于");
jMenu1.add(jMenuItem1);
jMenu2.add(jMenuItem2);
jMenu3.add(jMenuItem3);
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
JTextField jTextField = new JTextField(20);
JPanel jPanel = new JPanel(new FlowLayout());
jPanel.add(jTextField);
JButton jButton = new JButton("保存");
jPanel.add(jButton);
this.add(jPanel);
this.setVisible(true);
//事件处理,为组件添加监听
//匿名内部类
jMenuItem3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("记事本VT.1.0.001");
}
});
//按钮和输入框
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = jTextField.getText();
if (str.length() == 0){
//对话框组件
JOptionPane.showMessageDialog(null, "账号为空!");
return;
}
}
});
//输入框添加事件监听
jTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
System.out.println(jTextField.getText());
}
});
}
public static void main(String[] args) {
//创建一个窗口
new Module3();
}
}
内部类
public class A {
private int num = 10;
/*
内部类
在一个类的内部定义的类
类B只服务于类A,其他类用不到
1.内部类可以直接访问外部类的成员
2.内部类只服务于当前外部类
*/
public class B{
public void show(){
System.out.println(num);
}
}
public static void main(String[] args) {
}
}