简介

  • AWT(译:抽象窗口工具包),是Java的平台独立的窗口系统,图形和用户界面器件工具包。
  • Swing 是为了解决 AWT 存在的问题而以 AWT 为基础新开发的包(在使用Swing时也常会用到java.awt.*包)。

JFrame

JFrame容器允许程序员把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。我们可以直接new一个JFrame对象,也可以自己实现一个类继承它(常用)。

常用方法

  • 设置窗口可见:setVisible(true);//默认为false
  • 设置窗口标题;setTitle(String title)
  • 设置点击窗口的×后执行的操作:setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

各参数定义:

EXIT_ON_CLOSE 隐藏窗口,并退出程序
DO_NOTHING_ON_CLOSE 无任何操作
HIDE_ON_CLOSE 隐藏窗体,但不停止程序
DISPOSE_ON_CLOSE 释放窗体资源

 

  • 设置窗体的大小(单位:像素):setSize(int width, int height);

 

 

  • 设置窗体坐标(单位:像素):setLocation(int x, int y);

 

  • 坐标大小一起设置:setBounds(int x, int y, int width, int height);
  • 获取窗口坐标:横坐标:getX();纵坐标:getY()
  • 获取窗口大小:宽:getWidth();高:getHeight()
  • 获取窗口容器:Container contentPane = getContentPane();

设置背景:contentPane.setBackground(Color.GREEN)

添加组件:contentPane.add(Component comp)

验证容器中的组件(相当于刷新):contentPane.validate()

重新载入容器可实现刷新效果:setContentPane(contentPane) //一般使用validate()

设置布局:contentPane.setLayout(LayoutManager mgr);

 常见组件

按钮:JButton

复选框组件:JCheckbox 

下拉列表框:JCombox 

标签组件:JLabel 

单选按钮:JRadioButton

框列表框:JList

文本框:JTextField 

密码框:JPasswordField

文本域:JTextArea

对话框:JOptionPane

代码示例

JDialog对话框

 1 package com.czm;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 
 8 /**
 9  * 对话框
10  */
11 public class Demo02 extends JDialog {
12 
13     public Demo02(JFrame jFrame) {
14         /**
15          * 父窗体对象,对话框标题, 是否阻塞父窗体(只能弹出一个对话框,弹出后不能点击父窗体)
16          */
17         super(jFrame, "对话框标题", true);
18         Container contentPane = getContentPane();
19         contentPane.add(new JLabel("这是一个对话框"));
20         setBounds(300, 300, 300, 300);
21     }
22 
23     public static void main(String[] args) {
24         JFrame jFrame = new JFrame("父窗体");
25         jFrame.setBounds(200,200,500,500);
26         Container contentPane = jFrame.getContentPane();
27         JButton button = new JButton("弹出对话框");
28         //设置布局,使用流布局
29         contentPane.setLayout(new FlowLayout());
30         contentPane.add(button);
31         jFrame.setVisible(true);
32         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33 
34 //        button.addActionListener(new ActionListener() {
35 //            @Override
36 //            public void actionPerformed(ActionEvent e) {
37 //                Demo02 demo02 = new Demo02(jFrame);
38 //                //设置对话框可见
39 //                demo02.setVisible(true);
40 //            }
41 //        });
42 
43         //Lambda 表达式
44         button.addActionListener((e)->{
45             Demo02 demo02 = new Demo02(jFrame);
46                 //设置对话框可见
47                 demo02.setVisible(true);
48         });
49 
50     }
51 }
View Code

相关文章:

  • 2021-08-15
  • 2021-11-27
  • 2021-10-29
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2021-07-17
  • 2021-06-05
相关资源
相似解决方案