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 classJFrameDemo{public static voidmain(String[] args){newJFrameDemo.init();//静态方法不能调用非静态方法--创建对象生成窗口

}publicvoid 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= newButton("button");

container.add(button);

}

}

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

public classJFrameDemo{public static voidmain(String[])args{newMyFrame();

}

}class MyJFrame extendsJFrame{publicMyJFrame(){

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 classDialogDemo{public static voidmain(String[] args){newMainJFrame().init();

}

}class MainJFrame extendsJFrame {public voidinit(){

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(newActionListener(){

@Overridepublic voidactionPerformed(ActionEvent e){newMyDialog();

}

});

container.add(jButton);

}

}class MyDialog extendsJDialog{publicMyDialog(){

setBounds(300,300,100,100);

JLabel jLabel= new JLabel("label");

setVisible(true);

}

}

4. 标签

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

标签:

public classJLabelDemo {public static voidmain(String[] args) {newMyJFrameDemo();

}

}class MyJFrameDemo extendsJFrame{publicMyJFrameDemo(){

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 classDemo {public static voidmain(String[] args) {newMyFrame01().init();

}

}class MyIcon implementsIcon {private intwidth, height;publicMyIcon() {

}public MyIcon(int width, intheight) {this.height =height;this.width =width;

}

@Overridepublic void paintIcon(Component c, Graphics g, int x, inty) {

g.fillOval(x, y, width, height);

}

@Overridepublic intgetIconWidth() {returnwidth;

}

@Overridepublic intgetIconHeight() {returnheight;

}

}class MyFrame01 extendsJFrame {public voidinit() {

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 classDemo {public static voidmain(String[] args) {newMyJFrameDemo();

}

}class MyJFrameDemo extendsJFrame {publicMyJFrameDemo(){

URL url= MyJFrameDemo.class.getResource("wx.jpg");//获取图片地址

ImageIcon imageIcon = newImageIcon(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 classDemo {public static voidmain(String[] args) {newMyFrameDemo().init();

}

}class MyFrameDemo extendsJFrame {public voidinit() {

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= newJScrollPane(jTextArea);

container.add(jScrollPane);

}

}

6. 按钮

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

普通按钮 JButton

public classDemo {public static voidmain(String[] args) {newJButtonDemo01().init();

}

}class JButtonDemo01 extendsJFrame{public voidinit(){

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 extendsJFrame {publicButtonDemo(){//get container

Container container =getContentPane();

URL url= ButtonDemo.class.getResource("wx.jpg");

Icon icon= newImageIcon(url);//put this icon on the button

JButton jButton = newJButton();

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 voidmain(String[] args) {newButtonDemo();

}

}

单选按钮

public class Demo extendsJFrame {publicDemo(){

Container container=getContentPane();

URL url= Demo.class.getResource("wx.jpg");

ImageIcon imageIcon= newImageIcon(url);//single choice outline//JRadioButton的声明和添加偶尔会出现一些意外,原因不明

JRadioButton jRadioButton1 = new JRadioButton("choice1");

JRadioButton jRadioButton2= new JRadioButton("choice2");

JRadioButton jRadioButton3= new JRadioButton("choice3");//由于单选框只能选择一个,所以我们通常将其分组,就是一个组只能选一个,如果不分组的话三个都可以被选中,即形式上变成了多选框

ButtonGroup buttonGroup = newButtonGroup();

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 voidmain(String[] args) {newSingleButtonDemo();

}

}

多选按钮

JCheckBox

public class JCheckBoxDemo extendsJFrame {publicJCheckBoxDemo(){

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 voidmain(String[] args) {newJCheckBoxDemo();

}

}

6. 列表

下拉框 JComBox

public classDemo {public static voidmain(String[] args) {newJComBox().init();

}

}class JComBox extendsJFrame{public voidinit(){

setBounds(200,200,200,200);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setVisible(true);

Container container=getContentPane();

container.setLayout(null);

JComboBox jComboBox= newJComboBox();

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 classDemo {public static voidmain(String[] args) {newJListDemo().init();

}

}class JListDemo extendsJFrame {public voidinit() {

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= newJList(str);

jList.setBackground(Color.CYAN);

jList.setSize(150,80);

jList.setLocation(20,20);

container.add(jList);

}

}

7. 文本

文本框 JTextField

public classDemo {public static voidmain(String[] args) {newJTextFieldDemo().init();

}

}class JTextFieldDemo extendsJFrame{public voidinit(){

setBounds(200,200,200,200);

setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setResizable(false);

Container container=getContentPane();

container.setLayout(null);

JTextField jTextField01= newJTextField();

jTextField01.setText("hello");

jTextField01.setLocation(10,10);

jTextField01.setSize(100,20);

jTextField01.setColumns(10);

JTextField jTextField02= newJTextField();

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 classDemo {public static voidmain(String[] args) {newJTextAreaDemo().init();

}

}class JTextAreaDemo extendsJFrame {public voidinit() {

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 classDemo {public static voidmain(String[] args) {newJTextAreaDemo().init();

}

}class JTextAreaDemo extendsJFrame {public voidinit() {

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 classDemo {public static voidmain(String[] args) {newJPasswordFieldDemo();

}

}class JPasswordFieldDemo extendsJFrame{publicJPasswordFieldDemo(){

setBounds(200,200,200,200);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setVisible(true);

Container container=getContentPane();

JPasswordField jPasswordField= newJPasswordField();

jPasswordField.addActionListener(newMyListener000());

container.add(jPasswordField);

}

}class MyListener000 implementsActionListener{

@Overridepublic voidactionPerformed(ActionEvent e) {

String str=e.getActionCommand();

System.out.println(str);

}

}

java swing choice_Java-GUI基础(三)java.swing相关推荐

  1. 【Java开发语言 01】第一章 Java语言概述(基础常识+Java语言概述+Java程序运行机制及运行过程+Java语言环境的搭建+开发体验hello world+错误:编码GBK的不可映射字符)

    java入门-第一章Java语言概述 1基础常识 软件开发 人机交互方式 常用的DOS命令(win系统,有一些直接通过命令执行的) 2 Java语言概述 什么是计算机语言 关于面向对象和面向过程 Ja ...

  2. 没有基础可以学java吗?零基础学java

    很多人都知道Java作为市场第一大语言,其发展前景也是很可观的.无论IT市场需求如何变动,其Java的霸主地位依然未曾动摇,这足矣见证Java对于IT行业的作用以及价值何其之大.但是有可观的一面必然会 ...

  3. Java程序员必备基础:Java代码是怎么运行的?

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:一个线程池 bug 引发的 GC 思考!个人原创+1博客:点击前往,查看更多 链接:https://segmen ...

  4. Java修炼之路——基础篇——Java集合类详解2

    Set和List区别?Set如何保证元素不重复? Set.List都实现了Collection接口,List是有序的列表,Set是无序的集合(TreeSet有序) List实现类: ArrayList ...

  5. java se开发_JAVA_SE基础——3.Java程序的开发流程

    上一篇,写的是JAVA的环境变量的配置,今天我抽空写篇Java程序的开发流程,下面的教程是我结合书本和毕向东老师的视频写下的心的~ 在没有真正写Java程序前,首先需要了解Java程序的开发过程. S ...

  6. java的特征多态,java基础(三)—–java的三大特征之多态

    正文 面向工具编程有三大特征:封装.继续.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也珍爱了数据.对外界而已它的内部细节是隐藏的,露出给外界的只是它的接见方式. ...

  7. java多态基础_java基础(三)-----java的三大特性之多态

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  8. java 随机句子_Java基础三(Scanner键盘输入、Random随机数、流程控制语句)

    1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 ###01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner类 ...

  9. java.util 常见_Java基础知识-java.util.concurrent包下常见类的使用

    一,Condition 一个场景,两个线程数数,同时启动两个线程,线程A数1.2.3,然后线程B数4.5.6,最后线程A数7.8.9,程序结束,这涉及到线程之间的通信. public classCon ...

  10. Java修炼之路——基础篇——Java关键字

    1:transient 当对象被序列化时,transient阻止其修饰的对象进行序列化:当反序列化时,此对象的值不会被恢复. 2:instanceof 判断引用指向的对象,是不是某个类及其子类的实例对 ...

最新文章

  1. 50道练习实践学习Pandas!
  2. 如何用Map、Filter和Reduce替换Python For循环?
  3. 安卓学习 之 网络技术(十)
  4. 数组和lookup函数
  5. NOIP模拟测试6「那一天我们许下约定(背包dp)·那一天她离我而去」
  6. 679 - Dropping Balls
  7. pdfbox 第一页加内容_Java使用PDFBox操作PDF文件获取页码、文章内容、缩略图
  8. post 261.html,261除以6约等于
  9. mysql容灾备份脚本
  10. mysql的逻辑备份和恢复
  11. 用 C#.NET 编写的一个完整字谜游戏
  12. Sleep函数--使得程序暂停一段时间(单位毫秒)
  13. 可由线性表示且表达式唯一_典型例题解析例1设向量问取何值时可由线性表示且表示.doc...
  14. 安装Chrome Restlet Client插件
  15. ESP32-S3 LVGL http下载B站头像 JPG显示
  16. 鸿蒙系统应用开发初体验(一)
  17. vulnhub-Momentum2
  18. java绕过加密密码_Java实现简单密码加密功能
  19. 微软拼音输入法3.0 在 ie 中有MaxLength 的 textbox 框中输入的bug
  20. 如何判断页面是在移动端还是PC端打开

热门文章

  1. 用C语言求并集和交集
  2. Unity PBR材质
  3. android渠道 积分墙,安卓推广:既然能在应用商店做CPD,为什么还一定要做积分墙?...
  4. python取绝对值数组_Python算法——求数组中绝对值最小的数
  5. 计算机网络与互联网(三)
  6. 白苹果修复_ReiBoot Pro——iOS系统修复软件
  7. java炫酷龙卷风源码,这个炫酷!迷你龙卷风发生装置
  8. 史上最全的自动驾驶研究报告(下)
  9. python加载模型包_R中的错误:需要h5py Python包来保存和加载模型
  10. 6.misc类设备与蜂鸣器驱动