基本组件 | 容器 |
---|---|
文本框、按钮、输入框 单选/多选按钮、下拉框/复选框 所有的组件都必须存在于容器之中 | 可放置基本组件的组件 窗体、面板、框架 顶层容器Container |
AWT-Abstract Window Toolkit | Swing-轻量级纯Java的图形库 |
---|---|
图形界面是平台相关的 不同平台外观有差异 | 不同平台外观一致 基于AWT,更丰富,功能更强大 |
AWT | Swing |
---|---|
- java.awt.* | - javax.swing.* |
- Label、Button、List、TextField | -JLabel、JButton、 Jlist、JTextField |
- Frame、Panel、Container | - JFrame、JPanel |
组件类名 | 描述 |
---|---|
JLabel | 不可编辑的文本行(可带图标) |
JTextField | 可编辑的文本行 |
JButton | 按钮 |
JCheckBox | 复选框 |
JRadioButton | 单选框 |
JComboBox | 下拉框 |
JPanel | 面板(容器) |
JFrame | 框架(容器 |
界面布局
设计图形界面时,应该考虑组件在容器中的位置、大小和组件之间的排列方式。
如果没有使用布局管理器,需要确定每个组件的大小和在容器中的坐标。当窗口大小改变时,组件不会自动调整位置和大小。
JButton btn=ynew JButton("Ok”);
btn.setBounds(10,20,60,20);
add(btn);
布局管理器
Java使用布局管理器简化组件布局工作决定图形组件的排列形式。
实现接口 java.awt.LayoutManager。
使用布局管理器的好处
当程序窗口尺寸改变时,布局管理器能自动调整组件的位置和大小。
通过容器组件的 setLayout() 方法设置容器中的组件排列形式。如:setLayout(new FlowLayout())。
FlowLayout | 最简单的界面布局方式,从左至右、从上至下按顺序依次摆放组件 |
BorderLayout | 按东南西北中五个区域摆放组件,通常作为窗口主框架 |
GridLayout | 各组件占用相等的格空间 |
警告1
在编写程序时,会出现“The serializable class does not declare a static final serialVersionUID field of type long”的警告,可通过更改设置来解决:Window > Preferences > Java > Compiler > Errors/Warnings > Potential Programming Problemsm,该设置只适用于eclipse编译器。
警告2
解决:利用系统自动添加注释
代码1
package java_swing;
import java.awt.FlowLayout;
import javax.swing.*;
//"*"表示引入java.swing包中所有的类
public class MyWindows extends JFrame{
//JFrame是框架(容器)
private JLabel label; //不可编辑文本行
private JTextField textField; //可编辑的文本行
private JButton button; //按钮
private JCheckBox checkBox; //复选框
private JRadioButton radioButton; //单选框
@SuppressWarnings("rawtypes")
private JComboBox combox; //下拉框
@SuppressWarnings({ "rawtypes", "unchecked" })
public MyWindows(){
this.setLayout(new FlowLayout());
//设置当前窗体的布局,即组件在页面中如何排版
label = new JLabel("label"); //实例化组件
this.add(label); //把组件加入到窗体中
textField=new JTextField(30); //30为字符宽度
this.add(textField);
button = new JButton("按钮"); //“按钮”显示在按钮框里
this.add(button);
checkBox = new JCheckBox(); //复选框
this.add(checkBox);
combox = new JComboBox(); //下拉框,必须添加列
//给下拉框添加选项,其长度由内容决定
combox.addItem("a");
combox.addItem("b");
combox.addItem("c");
this.add(combox);
radioButton=new JRadioButton(); //单选按钮
this.add(radioButton);
this.setSize(400, 300); //设置窗体的长宽
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 使得在关闭窗体的时候自动结束程序
* JFrame.EXIT_ON_CLOSE的值就是3
* 也可写成this.setDefaultCloseOperation(3)
*/
this.setVisible(true); //让窗体可见
}
@SuppressWarnings("unused")
public static void main(String[] args) {
MyWindows window =new MyWindows();
}
}
呈现1
代码2
package java_swing;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TestWindow extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label;
//MyLabel是自己手动封装的类
private MyLabel userNameLabel; //账号
private MyLabel passwordLabel; //密码
private JTextField userName; //用户名输入框
private JTextField password; //密码输入框
private JButton login; //登录按钮
private JButton exit; //退出按钮
public TestWindow(){
this.setTitle("登录");
this.setLayout(null);
//空布局,放置组件时必须设置位置和大小
this.setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//在关闭窗体的时候自动结束程序
label = new JLabel("欢迎进入学生管理系统");
label.setBounds(70,40,300,30);
//依次设置x轴、y轴、宽度、高度
label.setFont(new Font("黑体", 30, 30));
//依次设置字体、粗细、大小
this.add(label);
userNameLabel = new MyLabel("账号:");
userNameLabel.setBounds(75, 90, 50, 30);
this.add(userNameLabel);
passwordLabel = new MyLabel("密码:");
passwordLabel.setBounds(75, 140, 50, 30);
this.add(passwordLabel);
userName = new JTextField();
userName.setBounds(125, 90, 230, 30);
this.add(userName);
password = new JTextField();
password.setBounds(125, 140, 230, 30);
this.add(password);
login = new JButton("登录");
login.setBounds(135, 190, 70, 30);
this.add(login);
exit = new JButton("退出");
exit.setBounds(265, 190, 70, 30);
this.add(exit);
//让上面程序设置的窗体可见
this.setVisible(true);
}
public static void main(String[] args) {
@SuppressWarnings("unused")
TestWindow win = new TestWindow();
}
}
封装的MyLabel函数
package java_swing;
import java.awt.Font;
import javax.swing.JLabel;
public class MyLabel extends JLabel{
private static final long serialVersionUID = 1L;
public MyLabel(String text){
super(text); //调用父类的带参构造
this.setFont(new Font("黑体", Font.PLAIN,16));
}
}
呈现2
以上只是简单的界面设计,没有包括功能实现。