图形化界面
AWT
Component 组件基类
Container继承体系 容器
组件,容器,布局
设置组件
对容器根的操作
一个简单的界面
1 2 3 4 5 6 7 8 9 10
| import java.awt.*;
public class Main { public static void main(String[] args) { Frame frame = new Frame(); frame.setLocation(100, 100); frame.setSize(500,500); frame.setVisible(true); } }
|
组件和容器
Panel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.awt.*;
public class Main { public static void main(String[] args) { Frame frame = new Frame("title");
Panel p = new Panel();
p.add(new TextField("这是一个组件")); p.add(new Button("这是一个按钮组件"));
frame.add(p); frame.setBounds(200,200,500,500); frame.setVisible(true); } }
|
ScrollPane
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.awt.*;
public class Main { public static void main(String[] args) { Frame frame = new Frame("title");
ScrollPane scrollPane = new ScrollPane();
scrollPane.add(new TextField("这是一个文本")); scrollPane.add(new Button("这是一个按钮"));
frame.add(scrollPane); frame.setBounds(100,100,500,300); frame.setVisible(true); } }
|
LayoutManager 接口 布局管理器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.awt.*;
public class Main { public static void main(String[] args) { Frame frame = new Frame("title");
for (int i = 0; i < 100; i++) { frame.add(new Button()); }
frame.setBounds(100,100,500,300); frame.setVisible(true); frame.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20)); } }
|
BoderLayout布局
边框布局,没有指定区域默认center
区域不使用会被覆盖
GridLayout 网格布局
简易的计算器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.awt.*;
public class Main { public static void main(String[] args) { Frame frame = new Frame("计算器");
Panel panel = new Panel(); panel.add(new TextField(30)); frame.add(panel, BorderLayout.NORTH);
Panel panel1 = new Panel(); panel1.setLayout(new GridLayout(3,5,4,4)); for (int i = 0; i < 10; i++) { panel1.add(new Button(i+"")); } panel1.add(new Button("+")); panel1.add(new Button("-") ); panel1.add(new Button("*")); panel1.add(new Button("/")); panel1.add(new Button(".")); frame.add(panel1);
frame.pack(); frame.setVisible(true);
} }
|
GridBagLayout
CardLayout 卡片布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Main { public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); CardLayout cardLayout = new CardLayout(); panel.setLayout(cardLayout); String[] name = new String[]{"第一张", "第二张", "第三张", "第四张","第五张" }; for (int i = 0; i < 5; i++) { panel.add(name[i] ,new Button("第"+(i+1)+"张")); } frame.add(panel, BorderLayout.CENTER);
Panel panel1 = new Panel(); Button b1 = new Button("上一张"); Button b2 = new Button("下一张"); Button b3 = new Button("第一张"); Button b4 = new Button("最后一张"); Button b5 = new Button("第三张");
panel1.add(b1); panel1.add(b2); panel1.add(b3); panel1.add(b4); panel1.add(b5);
ActionListener actionListener = new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String action = e.getActionCommand();
switch (action){ case "上一张": cardLayout.previous(panel); break; case "下一张": cardLayout.next(panel); break; case "第一张": cardLayout.first(panel); break; case "最后一张": cardLayout.last(panel); break; case "第三张": cardLayout.show(panel, "第三张"); break;
} } };
b1.addActionListener(actionListener); b2.addActionListener(actionListener); b3.addActionListener(actionListener); b4.addActionListener(actionListener); b5.addActionListener(actionListener); frame.add(panel1,BorderLayout.SOUTH);
frame.pack(); frame.setVisible(true); } }
|
BoxLayout 布局
Box 变量 = Box.createVerticalBox();
直接添加一个水平容器
间距
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Main { public static void main(String[] args) { Frame frame = new Frame();
Box box1 = Box.createHorizontalBox(); box1.add(new Button("水平1")); box1.add(Box.createHorizontalGlue()); box1.add(new Button("水平2")); box1.add(Box.createHorizontalStrut(30)); box1.add(new Button("水平3"));
Box box2 = Box.createVerticalBox(); box2.add(new Button("垂直1")); box2.add(Box.createVerticalGlue()); box2.add(new Button("垂直2")); box2.add(Box.createVerticalStrut(30)); box2.add(new Button("垂直3"));
frame.add(box1, BorderLayout.NORTH); frame.add(box2);
frame.pack(); frame.setVisible(true); } }
|
基本组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Main {
TextField textField = new TextField(40); Button b1 = new Button("确认");
List coloer = new List(4, true);
TextArea textArea = new TextArea(10, 30); Choice choice = new Choice();
CheckboxGroup checkboxGroup = new CheckboxGroup(); Checkbox checkbox = new Checkbox("男", checkboxGroup, true ); Checkbox checkbox1 = new Checkbox("女", checkboxGroup, true ); Checkbox checkbox2 = new Checkbox("是否已婚");
public void init(){ Frame frame = new Frame();
Box box3 = Box.createHorizontalBox(); box3.add(textField); box3.add(b1); frame.add(box3, BorderLayout.SOUTH);
Box box1_2 = Box.createHorizontalBox(); Box box1 = Box.createVerticalBox(); box1.add(textArea); Box box1_south = Box.createHorizontalBox(); box1_south.add(Box.createHorizontalStrut(20)); choice.add("红色"); choice.add("绿色"); choice.add("黑色"); box1_south.add(choice); box1_south.add(checkbox); box1_south.add(checkbox1); box1_south.add(checkbox2); box1.add(box1_south);
box1_2.add(box1); coloer.add("红色"); coloer.add("黑色"); box1_2.add(coloer); frame.add(box1_2); frame.pack(); frame.setVisible(true); }
public static void main(String[] args) { Main main = new Main(); main.init(); } }
|
DIalog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Main { Frame frame = new Frame();
Button button1 = new Button("打开文件"); Button button2 = new Button("保存文件");
FileDialog fileDialog1 = new FileDialog(frame, "打开文件" ,FileDialog.LOAD); FileDialog fileDialog2 = new FileDialog(frame, "关闭文件", FileDialog.SAVE);
public void init(){ frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH);
frame.pack(); frame.setVisible(true);
button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fileDialog1.setVisible(true); System.out.println("文件路径" + fileDialog1.getDirectory()); System.out.println("文件名"+ fileDialog1.getFile()); } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fileDialog2.setVisible(true); System.out.println("文件路径" + fileDialog2.getDirectory()); System.out.println("文件名"+ fileDialog2.getFile()); } }); }
public static void main(String[] args) { Main main = new Main(); main.init(); } }
|
事件处理
关系:
(外部动作)事件源、事件、事件监听器
流程
事件
事件监听器
菜单组件
继承关系:
绘图
概念
Graphics对象