GUI编程
简介
Swing,AWT,MVC架构与监听。
AWT
元素:窗口,按钮,文本框...
java.awt
Component(组件):
Button、TextArea、Label... (存在于容器中)
Container(容器): Window、Panel(面板)
Window: Frame、Dialog
Panel: Applet
Frame
// 构造器
Frame frame = new Frame("My First GUI Window");
// 设置可见性
frame.setVisible(true);
// 窗口大小
frame.setSize(400,400);
// 背景颜色
frame.setBackground(new Color(65,165,134));
frame.setBackground(Color.BLACK);
//弹出的初始位置
frame.setLocation(200,200);
// 设置大小固定,不可改变
frame.setResizable(false);
通常我们将Frame进行封装MyFrame
Panel
往Frame增加Panel -- Frame.add(Panel)
Panel也有对应的操作,如setBackground,setLocation,setColor...
无法单独显示,必须加到某个容器中
布局管理器
- 流式布局
frame.setLayout(new FlowLayout()); // 默认居中
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
- 东西南北中
Button north = new Button("North");
Button south = new Button("South");
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
- 表格布局
自动填充,与东西南北中不同。
Button button0 = new Button("button0");
Button button1 = new Button("button1");
frame.add(button0);
frame.add(button1);
TextField
将用户输入的文本输出,回车触发事件,一个监听器实现
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField1 = (TextField) e.getSource(); // 获得资源,返回对象
System.out.println(textField1.getText());
textField1.setText("");
}
}
密码隐藏
textField.setEchoChar('*');
Paint
只需在extends Frame后重写一个paint()方法
class MyPaint extends Frame{
@Override
public void paint(Graphics graphics){
// Generation
}
}
事件监听
- 写一个ActionListener类
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
System.out.println("Listener");
}
}
注:用e.getActionCommand().equals(“Start”)来实现多个案例共享一个事件
鼠标监听
一个例子:在窗口上点击,便画出一个小点。
package ch0;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
ArrayList<Point> points;
public MyFrame(){
this.setBounds(400,400,400,400);
points = new ArrayList<>();
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame)e.getSource();
Point point = new Point(e.getX(),e.getY());
points.add(point);
myFrame.repaint();
}
});
this.setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point)iterator.next();
g.setColor(Color.RED);
g.fillOval(point.x,point.y,10,10);
}
}
}
窗口监听
常见窗口监听
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
鼠标监听
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
}
});
Swing
JFrame
独特的窗口关闭
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
一个JFrame程序
class MyFrame extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(200,200,400,400);
JLabel jLabel = new JLabel("Welcome");
this.add(jLabel);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.GREEN);
//JLabal的居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JLabel
new JLabel("***");
图标和图片
public class ImageIconDemo extends JFrame{
public ImageIconDemo(){
// 标签--> url --> url创建一个图标--> 标签设置图标
JLabel jLabel = new JLabel("BiliBili");
URL url = ImageIconDemo.class.getResource("tx.jpg"); // 获取相对路径下的图片url
ImageIcon imageIcon = new ImageIcon(url); // 根据url创建一个图标
jLabel.setIcon(imageIcon); // 标签上设置图标
jLabel.setHorizontalAlignment(SwingConstants.CENTER); // 设置标签的位置
Container container = this.getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,400,400);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
JPanel
JPanel …
JScrollPane
public class JTextAreaDemo extends JFrame {
public JTextAreaDemo(){
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("Welcome to Java");
//container.add(jTextArea);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new JTextAreaDemo();
}
}
JButton
图片按钮
public class JButtonDemo extends JFrame{
public static void main(String[] args) {
new JButtonDemo();
}
public JButtonDemo(){
Container container = this.getContentPane();
URL url = JButtonDemo.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("BiliBili");
container.add(jButton);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选框
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.SOUTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.NORTH);
复选框
JCheckBox jCheckBox1 = new JCheckBox("1");
JCheckBox jCheckBox2 = new JCheckBox("0");
container.add(jCheckBox1,BorderLayout.SOUTH);
container.add(jCheckBox2,BorderLayout.WEST);
列表
下拉框
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("江苏");
jComboBox.addItem("湖北");
jComboBox.addItem("浙江");
container.add(jComboBox);
列表(动态扩容,用于展示)
public ComboboxTest(){
Container container = this.getContentPane();
//String[] contents = {"1","2","3"};
Vector contents = new Vector();
contents.add("江苏");
contents.add("湖北");
contents.add("浙江");
JList jList =new JList(contents);
container.add(jList);
}
文本框,密码框
文本框 JTextArea
密码框JPassword
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);