java中组件类都位于: java.awt 和 javax.swing
区别:
1.二者存在相同图形类,区别在于swing的图形都以J开头
2.java.awt中的图形类的图形依赖系统(windows.linux)图形库
3.javax.swing中的图形类的图形都是sun自己去实现的
所有的图形类都称作为组件:
容器组件
非容器组件
容器组件:
一、窗体
public class JFrame1 {
public static void main(String[] args) {
// 创建一个窗体对象
JFrame frame = new JFrame("第一个窗体");
// 设置窗体的大小(象素为单位)
// frame.setSize(300, 400);
// 设置窗体左上角出现的位置,公式自己推导一下,哈哈,,只能适应固定屏幕分辨率
// frame.setBounds((1366-300)/2, (768-400)/2, 300, 400);
// 获取分辨率设置位置,窗体大小
initFrame(frame, 300, 400);
// 设置窗体的可见性
frame.setVisible(true);
// 设置窗体的关闭事件
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 设置窗体出现在中间位置
public static void initFrame(JFrame frame, int width, int height) {
// 获取默认系统工具包
Toolkit toolkit = Toolkit.getDefaultToolkit();
// 获取屏幕的分辨率。
Dimension d = toolkit.getScreenSize();
int x = (int) d.getWidth();
int y = (int) d.getHeight();
// 根据屏幕分辨率设置位置
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);
}
}
二、对话框
Dialog(对话框)
Dialog(Dialog owner, String title, boolean modal)
owner : 父级组件(窗体等)
title : 对话框的标题
modal : 模式
true: 对话框没有关闭的时候,不准操作父级组件(窗体等)。
false: 对话框即使没有关闭,还是可以操作父级组件(窗体等)
JOptionPane(对话框)
//消息、 警告、 错误
showMessageDialog(Component parentComponent, Object message, String title, int messageType)
parentComponent: 父级组件(窗体等)
message : 显示的消息
title : 对话框的标题
messageType : 指定对话框的类型(消息、 警告、 错误)
public class Dialog1 {
public static void main(String[] args) {
// 创建一个窗体
JFrame frame = new JFrame("窗体");
// 设置窗体
FrameUtil.initFrame(frame, 500, 400);
// frame.setBounds((1366-300)/2, (768-400)/2, 300, 400);
// 创建一个对话框
// 方式一:不好看
/*
* Dialog dialog2=new Dialog(frame, "dd",false);
* 设置对话框位置属性+尺寸
* dialog2.setBounds(580, 250, 200, 200);
* dialog2.setVisible(true);
*/
// 方式二:好看一点的
JDialog dialog = new JDialog(frame, "对话框", false);
// 设置对话框位置属性+尺寸
dialog.setBounds(580, 250, 200, 200);
dialog.setVisible(true);
// 消息对话框
JOptionPane.showMessageDialog(frame, "明天放假了!爽!!", "通知:", JOptionPane.INFORMATION_MESSAGE);
// 警告对话框
JOptionPane.showMessageDialog(frame, "别在嫌我丑了", "警告:", JOptionPane.WARNING_MESSAGE);
// 错误对话框
JOptionPane.showMessageDialog(frame, "别睡觉了..", "出局:", JOptionPane.ERROR_MESSAGE);
// 确认对话框
int select = JOptionPane.showConfirmDialog(frame, "软件继续安装吗?");
System.out.println("num:" + select);
// 输入对话框
String money = JOptionPane.showInputDialog(frame, "请输入你要取的金额:");
System.out.println("money:" + money);
}
}
FileDialog(文件对话框)
FileDialog(Dialog parent, String title, int mode)
parent : 父级组件(窗体等)
title: 标题
mode: FileDialog.LOAD(加载)
FileDialog.SAVE(保存)
//文件对话框
public class Dialog2 {
public static void main(String[] args) {
// 创建窗体
JFrame frame = new JFrame("窗体");
// 设置窗体
FrameUtil.initFrame(frame, 500, 400);
// 创建文件对话框
// FileDialog dialog = new FileDialog(frame, "打开文件", FileDialog.SAVE);
FileDialog dialog = new FileDialog(frame, "打开文件", FileDialog.LOAD);
// 设置显示dialog
dialog.setVisible(true);
// 获取文件的路径
System.out.println("文件的路径:" + dialog.getDirectory());
// getFile() 获取文件名。
System.out.println("文件名:" + dialog.getFile());
}
}
三、面板
public class Panel1 {
public static void main(String[] args) {
JFrame frame = new JFrame("窗体");
// 面板
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
// 把面板添加到窗体上。
frame.add(panel);
FrameUtil.initFrame(frame, 500, 400);
}
}
public class Panel2 {
public static void main(String[] args) {
JFrame frame = new JFrame("注册");
// 创建一个面板
JPanel panel = new JPanel();
// 创建一个标签对象
JLabel nameLabel = new JLabel("用户名:");
// 输入框
JTextField nameField = new JTextField(12);
// 把组件添加面板
panel.add(nameLabel);
panel.add(nameField);
// 密码
JLabel passLabel = new JLabel("密码:");
// 密码框
JPasswordField passField = new JPasswordField(12);
panel.add(passLabel);
panel.add(passField);
// 性别: 单选框
JLabel sexLabel = new JLabel("性别:");
// 单选框 注意: 单选框一定要进行分组,在同一组的单选框中只能选择其中的一个 true:默认值
JRadioButton man = new JRadioButton("男", true);
JRadioButton woman = new JRadioButton("女");
// 分组
ButtonGroup group = new ButtonGroup();
group.add(man);
group.add(woman);
// 把组件添加面板
panel.add(sexLabel);
panel.add(man);
panel.add(woman);
// 城市 --- 下拉框
JLabel cityLabel = new JLabel("来自城市");
Object[] citys = { "北京", "上海", "广州", "深圳" };
JComboBox<Object> cityBox = new JComboBox<Object>(citys);
panel.add(cityLabel);
panel.add(cityBox);
// 爱好---复选框
JLabel hobitLabel = new JLabel("爱好");
JCheckBox java = new JCheckBox("java");
JCheckBox javascript = new JCheckBox("JS");
JCheckBox write = new JCheckBox("敲java");
panel.add(hobitLabel);
panel.add(java);
panel.add(javascript);
panel.add(write);
// 自我简介
JLabel introLabel = new JLabel("自我简介:");
JTextArea area = new JTextArea(15, 15);
panel.add(introLabel);
panel.add(area);
frame.add(panel);
FrameUtil.initFrame(frame, 830, 500);
}
}
非容器组件
一、菜单组件:
菜单条(JMenuBar) 菜单(JMenu) 菜单项(JMenuItem)
关系: 菜单条添加菜单 , 菜单添加菜单项
复选菜单: 菜单添加菜单, 然后菜单再添加菜单项。
public class Menu1 {
// 窗体
JFrame frame = new JFrame("记事本");
// 菜单条
JMenuBar bar = new JMenuBar();
// 菜单
JMenu fileMenu = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
JMenu helpMenu = new JMenu("帮助");
// 菜单项
JMenuItem open = new JMenuItem("打开");
JMenuItem save = new JMenuItem("保存");
JMenuItem copy = new JMenuItem("拷贝");
JMenuItem about = new JMenuItem("关于");
JMenuItem version = new JMenuItem("升级");
// 文本域
JTextArea area = new JTextArea(20, 20);
public void init() {
// 把菜单添加到菜单条上
bar.add(fileMenu);
bar.add(editMenu);
// 把菜单项添加到菜单
fileMenu.add(open);
fileMenu.add(save);
editMenu.add(copy);
// 复选菜单, 菜单添加到菜单上。
editMenu.add(helpMenu);
// 菜单添加菜单项
helpMenu.add(about);
helpMenu.add(version);
// 把菜单条添加到窗体上
frame.add(bar, BorderLayout.NORTH);
frame.add(area);
//设置窗体
FrameUtil.initFrame(frame, 500, 600);
}
public static void main(String[] args) {
new Menu1().init();
}
}
布局管理器 :
作用就:摆放组件。不同布局管理器有不同风格
1.BorderLaytout(边框布局管理器):
borderLayout 注意:
1. 容器使用了BorderLayout布局管理器,容器中组件没有指定具体的方位,默认在中间。
2. Frame默认使用BorderLayout布局管理器.
3. 中间的组件都会占据其东南西北那个组件空缺位置。
public class BorderLayout1 {
public static void main(String[] args) {
// 创建一个窗体
JFrame frame = new JFrame("边框布局管理器");
// 创建边框布局管理器
BorderLayout borderLayout = new BorderLayout();
// 让窗体使用边框布局管理器
frame.setLayout(borderLayout);
// 位置摆放
frame.add(new JButton("北"), BorderLayout.NORTH);
frame.add(new JButton("南"), BorderLayout.SOUTH);
frame.add(new JButton("西"), BorderLayout.WEST);
frame.add(new JButton("东"), BorderLayout.EAST);
frame.add(new JButton("中"), BorderLayout.CENTER);
// 初始化窗体
FrameUtil.initFrame(frame, 300, 300);
}
}
2.FlowLayout 流式布局管理器
FlowLayout注意:
1. 使用FlowLayout的时候默认是居中对齐的。
2. panel默认使用的布局管理器就是FlowLayout.
public class FlowLayout1 {
public static void main(String[] args) {
JFrame frame = new JFrame("窗体");
// 面板
JPanel panel = new JPanel();
// 创建一个流式布局管理器
panel.setBackground(Color.BLUE);
// 默认居中对其
// FlowLayout flowLayout = new FlowLayout();
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 10, 0);
// 让面板使用流式布局管理器
panel.setLayout(flowLayout);
frame.add(panel);
panel.add(new JButton("one"));
panel.add(new JButton("two"));
panel.add(new JButton("three"));
panel.add(new JButton("four"));
FrameUtil.initFrame(frame, 300, 300);
}
}
3.GridLayout 表格布局管理器
如果添加的组件超过了表格的个数,那么会添加多一列处理
public class GridLayout1 {
public static void main(String[] args) {
// 创建一个窗体
JFrame frame = new JFrame("计算器");
// 创建一个表格布局管理器
GridLayout gridLayout = new GridLayout(4, 4);
frame.setLayout(gridLayout);
for (int i = 0; i < 10; i++) {
frame.add(new JButton(i + ""));
}
frame.add(new JButton("+"));
frame.add(new JButton("-"));
frame.add(new JButton("*"));
frame.add(new JButton("/"));
frame.add(new JButton("="));
frame.add(new JButton("."));
FrameUtil.initFrame(frame, 250, 300);
}
}
事件:组件发生了指定的动作时,会有相应的处理方案。
事件组成: 事件源、监听器、事件、处理方式
动作监听器:动作监听器针对 鼠标点击 和 空格按键
public class Listener1 {
public static void main(String[] args) {
// 创建窗体
JFrame frame = new JFrame("窗体");
// 创建一个按钮
JButton button = new JButton("点我啊");
// 个按钮添加一个监听器
button.addActionListener(new ActionListener() {
// 如果发生鼠标点击、按下空格键就会调用actionPerformed方法
@Override
public void actionPerformed(ActionEvent e) {
// 获取事件源对象
JButton button = (JButton) e.getSource();
String content = button.getText();
if ("点我啊".equals(content)) {
button.setText("点他吧!");
} else {
button.setText("点我啊");
}
}
});
frame.add(button);
FrameUtil.initFrame(frame, 200, 200);
}
}
鼠标监听器
public class Listener2 {
public static void main(String[] args) {
JFrame frame = new JFrame("窗体");
// 创建一个按钮
JButton button = new JButton("点我啊");
// 给按钮添加一个鼠标监听器
/*
* button.addMouseListener(new MouseListener() {
*
* @Override public void mouseReleased(MouseEvent e) {
* System.out.println("鼠标松开.."); }
*
* @Override public void mousePressed(MouseEvent e) {
* System.out.println("鼠标按下..."); }
*
* @Override public void mouseExited(MouseEvent e) {
* System.out.println("鼠标离开..."); }
*
* @Override public void mouseEntered(MouseEvent e) {
* System.out.println("鼠标进入...."); }
*
* @Override public void mouseClicked(MouseEvent e) {
* System.out.println("鼠标单击...."); } });
*
*/
// MouseAdapter适配器 ---- 该类已经实现了MouseListener接口,但是实现的方法全部都是空实现。
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println("双击了...");
}
}
});
frame.add(button);
FrameUtil.initFrame(frame, 200, 200);
}
}
键盘监听器
public class Listener3 {
public static void main(String[] args) {
JFrame frame = new JFrame("窗体");
// 创建一个按钮
JButton button = new JButton("点我啊");
// 给按钮添加一个键盘监听器
/*
* button.addKeyListener(new KeyListener() {
*
* @Override public void keyTyped(KeyEvent e) {
* System.out.println(" 键入某个键..."); }
*
* @Override public void keyReleased(KeyEvent e) {
* System.out.println("释放键 ..."); }
*
* @Override public void keyPressed(KeyEvent e) {
* System.out.println("按下某个键..."); } });
*/
button.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("按下键的字符:" + e.getKeyChar() + " 键的code:" + e.getKeyCode());
}
});
frame.add(button);
FrameUtil.initFrame(frame, 200, 200);
}
}