GUI
GUI简介
GUI:Graphical User Interface,图形用户接口,用来提供给用户操作的图形界面的接口,通过这些提供的图形用户接口就可以实现包括窗口、菜单、按钮、工具栏和其他各种图形界面元素,从而实现程序的图形用户界面来方便用户操作
GUI核心技术:Swing AWT JavaFX
不流行原因:
- 界面不美观
- 需要 jre 环境
为什么要学习:
- 可以写一些小工具
- 工作中,可能需要维护 Swing 界面,概率极小
- 了解MVC架构,了解监听
组件:
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
AWT
AWT介绍
组件和容器
窗口 Frame
Frame:是一个顶级窗口
Frame frame = new Frame("Java界面");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(200,200);
//设置背景颜色
frame.setBackground(Color.BLUE);
//设置窗口初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
将上述代码封装:
public class Demo02 {
public static void main(String[] args) {
new MyFrame(100,100,200,200,Color.black);
new MyFrame(300,100,200,200,Color.pink);
new MyFrame(100,300,200,200,Color.red);
new MyFrame(300,300,200,200,Color.yellow);
}
}
class MyFrame extends Frame {
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setVisible(true);
setBackground(color);
}
}
面板 Panel
Panel:无法单独显示,必须添加到某个容器中
解决了关闭事件
public static void main(String[] args) {
Frame frame = new Frame();
//Panel 可以看成是一个空间,但不能单独存在
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//frame坐标
frame.setBounds(100,100,300,300);
frame.setBackground(new Color(237, 207, 108));
//panel坐标,相对于frame
panel.setBounds(50,50,100,100);
panel.setBackground(new Color(88, 75, 230));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时要做的事
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
布局管理器
- 流式布局
Frame frame = new Frame();
//按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
- 东西南北中
Frame frame = new Frame();
Button button1 = new Button("East");
Button button2 = new Button("West");
Button button3 = new Button("South");
Button button4 = new Button("North");
Button button5 = new Button("Center");
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
- 表格布局(Grid)
Frame frame = new Frame();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setVisible(true);
frame.pack();
事件监听
事件监听:当某个个事情发生时,会做出什么事情
public class Demo07 {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
closeWindows(frame);
frame.setVisible(true);
}
//关闭窗口事件
private static void closeWindows(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按啥呢");
}
}
多个按钮共享一个事件
public class Demo08 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("end");
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
button1.setActionCommand("点什么");
MyListener myListener = new MyListener();
button1.addActionListener(myListener);
button2.addActionListener(myListener);
frame.pack();
frame.setVisible(true);
}
}
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮的信息
System.out.println("按钮被点击了:"+e.getActionCommand());
}
}
输入框 TextField
public class Demo09 {
public static void main(String[] args) {
new Myframe();
}
}
class Myframe extends Frame{
public Myframe(){
//创建输入框
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyActonListener1 myActonListener1 = new MyActonListener1();
//按下 enter 就会触发输入框的事件
textField.addActionListener(myActonListener1);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActonListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getSource() 获得一些资源,返回一个对象
TextField field = (TextField) e.getSource();
//field.getText() 获得输入框的文本
System.out.println(field.getText());
field.setText(" ");
}
}
画笔 paint
画笔用完,要将其还原为最初的颜色
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,500,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
//super.paint(g);
//画笔颜色
g.setColor(Color.red);
//g.drawOval(100,100,100,100); 空心圆
g.fillOval(100,100,100,100); //实心圆
}
}
鼠标监听
class Myframe extends Frame{
ArrayList points;
//1.画画需要画笔,需要监听鼠标当前的位置,需要集合来存储点
public Myframe(String title){
super(title);
setBounds(200,200,400,300);
//5.存储鼠标点击的点
points = new ArrayList();
//3.鼠标监听器,针对于窗口而言
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//2.画画,需要监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.red);
g.fillOval(point.x,point.y,10,10);
}
}
//添加点到界面上
public void addPoint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
Myframe myframe = (Myframe) e.getSource();
//4.鼠标点击的时候就会在界面上产生一个点
//鼠标的点坐标
myframe.addPoint(new Point(e.getX(), e.getY()));
//每次点击鼠标都需要重新画一遍
myframe.repaint();
}
}
}
窗口监听
class WindowFrame extends Frame{
public WindowFrame() {
setBounds(100,100,300,300);
setBackground(Color.red);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
//正在关闭中
public void windowClosing(WindowEvent e) {
System.out.println("你点击了X");
setVisible(false);//隐藏窗口,程序并未退出
System.exit(0);//正常退出
}
@Override
//已经关闭
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
@Override
//激活
public void windowActivated(WindowEvent e) {
WindowFrame frame = (WindowFrame) e.getSource();
frame.setTitle("被激活了");
System.out.println("窗口被激活");
}
}
);
}
/* //内部类
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,程序并未退出
System.exit(0);//正常退出
}
}
*/
}
键盘监听
class KeyFream extends Frame{
public KeyFream() {
setBounds(100,200,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按了上键");
}
}
});
}
}
Swing
窗口 JFrame
//init():初始化
public void init(){
JFrame jFrame = new JFrame("JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,200,300,400);
//设置文字 Jlable
JLabel jLabel = new JLabel("好好学习天天向上!");
jFrame.add(jLabel);
//文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(Color.red);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
弹窗 JDialog
JDialog:用来被弹出,默认就有关闭事件
public class Demo06 extends JFrame {
public Demo06() {
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jButton = new JButton("弹出一个框");
jButton.setBounds(30,30,200,200);
//点击按钮的时候,弹出一个弹窗
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
}
);
container.add(jButton);
}
public static void main(String[] args) {
new Demo06();
}
}
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,100,100);
}
}
标签 JLable
标签 JLable:
new JLable("XXX");
图标 Icon:
//图标,是一个接口,需要实现类,JFrame继承
public class Demo07 extends JFrame implements Icon {
private int width;
private int height;
public Demo07() {} //无参构造
public Demo07(int width,int height) { //有参构造
this.width = width;
this.height = height;
}
public void init(){
Demo07 demo07 = new Demo07(100,100);
//图标放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("图标", demo07, SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo07().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片 ImageIcon:
public class Demo08 extends JFrame {
public Demo08() {
//获取图片的地址
URL url = Demo08.class.getResource("JAVA.png");
JLabel jLabel = new JLabel();
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo08();
}
}
面板 JPanel
面板 JPanel:
Container container = this.getContentPane();
JPanel jPanel = new JPanel(new GridLayout(1,3,20,20));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("2"));
jPanel.add(new JButton("3"));
container.add(jPanel);
this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
滚动条 JScrollPanel:
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 30);
jTextArea.setText("你好");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setSize(200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
按钮 JButton
图片按钮:
public class Demo10 extends JFrame {
public static void main(String[] args) {
new Demo10();
}
public Demo10() {
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = Demo10.class.getResource("JAVA.png");
ImageIcon imageIcon = new ImageIcon(url);
//将图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
//提示
jButton.setToolTipText("图片按钮");
container.add(jButton);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选按钮 JRadioButton:分组
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = Demo10.class.getResource("JAVA.png");
ImageIcon imageIcon = new ImageIcon(url);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton01");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton02");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
复选按钮 JCheckBox:
Container container = this.getContentPane();
//将一个图片变为一个图标
URL url = Demo10.class.getResource("JAVA.png");
ImageIcon imageIcon = new ImageIcon(url);
//多选框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.CENTER);
container.add(jCheckBox3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
列表
下拉框 JComboBox:
Container container = this.getContentPane();
//下拉框
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("null");
jComboBox.addItem("热映中");
jComboBox.addItem("已下架");
jComboBox.addItem("未上映");
container.add(jComboBox);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
列表框 JList:
Container container = this.getContentPane();
//列表框,生成列表的内容
//String[] contents = {"1","2","3"};
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("123");
contents.add("456");
contents.add("789");
container.add(jList);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
文本框
文本框 JTextField:
Container container = this.getContentPane();
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField("Hello",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
密码框 JPasswordField:
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
//passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
文本域
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 30);
jTextArea.setText("你好");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setSize(200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

浙公网安备 33010602011771号