1. 简介

swing与awt:可以认为awt是swing的前身,awt即Abstrace Window Toolkit抽象窗口工具包,swing是为了解决awt在开发中的问题而开发的,是awt的改良。

因此,在功能上swing比awt更加全面,而使用方法并未太大的差别,swing中的类名称通常在awt的类名称前添加字母J,如Frame-->JFrame。

本文对swing中以下部分进行解释:

窗口/面板、弹窗、标签、面板、按钮、列表、文本(框/域,密码框)

2. 窗口、面板

建立

JFrame、JPanel,在构建窗口中一般使用init()方法进行定义,init()只是一个普通方法,但是使用者比较广泛,就如同初学者多使用helloWorld()方法一样

此外,在swing中需要添加Container对象用以添加组件或相关设置

swing中提供了默认的关闭事件(开发者也可以自己编写监听器)

public class JFrameDemo{
public static void main(String[] args){
new JFrameDemo.init();
//静态方法不能调用非静态方法--创建对象生成窗口
}
public void init(){
JFrame jframe = new JFrame("titleForJFrame");
Container container = jframe.getContentPane();//获取容器对象
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jframe.setBounds(200,200,200,200);
jframe.setVisible(true);
Button button = new Button("button");
container.add(button);
}
}

除上述外还可通过继承JFrame类来实现

public class JFrameDemo{
public static void main(String[])args{
new MyFrame();
}
}
class MyJFrame extends JFrame{
public MyJFrame(){
setBounds(200,200,200,200);
setVisible(true);
Container container = getContentPane();
Button button = new Button("button");
container.add(button);
}
}

布局

除直接对容器的布局管理之外还可在组件上设置

JLabel jLabel = new JLabel("label");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);

3. 弹窗

JDialog,弹窗

与其他窗口一样是在事件发生时显示出来的新窗口,JFrame视为主窗口的话JDialog可视为副窗口

默认具有关闭事件,不用单独写

public class DialogDemo{
public static void main(String[] args){
new MainJFrame().init();
}
}
class MainJFrame extends JFrame {
public void init(){
setBounds(200,200,200,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(null);
JButton jButton = new JButton("button");
jButton.setBounds(20,20,150,150);
jButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new MyDialog();
}
});
container.add(jButton);
}
}
class MyDialog extends JDialog{
public MyDialog(){
setBounds(300,300,100,100);
JLabel jLabel = new JLabel("label");
setVisible(true);
}
}

4. 标签

JLabel,主体为一串文字,可加上图标,即Icon、ImageIcon

标签:

public class JLabelDemo {
public static void main(String[] args) {
new MyJFrameDemo();
}
}
class MyJFrameDemo extends JFrame{
public MyJFrameDemo(){
setBounds(200,200,200,200);
setVisible(true);
JLabel jLabel = new JLabel("this is a label");
jLabel.setBackground(Color.BLUE);
Container container = getContentPane();
container.add(jLabel);
container.setBackground(Color.CYAN);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

获取、添加标签

图形图标

public class Demo {
public static void main(String[] args) {
new MyFrame01().init();
}
} class MyIcon implements Icon {
private int width, height; public MyIcon() {
} public MyIcon(int width, int height) {
this.height = height;
this.width = width;
} @Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
} @Override
public int getIconWidth() {
return width;
} @Override
public int getIconHeight() {
return height;
}
} class MyFrame01 extends JFrame {
public void init() {
setBounds(200, 200, 200, 200);
setVisible(true);
Container container = getContentPane();
JLabel jLabel = new JLabel("this ia a label", new MyIcon(15,15), SwingConstants.CENTER);
container.add(jLabel);
}
}

图片图标

public class Demo {
public static void main(String[] args) {
new MyJFrameDemo();
}
}
class MyJFrameDemo extends JFrame {
public MyJFrameDemo(){
URL url = MyJFrameDemo.class.getResource("wx.jpg");
//获取图片地址
ImageIcon imageIcon = new ImageIcon(url);
//将图片地址加载至图片图标
setBounds(200,200,200,200);
setVisible(true);
JLabel jLabel = new JLabel("this is a label");
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.WEST);
//以上上句等价于:JLabel jLabel = new JLabel("this is a label",imageIcon,SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
container.setBackground(Color.CYAN);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

5. 面板

实现原理与awt基本一致,分为普通面板和滚动面板

基本面板

JPanel

public class Demo {
public static void main(String[] args) {
new JPanelDemo01().init();
}
}
class JPanelDemo extends JFrame{
public void init(){
setBounds(200,200,200,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
JPanel jPanel = new JPanel();
JButton jButton1 = new JButton("1");
JButton jButton2 = new JButton("2");
JButton jButton3 = new JButton("3");
JButton jButton4 = new JButton("4");
JButton jButton5 = new JButton("5");
JButton jButton6 = new JButton("6");
jPanel.setLayout(new GridLayout(2,3,10,10));
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(jButton4);
jPanel.add(jButton5);
jPanel.add(jButton6);
container.add(jPanel);
}
}

滚动面板

JScrollPane

当文本超过窗口大小是会有滚动条

public class Demo {
public static void main(String[] args) {
new MyFrameDemo().init();
}
} class MyFrameDemo extends JFrame {
public void init() {
Container container = getContentPane();
setVisible(true);
setBounds(200, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea jTextArea = new JTextArea("welcome to JScrollPane", 20, 50);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
}
}

 6. 按钮

普通按钮,图片按钮,单选按钮,多选按钮

普通按钮 JButton

public class Demo {
public static void main(String[] args) {
new JButtonDemo01().init();
}
}
class JButtonDemo01 extends JFrame{
public void init(){
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
JButton jButton1 = new JButton("button");
JButton jButton2 = new JButton("button");
JButton jButton3 = new JButton("button");
JButton jButton4 = new JButton("button");
JButton jButton5 = new JButton("button");
Container container = getContentPane();
container.add(jButton1,BorderLayout.WEST);
container.add(jButton2,BorderLayout.SOUTH);
container.add(jButton3,BorderLayout.NORTH);
container.add(jButton4,BorderLayout.CENTER);
container.add(jButton5,BorderLayout.EAST);
}
}

图片按钮

public class ButtonDemo extends JFrame {
public ButtonDemo(){
//get container
Container container = getContentPane();
URL url = ButtonDemo.class.getResource("wx.jpg");
Icon icon = new ImageIcon(url);
//put this icon on the button
JButton jButton = new JButton();
jButton.setIcon(icon);
//setup the tip text (display when mouse stay on it)
jButton.setToolTipText("this is a pic-button");
container.add(jButton);
setBounds(200,200,200,200);
setBackground(Color.GREEN);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonDemo();
} }

单选按钮 

public class Demo extends JFrame {
public Demo(){
Container container = getContentPane();
URL url = Demo.class.getResource("wx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
//single choice outline
//JRadioButton的声明和添加偶尔会出现一些意外,原因不明
JRadioButton jRadioButton1 = new JRadioButton("choice1");
JRadioButton jRadioButton2 = new JRadioButton("choice2");
JRadioButton jRadioButton3 = new JRadioButton("choice3");
//由于单选框只能选择一个,所以我们通常将其分组,就是一个组只能选一个,如果不分组的话三个都可以被选中,即形式上变成了多选框
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
//add button
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH); setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new SingleButtonDemo();
}
}

多选按钮

JCheckBox

public class JCheckBoxDemo extends JFrame {
public JCheckBoxDemo(){
JCheckBox jCheckBox1 = new JCheckBox("choice01");
JCheckBox jCheckBox2 = new JCheckBox("choice02");
JCheckBox jCheckBox3 = new JCheckBox("choice03");
//added into container
container.add(jCheckBox1,BorderLayout.SOUTH);
container.add(jCheckBox2,0);
container.add(jCheckBox3,"North");
//按钮位置设定的三种写法
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new JCheckBoxDemo();
}
}

 6. 列表

下拉框 JComBox

public class Demo {
public static void main(String[] args) {
new JComBox().init();
}
}
class JComBox extends JFrame{
public void init(){
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
Container container = getContentPane();
container.setLayout(null);
JComboBox jComboBox = new JComboBox();
jComboBox.setToolTipText("this is a ComBox");
jComboBox.addItem("statement01");
jComboBox.addItem("statement02");
jComboBox.addItem("statement03");
jComboBox.addItem("statement04");
jComboBox.setLocation(10,10);
jComboBox.setSize(150,30);
container.add(jComboBox);
}
}

列表框 JList

public class Demo {
public static void main(String[] args) {
new JListDemo().init();
}
}
class JListDemo extends JFrame {
public void init() {
setVisible(true);
setBounds(200, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setBackground(Color.BLACK);
container.setLayout(null);
setResizable(false);
String[] str = {"statement01", "statement02", "statement03", "statement04"};
JList jList = new JList(str);
jList.setBackground(Color.CYAN);
jList.setSize(150,80);
jList.setLocation(20,20);
container.add(jList);
}
}

7. 文本

文本框 JTextField

public class Demo {
public static void main(String[] args) {
new JTextFieldDemo().init();
}
}
class JTextFieldDemo extends JFrame{
public void init(){
setBounds(200,200,200,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
Container container = getContentPane();
container.setLayout(null);
JTextField jTextField01 = new JTextField();
jTextField01.setText("hello");
jTextField01.setLocation(10,10);
jTextField01.setSize(100,20);
jTextField01.setColumns(10);
JTextField jTextField02 = new JTextField();
jTextField02.setText("world");
jTextField02.setLocation(10,100);
jTextField02.setSize(100,20);
jTextField02.setColumns(10);
container.add(jTextField01,BorderLayout.NORTH);
container.add(jTextField02,BorderLayout.SOUTH);
}
}

                                       

 文本域

TextArea

public class Demo {
public static void main(String[] args) {
new JTextAreaDemo().init();
}
} class JTextAreaDemo extends JFrame {
public void init() {
setBounds(200, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
Container container = getContentPane();
TextArea jTextArea = new TextArea(20,50);
jTextArea.setText("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld");
container.add(jTextArea);
}
}

JTextArea

public class Demo {
public static void main(String[] args) {
new JTextAreaDemo().init();
}
} class JTextAreaDemo extends JFrame {
public void init() {
setBounds(200, 200, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
Container container = getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld");
container.add(jTextArea);
}
}

 密码框 JPasswordField

public class Demo {
public static void main(String[] args) {
new JPasswordFieldDemo();
}
}
class JPasswordFieldDemo extends JFrame{
public JPasswordFieldDemo(){
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
Container container = getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.addActionListener(new MyListener000());
container.add(jPasswordField);
}
}
class MyListener000 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
System.out.println(str);
}
}

    

Java-GUI基础(三)java.swing的更多相关文章

  1. 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点

    浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...

  2. Java GUI编程中AWT/swing/SWT的优缺点

    http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...

  3. Java语言基础及java核心

    一.Java语言特点 1. 简单 2. 面向对象 3. 分布式 4. 健壮 5. 安全 6. 中性架构跨平台 7. 超强的可移植性 8. 高性能 9. 多线程 二.java的环境变量 JAVA_HOM ...

  4. Java 数组基础,java.util.Arrays

    定义数组 方式1(推荐,更能表明数组类型) 方式2(同C语言) 方式3定义时直接初始化 数组运用基础 数组长度 equals() 数组元素不为基本数据类型时 二维数组 二维数组基础 变长的二维数组 j ...

  5. java线程基础知识----java线程模型

    转载自http://www.cnblogs.com/nexiyi/p/java_memory_model_and_thread.html 1. 概述 多任务和高并发是衡量一台计算机处理器的能力重要指标 ...

  6. 【Java并发基础】Java内存模型解决有序性和可见性

    前言 解决并发编程中的可见性和有序性问题最直接的方法就是禁用CPU缓存和编译器的优化.但是,禁用这两者又会影响程序性能.于是我们要做的是按需禁用CPU缓存和编译器的优化. 如何按需禁用CPU缓存和编译 ...

  7. 【Java并发基础】Java线程的生命周期

    前言 线程是操作系统中的一个概念,支持多线程的语言都是对OS中的线程进行了封装.要学好线程,就要搞清除它的生命周期,也就是生命周期各个节点的状态转换机制.不同的开发语言对操作系统中的线程进行了不同的封 ...

  8. Java 语言基础 (初识Java语言, 变量和数据类型, 运算符, 流程控制语句, 数组)

    初始 Java 语言 Java SE -- Java Platform, Standard Edition 是 Java 平台的基础 Java SE 以前称为 J2SE, 可以编写桌面应用和基于 we ...

  9. Java 学习笔记 (三) Java 日期类型

    以下内容摘自:  https://www.cnblogs.com/crazylqy/p/4172324.html import java.sql.Timestamp; import java.text ...

  10. java线程基础知识----java daemon线程

    java线程是一个运用很广泛的重点知识,我们很有必要了解java的daemon线程. 1.首先我们必须清楚的认识到java的线程分为两类: 用户线程和daemon线程 A. 用户线程: 用户线程可以简 ...

随机推荐

  1. Python练习题 018:打印星号菱形

    [Python练习题 018] 打印出如下图案(菱形): * *** ***** ******* ***** *** * --------------------------------------- ...

  2. Salesforce Javascript(二) 箭头函数

    本篇参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions 我们在 ...

  3. 手把手撸套框架-Victory框架1.0 详解

    目录 其实Victory框架1.0 在8月份就完成了,整个9月份都没有更新博客,主要还是因为松懈了. 所以,趁着国庆节的放假的时间把博客给更新一下,1.0总的来说算不得一个成熟的产品,但是拿来开发我们 ...

  4. 【保姆级教程】手把手教你进行Go语言环境安装及相关VSCode配置

    [Go语言入门系列]前面的文章: [Go语言入门系列](七)如何使用Go的方法? [Go语言入门系列](八)Go语言是不是面向对象语言? [Go语言入门系列](九)写这些就是为了搞懂怎么用接口 本篇文 ...

  5. 07 C语言常量

    常量的定义 常量是指固定的值,固定值在程序执行期间不会改变.这些固定值,又叫做字面量. 常量可以是任意的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量. 不要搞得太复杂, ...

  6. 多测师讲解python _函数中参数__高级讲师肖sir

    函数中讲解参数: 形参和实参的认识 函数无参数的调用 函数单个参数的调用 函数多个参数的调用 # #调试函数给默认参数传新值,则函数使用新值 # 注意:当多种参数同时出现在函数中,默认参数要放在最后的 ...

  7. Mac zsh中所有命令失效

    参考文章 https://blog.csdn.net/hujincai_55/article/details/95680245?utm_medium=distribute.pc_relevant.no ...

  8. C#数据结构-链栈

    上一篇我们通过数组结构实现了栈结构(准确的说是栈的顺序存储结构),现在我们通过链(单链)存储栈,也就是链栈. 通常对于正向单链表来说,是从头节点开始,在链的尾部附加节点,前一个节点的指针指向附加节点: ...

  9. day37 Pyhton 网络编程04

    # tcp协议和udp协议的选择问题 # tcp # 大量的连续的数据 传递文件\发送邮件 # 文件的传递 # 下载电影 # udp # 短消息类 社交软件 # qq 微信 # 在线播放视频 快会丢帧 ...

  10. 单调队列+线性dp题Watching Fireworks is Fun (CF372C)

    一.Watching Fireworks is Fun(紫题) 题目:一个城镇有n个区域,从左到右1编号为n,每个区域之间距离1个单位距离节日中有m个烟火要放,给定放的地点ai,时间ti当时你在x,那 ...