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 的 ...
随机推荐
- 通过modbus+socket+GPRS采集硬件数据
# !/usr/bin/python # -*- coding: UTF-8 -*- import sys import os TESTCASE = os.path.dirname(os.path.a ...
- yield基础知识
function* y(p1){ let r=p1; r=(yield r-5) //表达式A,在第1轮是普通return,在第2轮next(param)时会被param替换 r=(yield r*2 ...
- 4组-Alpha冲刺-5/6
一.基本情况 队名:摸鲨鱼小队 组长博客:https://www.cnblogs.com/smallgrape/p/15563236.html 小组人数:8人 二.冲刺概况汇报 组长:许雅萍 过去两天 ...
- 网页元素间距测量(better rule插件的使用)
我们在测试UI界面的时候,需要测量各元素大小及元素之间的距离.元素大小,使用F12可以简易的得到数据,但是元素的间距相对来说会比较复杂.这里推荐一款chrome插件better rule,帮助大家测量 ...
- C++从键盘读取一行的方法
从键盘读取一行的方法 cin类中的成员函数getline()和get()--使用数组来处理字符串 cin.getline(数组,要读入的字符数).getline()将丢弃换行符.这个成员函数通过换行符 ...
- Using Semaphores in Delphi, Part 2: The Connection Pool
Abstract: Semaphores are used to coordinate multiple threads and processes. That semaphores provide ...
- 运行python脚本报错SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
运行python脚本报错 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: ...
- axel多线程下载
Axel 是一个轻量级下载程序,它和其他加速器一样,对同一个文件建立多个连接,每个连接下载单独的文件片段以更快地完成下载. Axel 通过打开多个 HTTP/FTP 连接来将一个文件进行分段下载,从而 ...
- 拉勾java核心类库--String类
String类 @author :magiclx 摘要:String类是用final进行修饰的,关于字符串的一个类,其中包含很多个方法 关键词:String,常量池,正则表达式,数组转化 正文: 参考 ...
- 【随笔】Tomcat部署图片服务器Server.xml配置记录
在tomcat应用conf/server.xml文件的标签中添加下面内容: <Service name="imageService"> <!--分配8089端口 ...