一、AWT与Swing
- 两者区别:
1. AWT是最早开发出来的,比较粗略,对硬件依赖性强;
2. Swing组件相对美观且对硬件依赖性弱;
3.import javax.swing.*
- 两者联系:
1. Swing 继承了 AWT
二、容器
- 顶层容器
1.JFrame();
2.JFeame(String title);
3. 设置位置和宽高:setBounds(int x,int y,int width,int height)
4. 设置尺寸:setSize(int width,int height)
5. 设置背景颜色:setBackground(Color c)
6. 设置可见:setVisible(boolean flag)
7. 设置紧凑排列:pack()
8. 设置标题:setTitle(String title)
9. 获取内容窗格:getContentPane()
10. 设置布局管理器:setLayout(LayoutManager m)
- 内容窗格
//通过顶层容器设置内容窗格 Container cp= frame.getContentPane(); Button b = new Button("11") cp.add(b,BprderLayout.CENTER) //创建一个新的内容窗格 Jpanel jp= new JPanel();//创建面板 jp.setLayout(newBorderLayout) jp.add(b,BprderLayout.CENTER) frame.setContentPane(jp);//设置内容窗格
- 面板
1. 普通面板:Jpanel()
2. 滚动面板:JScrollPane();JScrollPane(Component view);
3. 设置水平滚动策略:void setHorizontalScrollBarPolicy(int policy)
Needed:需要时,Never:从不,Always:总是;
4. 设置垂直滚动策略:void setVerticalScrollBarPolicy(int policy)
5. 垂直滚动条:Scrollbar();
指定方向<H:水平,V:垂直>:Scrollbar(int o);
指定方向、初始值、可视量、最小值和最大值ScrollBar(int o,int value,int visible,int mini,int max)
三、标签和按钮
- 标签:构造方法可设置文本、对齐方式和图标
- 按钮:构造方法可设置文本和图标
1. 设置启用:void setEnabled(boolean b)
2. 设置快捷键:void setMnemonic(int c)
3. 注册点击事件:addActionListener(ActionListener a)
4. 获取事件源:e.getSource()
5. 获取事件源命令字符串:e.getActionCommand()
- 切换按钮
JToggleButton
- 复选按钮
JCheckBox
、单选按钮JRadioButton
1. 是否被选中:isSelected
2. 注册点击事件:addActionListener(ActionListener a)
3. 注册选中事件:addItemListener(ItemListener i)
4. 获取事件源:e.getSource()
和e.getItem()
四、布局管理器
- 流式布局管理器
FlowLayout
(组件大小不随容器大小改变)
1.new FlowLayout(FlowLayout.RIGHT,20,40)
2.new FlowLayout(FlowLayout.LEFT)
3.new FlowLayout()
- 边界布局管理器
BorderLayout
(JFrame
默认布局)
1. 分为五个部分North、West、Center、East、South
2. 组件大小随容器大小改变 - 网格布局管理器
GridLayout
1. 指定横纵:new GridLayout(3,3)
2. 组件大小随容器大小改变 - 卡片布局管理器
CardLayout
1. 第一张:first()
2. 下一张:next()
3. 前一张:previous()
4. 最后一张:last()
5. 指定:show(Container parent,String name)
- 盒式布局管理器
BoxLayout
- 空布局,通过
setBounds(int x,int y,int width,int height)
控制布局大小
五、事件处理(重要)
- 事件处理模型(授权处理模式)
- 事件的种类
事件类型 | 组件 | 接口名称 | 适配器名称 | 方法及说明 |
---|---|---|---|---|
ActionEvent | JButton 、JCheckBox、JComboBox、JMenuItem、JRadioButton | ActionListener | actionPerformed(ActionEvent e)点击时或回车时 | |
AdjustmentEvent | JScrollBar | AdjustmentListener | adjustmentValueChanged(AdjustmentEvent e)改变滑块位置 | |
ComponentEvent | JComponent及其子类 | ComponentListener | ComponentAdapter | componentMoved(ComponentEvent e)组件移动时 componentHidden(ComponentEvent e)组件隐藏时 componentResized(ComponentEvent e)组件缩放时componentShown(ComponentEvent e)组件移动时 |
ContainEvent | JContainer及其子类 | ComtainerListener | ComtainerAdapter | componentAdded(ContainEvent e)添加组件时 componentRemoved(ContainEvent e)移除组件时 |
ItemEvent | JCheckBox、JCheckboxMenuItem、JComboBox | ItemListener | itemStateChanged(ItemEvent e) | |
KeyEvent | JComponent及其子类 | KeyListener | KeyAdapter | keyPressed(KeyEvent e)键按下时 keyReleased(KeyEvent e)键释放时 keyTyped(KeyEvent e)击键时 |
MouseButtonEvent | JComponent及其子类 | MouseListener | MouseAdapter | mousePressed(MouseEvent e)鼠标键按下时 mouseReleased(MouseEvent e)鼠标键释放时 mouseEntered(MouseEvent e)鼠标键进入时 mouseExited(MouseEvent e)鼠标键离开时 mouseClicked(MouseEvent e)鼠标键点击时 |
MouseMotionEvent | JComponent及其子类 | MouseMotionListener | MouseMotionAdapter | mouseDragged(MouseEvent e) 鼠标拖放时 mouseMoved(MouseEvent e) 鼠标移动时 |
TextEvent | JTextField、JTextArea | TextListener | textValueChanged(TextEvent e)内容修改时 | |
WindowEvent | JFrame、JWindow、JDialog | WindowListener | WindowAdapter | windowClosing(WindowEvent e)窗口关闭时 windowOpened(WindowEvent e)窗口打开后 windowIconified(WindowEvent e)窗口最小化时 windowDeiconified(WindowEvent e)窗口还原时 windowClosed(WindowEvent e)窗口关闭后 windowActivated(WindowEvent e)窗口激活时 windowDeactivated(WindowEvent e) 窗口失焦时 |
六、绘图基础(会考)
- 颜色:
java.awt.Color
- 字体:
Font f = new Font("字体","风格","大小")
Graphics
的基本功能(相当于笔)
1. 重写Jcomponent
的paintComponent()
获取Graphics
Graphics2D
绘图
1.java.awt.geom
图形类
2.Graphics2D g2d = (Graphics2D)g;
g是从容器上获取的画笔
3.g2d.draw(对象)
绘画
七、程序实例
package com.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestSwingAndAwt extends JFrame {
JButton button;//按钮
JTextField textField;//文本框
TestSwingAndAwt(){
//创建组件
button = new JButton("输出");
button.addActionListener(new ButtonActionListener());//注册事件
textField = new JTextField(20);
FlowLayout flowLayout = new FlowLayout();
Container current = getContentPane();//获取容器
current.setLayout(flowLayout);//设置布局
current.add(textField);
current.add(button);
}
class ButtonActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
//事件处理
String content = textField.getText();
System.out.println(content);
}
}
public static void main(String[] args) {
TestSwingAndAwt test = new TestSwingAndAwt();
test.setSize(400,200);//设置宽高
test.setVisible(true);//设置可见
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭
}
}
注意:单选题、简答题、程序填空题、写程序题(20-35分左右)