GUI编程--3 Swing
GUI编程-3 Swing
3.1 JFrame 窗口
窗口:
package com.ssl.lesson04;
import javax.swing.*;
import java.awt.*;
public class JframeDemo {
//init():初始化
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口!");
jf.setVisible(true);
jf.setBounds(100,100,400,400);
jf.setBackground(new Color(11, 81, 231)); //这个设置不了颜色
//通过获取容器设置背景颜色
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.BLUE);
//设置文字JLabel
JLabel label = new JLabel("这是一个Jlabel的标签");
jf.add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JframeDemo().init();
}
}
3.2 弹窗
package com.ssl.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("弹出一个对话框");
button.setBounds(30,30,200,50);
this.add(button);
button.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,100,100);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//默认有关闭事件
Container container = this.getContentPane();
container.setLayout(null);
//container.add(new Label("111")); 为啥不显示内容
}
}
3.3 标签
label
new JLabel("xxx");
设置图标:ICON
图标
package com.ssl.lesson04;
import javax.swing.*;
import java.awt.*;
//图标:需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){} //无参构造
public IconDemo(int width,int height){
this.width=width;
this.height=height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标可以放在标签上,也可以放在按钮上
JLabel label = new JLabel("Icontest", iconDemo, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setBounds(40,40,200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,this.width,this.height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片
package com.ssl.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel label = new JLabel("ImageIcon");
//获取图片地址
URL url = ImageIconDemo.class.getResource("T.png");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
//标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setBounds(40,40,200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4 面板
package com.ssl.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10)); //后2个参数,间距
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
//添加面板
container.add(panel);
this.setVisible(true);
this.setBounds(40,40,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- 滑动窗口JScroll
package com.ssl.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,20);
textArea.setText("欢迎学习java GUI编程");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(200,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5 按钮
- 图片按钮
package com.ssl.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container = this.getContentPane();
//将一个图片变成图标
URL resource = JButtonDemo01.class.getResource("T.png");
ImageIcon imageIcon = new ImageIcon(resource);
//把图标放到按钮上
JButton button = new JButton();
button.setIcon(imageIcon);
button.setToolTipText("图片按钮");
//加到容器上
container.add(button);
this.setVisible(true);
this.setBounds(200,200,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 单选按钮
package com.ssl.lesson05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends JFrame{
public JButtonDemo02(){
Container container = this.getContentPane();
//单选按钮
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton03");
//单选框一般只能选择一个,一般都要分组。一个组中只能选择一个。
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.CENTER);
container.add(jRadioButton2,BorderLayout.NORTH);
container.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 复选按钮
package com.ssl.lesson05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame{
public JButtonDemo03(){
Container container = this.getContentPane();
//多选按钮
JCheckBox jCheckBox01 = new JCheckBox("1");
JCheckBox jCheckBox02 = new JCheckBox("2");
JCheckBox jCheckBox03 = new JCheckBox("3");
container.add(jCheckBox01,BorderLayout.SOUTH);
container.add(jCheckBox02,BorderLayout.NORTH);
container.add(jCheckBox03,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(200,200,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6 列表
- 下拉框
package com.ssl.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container container = this.getContentPane();
//列表
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setBounds(400,400,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
package com.ssl.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02(){
Container container = this.getContentPane();
//生成列表内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setBounds(400,400,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
应用场景:
- 选择地区,或者一些单个选项。
- 列表,展示信息,一般是动态扩容。
3.7 文本框
- 文本框
package com.ssl.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
//文本框
JTextField jTextField = new JTextField("hello");
container.add(jTextField);
this.setVisible(true);
this.setBounds(400,400,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 密码框
package com.ssl.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(){
Container container = this.getContentPane();
//密码框
JPasswordField jPasswordField = new JPasswordField();
//可以设置
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
this.setVisible(true);
this.setBounds(400,400,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
- 文本域
package com.ssl.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo03 extends JFrame {
public TestTextDemo03(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,20);
textArea.setText("欢迎学习java GUI编程");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(400,400,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo03();
}
}
前端页面HTML+监听器serverlet
C/S
B/S 主流
GUI编程--3 Swing的更多相关文章
- Java中的Swing及AWT又称GUI编程
Java中的Swing及AWT又称GUI编程. 关于学习Java要不要学Swing及AWT,这个完全取决于个人的开发及发展方向. 如果从事web方向的开发,则可以不用学习Swing及AWT. 如果从事 ...
- Java GUI编程中AWT/swing/SWT的优缺点
http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...
- Java学习之Swing Gui编程
Java学习之Swing Gui编程 0x00 前言 前面的使用的Gui是基于Awt 去进行实现,但是在现实写Gui中 AWT实际运用会比较少. 0x01 Swing 概述 AWT 和Swing 区别 ...
- 1.JAVA之GUI编程概述
下列内容为本人看毕向东老师java视频教程学习笔记! JAVA GUI图形用户界面编程: Windows 操作系统提供两种操作方式: ...
- 4.JAVA之GUI编程事件监听机制
事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...
- java Gui编程 事件监听机制
1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式: 1. 命令交互方式 图书管理系统 ...
- python大法好——ython GUI编程(Tkinter)
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- 实验十五 GUI编程练习与应用程序部署
实验十五 GUI编程练习与应用程序部署 实验时间 2018-12-6 一:理论部分 1.Java 程序的打包:编译完成后,程序员将.class 文件压缩打包为 .jar 文件后,GUI 界面序就可以 ...
- 第70讲:Scala界面GUI编程实战详解
今天又学习了王家林老师的scala学习讲座第70讲,关于scala的界面编程,让我们来初步学习一下scala中界面编程的过程. 信息来源于 DT大数据梦工厂微信公众账号:DT_Spark 关注微信账号 ...
- Python GUI 编程
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
随机推荐
- 实验一 密码引擎-1-OpenEuler-OpenSSL编译
1. 下载最新的OpenSSL源码 2. 用自己的8位学号建立一个文件夹,cd 你的学号,用pwd获得绝对路径 3. 参考https://www.cnblogs.com/rocedu/p/508762 ...
- Java-【Arrays类】和【System类】
Arrays类 [基本介绍] JDK中提供了一个专门用于操作数组的工具类,即Arrays类,位于java util 中. 用前需导包:import java.util.Arrays; [常用方法] 返 ...
- python菜鸟学习: 1.用户登录输入输出
# -*-coding: utf-8 -*-name = 'liyuzhoupan'password = '123'def login_test(): count = 0 while count &l ...
- Linux 使用Bind提供域名解析服务
DNS域名解析服务 相较于由数字构成的IP地址,域名更容易被理解和记忆,所以我们通常更习惯通过域名的方式来访问网络中的资源.但是,网络中的计算机之间只能基于IP地址来相互识别对方的身份,而且要想在互联 ...
- 使用LitJson输出格式化json文件到本地
百度上搜了半天,竟然没有C#使用LitJson格式化输出的例子,全都是Newtonsoft.Json的,最后在litjson的官网找到了方法. 给大家分享一下: https://litjson.net ...
- OO课程第二阶段(实验和期中试题)总结Blog2
OO课程第二阶段(实验和期中试题)总结Blog2 前言:学习OOP课程的第二阶段已经结束了,在此进行对于知识点,题量,难度的个人看法. 学习OOP课程的第二阶段已经结束了,较第一次阶段学习难度加大,学 ...
- mybatis核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- 如何修改被编译后DLL文件 (转发)
我们平时在工作中经常会遇到一些已经被编译后的DLL,而且更加麻烦是没有源代码可以进行修改,只能针对这个DLL的文件进行修改才能得到我们想要的结果:本文将通过一个实例来演示如果完成一个简单的修改;我们将 ...
- spring boot 中 CommandLineRunner接口使用
接口定义:接口,用于指示bean包含在SpringApplication中时应运行.可以在同一应用程序上下文中定义多个CommandLineRunner bean,并可以使用ordered接口或@Or ...
- oracle从1到10生成顺序号
oracle从1到10生成顺序号,脚本案例如下: select 'ABAB'|| lpad(level,5,0) as serial_no from dual connect by level< ...