JavaSE基础——GUI编程(Swing)
Swing
1、简介
Swing并没有完全取代AWT,而是基于AWT的架构之上。Swing仅仅是提供了能力更强大的用户界面组件。在使用Swing编写的程序中,还需要使用基本的AWT处理事件。从现在开始,Swing是“被绘制的”用户界面 ;AWT是指像事件处理这样的窗口工具箱的底层机制。
2、窗口和面板
import javax.swing.*;
import java.awt.*;public class JFrameDemo {//初始化窗口public static void init() {JFrame jFrame = new JFrame("Jframe窗口");jFrame.setBounds(100,100,500,500);jFrame.setBackground(Color.white);jFrame.setResizable(true);jFrame.setVisible(true);jFrame.setLayout(new FlowLayout());JLabel jLabel = new JLabel("Jframe 文字标签");jFrame.add(jLabel,BorderLayout.CENTER);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {init();}
}
执行结果:
2、弹窗
弹出的窗口,默认有关闭事件
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;/*** 主窗口*/
public class DialogDemo extends JFrame{public DialogDemo() {//设置窗口属性this.setBounds(100,100,500,500);this.setBackground(Color.BLACK);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//用容器装组件Container container = this.getContentPane();container.setLayout(new FlowLayout());Button button = new Button("button");button.setBounds(100,100,200,100);container.add(button);//给按钮添加事件监听button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new MyDialog();}});}public static void main(String[] args) {new DialogDemo();}
}/*** 弹出的窗口*/
class MyDialog extends JDialog{public MyDialog() {this.setBounds(100,100,200,200);this.setVisible(true);this.setResizable(false);this.setBackground(Color.green);//用一个容器来装标签Container container = this.getContentPane();container.setLayout(new FlowLayout());container.add(new JLabel("Jlabel 标签"));}
}
3、标签和图标
绘出自己的图标
import javax.swing.*;
import java.awt.*;/*** Icon图标,继承Jframe的同时实现Icon接口*/
public class IconlDemo extends JFrame implements Icon {private int width;private int height;public IconlDemo() {}public IconlDemo(int width, int height) throws HeadlessException {this.width = width;this.height = height;}//初始化图标public void init() {IconlDemo iconlDemo = new IconlDemo(15,15);//把图片放在标签上JLabel jLabel = new JLabel("IconTest",iconlDemo,SwingConstants.CENTER);Container container = this.getContentPane();container.add(jLabel);this.pack();this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}//画图标@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillRect(x,y,width,height);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}//测试public static void main(String[] args) {new IconlDemo().init();}
}
把图片添加到标签上
import javax.swing.*;
import java.awt.*;
import java.net.URL;public class ImageIconDemo extends JFrame {public ImageIconDemo() {JLabel jLabel = new JLabel("ImageIcon");//获取图片的地址URL url = ImageIconDemo.class.getResource("/com/westos/demo5/Irving.jpg");ImageIcon imageIcon = new ImageIcon(url);jLabel.setIcon(imageIcon);Container container = this.getContentPane();container.add(jLabel);this.setBounds(100,100,600,500);this.setBackground(Color.YELLOW);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}/*** 测试图片标签* @param args*/public static void main(String[] args) {new ImageIconDemo();}
}
4.1、面板Jpanel
import javax.swing.*;
import java.awt.*;public class JPanelDemo extends JFrame {public JPanelDemo() {Container container = this.getContentPane();container.setBackground(Color.YELLOW);container.setLayout(new GridLayout(2,1,5,5));//创建面板对象JPanel jPanel1 = new JPanel(new GridLayout(2, 3));JPanel jPanel2 = new JPanel(new GridLayout(2, 2));jPanel1.add(new Button("1"));jPanel1.add(new Button("1"));jPanel1.add(new Button("1"));jPanel1.add(new Button("1"));jPanel1.add(new Button("1"));jPanel1.add(new Button("1"));jPanel2.add(new Button("2"));jPanel2.add(new Button("2"));jPanel2.add(new Button("2"));jPanel2.add(new Button("2"));container.add(jPanel1);container.add(jPanel2);this.setBounds(100,100,500,500);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}/*** 测试* @param args*/public static void main(String[] args) {new JPanelDemo();}
}
执行结果:
4.2、滚动面板JScrollPane
import javax.swing.*;
import java.awt.*;/*** 滚动窗口*/
public class JScrollPanelDemo extends JFrame {public JScrollPanelDemo() {Container container = this.getContentPane();//文本域TextArea textArea = new TextArea(20,30);textArea.setText("TextArea TextArea TextArea TextArea ");//滚动面板JScrollPane jScrollPane = new JScrollPane(textArea);container.add(jScrollPane);this.setVisible(true);this.setBounds(100,100,500,600);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}/*** 测试* @param args*/public static void main(String[] args) {new JScrollPanelDemo();}
}
5.1、图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;public class JButtonDemo extends JFrame {public JButtonDemo() {//把这个图标变为图片Container container = this.getContentPane();URL url = JButtonDemo.class.getResource("/com/westos/demo6/Irving.jpg");Icon icon = new ImageIcon(url);//把图标添加到按钮上JButton jButton = new JButton(icon);jButton.setToolTipText("图片按钮");container.add(jButton);this.setSize(400,300);this.setVisible(true);this.pack();this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}/*** 测试* @param args*/public static void main(String[] args) {new JButtonDemo();}
}
5.1、单选按钮
import javax.swing.*;
import java.awt.*;public class JButtonDemo2 extends JFrame {public JButtonDemo2() {Container container = this.getContentPane();//单选框JRadioButton radioButton1 = new JRadioButton("radioButton1");JRadioButton radioButton2 = new JRadioButton("radioButton2");JRadioButton radioButton3 = new JRadioButton("radioButton3");//给单选框分组ButtonGroup buttonGroup = new ButtonGroup();buttonGroup.add(radioButton1);buttonGroup.add(radioButton2);buttonGroup.add(radioButton3);container.add(radioButton1,BorderLayout.SOUTH);container.add(radioButton2,BorderLayout.NORTH);container.add(radioButton3,BorderLayout.CENTER);container.setBackground(Color.YELLOW);this.setSize(600,500);this.setVisible(true);this.pack();this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}/*** 测试*/public static void main(String[] args) {new JButtonDemo2();}
}
5.1 复选按钮
import javax.swing.*;
import java.awt.*;public class JButtonDemo3 extends JFrame {public JButtonDemo3() {Container container = this.getContentPane();//创建复选框Checkbox checkBox1 = new Checkbox("CheckBox01");checkBox1.setSize(200,200);Checkbox checkBox2 = new Checkbox("CheckBox02");checkBox2.setSize(200,200);//复选框放进窗口容器container.add(checkBox1,BorderLayout.SOUTH);container.add(checkBox2,BorderLayout.NORTH);//设置窗口属性this.setSize(400,300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo3();}
}
6.1、列表下拉框
import javax.swing.*;
import java.awt.*;
import java.util.Comparator;public class ComboxDemo01 extends JFrame {public ComboxDemo01() {Container container = this.getContentPane();//创建下拉框JComboBox jComboBox = new JComboBox();jComboBox.addItem(null);jComboBox.addItem("拍电影");jComboBox.addItem("看电影");jComboBox.addItem("付费");container.add(jComboBox);container.setBackground(Color.YELLOW);this.setSize(400,300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboxDemo01();}
}
6.2、列表框
/*** 列表框*/import javax.swing.*;
import java.awt.*;
import java.util.Vector;public class ComboxDemo02 extends JFrame {public ComboxDemo02() {Container container = this.getContentPane();//列表的内容Vector<String> contents = new Vector<>();contents.add("上海自来水来自海上");contents.add("山东运粮车粮运东山");contents.add("山西产煤处煤产西山");//把集合放进列表中JList jList = new JList(contents);container.add(jList);//设置窗口属性this.setSize(400,300);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboxDemo02();}
}
6.3、文本框
import javax.swing.*;
import java.awt.*;/*** 文本框*/
public class TextDemo extends JFrame {public TextDemo() {Container container = this.getContentPane();container.setBackground(Color.YELLOW);//创建文本框对象JTextField jTextField1 = new JTextField("这是一个文本框1");JTextField jTextField2 = new JTextField("这是一个文本框2",10);container.add(jTextField1,BorderLayout.NORTH);container.add(jTextField2,BorderLayout.SOUTH);this.setSize(400,300);this.setVisible(true);this.pack();this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo();}
}
6.4、密码框
import javax.swing.*;
import java.awt.*;/*** 文本域*/
public class JScrollDemo extends JFrame {public JScrollDemo() {Container container = this.getContentPane();//文本域JTextArea jTextArea = new JTextArea(25, 30);jTextArea.setText("这是文本域中的一行文本");//滚动面板JScrollPane jScrollPane = new JScrollPane(jTextArea);container.add(jScrollPane);this.setVisible(true);this.setSize(300,300);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}
总结:在GUI学习中的基本知识有Swing和AWT的组件,如文本组件,按钮和滑动组件等,这些都是基本的用户界面组件,使用也很频繁。
JavaSE基础——GUI编程(Swing)相关推荐
- 「JavaSE」- GUI编程
GUI 编程 Swing和AWT 是java开发GUI常用的技术,但是由于外观不太美观, 组件数量偏少, 并且运行需要JRE环境(动不动就上百M的JRE包....), 所以没有流行起来.但是 ,建议简 ...
- javase基础socket编程之局域网聊天,局域网文件共享
2017年06月04日 23点25分 javase基础学完可以做什么,javase实现局域网聊天室 包含内容:基础语法,面向对象,多线程,IO流,GUI编程,网络编程(udp) 实现功能:局域网群聊 ...
- Java基础-GUI编程讲解
GUI编程 组件 窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具 简介 GUI的核心技术:Swing和AWT 1.界面不美观 2.需要jre环境 3.GUI是MVC的 ...
- java gui 单选_java GUI编程(swing)之三swing单选框复选框组件
swing复选框(JCheckBox) 单选框(JRadioButton) 特别说明:同一组单选按钮,必须先创建一个ButtonGroup,然后把单选按钮放到ButtonGroup 中 package ...
- Java的GUI编程---Swing介绍
Swing 窗口,面板 package com.akita.lesson03;import javax.swing.*; import java.awt.*;public class JFrameDe ...
- Java:GUI编程
文章目录 GUI编程 AWT 一.AWT介绍 二.组件和容器(Component和Container) 2.1.Frame 2.2.Panel 三.布局管理器 3.1.第一种布局管理器--FlowLa ...
- 基础,算法,编程的1000+篇文章总结
基础,算法,编程的1000+篇文章总结 本文收集和总结了有关基础,算法,编程的1000+篇文章,由于篇幅有限只能总结近期的内容,想了解更多内容可以访问:http://www.ai2news.com/, ...
- GUI编程基础01AWT(了解)
文章目录 GUI编程基础01AWT 简介 AWT(抽象窗口工具包) 组件和容器 第一个Frame窗口 frame类的常用方法 Panel面板讲解 三种布局管理器 流式布局 东西南北中 表格布局 练习 ...
- 第一篇、GUI编程基础
文章目录 前言 一.GUI编程 二.技术介绍 1.AWT 1.1AWT介绍 1.2组件和容器 1.2.1框架Frame 1.生成第一个界面 2.回顾封装的应用 1.2.2 面板Panel 1.面板在框 ...
- 零基础入门学习Python(33)-图形用户界面编程(GUI编程)EasyGui
用户界面编程,即平时说的GUI(Graphical User Interface)编程,那些带有按钮.文本.输入框的窗口的编程 EasyGui是一个非常简单的GUI模块,一旦导入EasyGui模块,P ...
最新文章
- 量子技术发展的一小步:Google AI推出开源框架Cirq
- 马斯克员工参与新冠研究,论文登上Nature子刊
- c语言编程能控制热风炉,利用C语言设计热风炉悬链线拱顶研究.pdf
- 李开复给中国学生的第六封信:选择的智慧
- 机器学习-MNIST数据集-神经网络
- 什么是注入式攻击(2)
- VScode前端开发常用插件
- tensorflow2.1学习--认识张量和常用函数二
- 决策树 结构_如何快速简单的理解决策树的概念?
- 跨时区时间运算以及时间实时更新方法
- 为什么python叫爬虫_python为什么叫爬虫
- 美团圈圈是什么?美团圈圈介绍,美团圈圈是什么平台?
- 1.Python下载与安装教程 For Windows
- 软件定制开发怎么收费
- java解析dcm文件到jpg
- -bash: ifconfig: command not found
- Isight与MATLAB联合仿真时出现:无法定位或初始化类(unsupported major minor version 52.0)
- Python 书籍 搜索
- 使用Python的turtle模块绘制爱心图案
- 苹果电脑win10蓝牙音响卡顿_解决Macbook Pro下Win10双系统蓝牙鼠标无法连接及卡顿...
热门文章
- yum离线安装rpm包
- the first blog
- 拓端tecdat|用R语言和python进行社交网络中的社区检测
- 拓端tecdat|R语言使用Metropolis- Hasting抽样算法进行逻辑回归
- docker 权限问题 Got permission denied while trying to connect to the Docker daemon socket at 。。。
- 4、matplotlib中的子图相关subplot
- java日历制作日期不对_在我的代码中,为什么Java日期或日历中的月份日期不同? - java...
- batchsize和模型精度的影响
- fer2013人脸表情数据实践
- Python下安装LDA模块,学习使用