GUI编程--03--Swing
Swing 窗口
import javax.swing.*;
import java.awt.*;public class TestJFrame {public static void main(String[] args) {//建立一个窗口new TestJFrame().init();}//init();初始化public void init(){//顶级窗口JFrame jf = new JFrame("这是一个JFram窗口");jf.setVisible(true);jf.setBounds(100,100,200,200);//设置文字JlabelJLabel label = new JLabel("加油!!!");jf.add(label);//居中label.setHorizontalAlignment(SwingConstants.CENTER);//容器实例化Container contentPane = jf.getContentPane();contentPane.setBackground(Color.green);//关闭事件jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
容器实例化
//容器实例化
Container contentPane = jf.getContentPane();
弹窗 JDialog
package swing;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;public class TestDialog {public static void main(String[] args) {new Dialog1();}
}//主窗口
class Dialog1 extends JFrame{public Dialog1(){this.setVisible(true);this.setBounds(100,100,300,300);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Jframe放东西,容器Container container = this.getContentPane();//绝对布局container.setLayout(null);//按钮JButton jButton = new JButton("弹出对话框");//创建jButton.setBounds(30,30,200,50);container.add(jButton);//点击这个按钮的时候,弹出一个弹窗(监听事件)jButton.addActionListener(new AbstractAction() { //监听器@Overridepublic void actionPerformed(ActionEvent e) {//弹窗new Dialog2();}});container.add(jButton);}
}
//弹窗的窗口
class Dialog2 extends JDialog{public Dialog2(){this.setVisible(true);this.setBounds(100,100,500,500);//弹窗中默认有此事件//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);Container container = this.getContentPane();container.setLayout(null);container.setBackground(Color.PINK);Label label = new Label("fighting !!!");label.setBounds(100,100,100,100);container.add(label);}
}
标签/图片
label
new Lavel(“xxx”);
new JLavel(“xxx”);
Icon 图标
import javax.swing.*;
import java.awt.*;public class TestIcon {public static void main(String[] args) {new Icon1(100,100).init();}
}class Icon1 extends JFrame implements Icon{//属性private int width;private int high;public Icon1(){}//无参构造public Icon1(int width,int high){this.width = width;this.high = high;}//方法public void init(){setBounds(100,100,300,300);Icon1 icon1 = new Icon1(15,15);//图标放在标签上,也可以放在按钮上JLabel jLabel = new JLabel("gaogao",icon1,SwingConstants.CENTER);Container container = getContentPane();container.add(jLabel);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x,y,width,high);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.high;}
}
ImageIcon 图片
package swing;import javax.swing.*;
import java.awt.*;
import java.net.URL;public class TestImageIcon {public static void main(String[] args) {new TestImageIcon();}public TestImageIcon() {JFrame jFrame = new JFrame();//获取图片地址 url是一个具体的地址JLabel jLabel = new JLabel("ImageIcon");URL url = TestImageIcon.class.getResource("gougou.jpg");//获取当前类下的资源ImageIcon imageIcon = new ImageIcon(url);//命名避免冲突jLabel.setIcon(imageIcon);//图片饭在jLabel上jLabel.setHorizontalAlignment(SwingConstants.CENTER);Container container =jFrame.getContentPane();container.add(jLabel);jFrame.setVisible(true);jFrame. setBounds(100,100,300,300);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}}
面板 JPanel
import javax.swing.*;
import java.awt.*;public class TestJPane extends JFrame{public TestJPane() {Container container = this.getContentPane();container.setLayout(new GridLayout(2,1,10,10)); //间距JPanel jPanel = new JPanel(new GridLayout(1,3));JPanel jPanel1 = new JPanel(new GridLayout(1,3));JPanel jPanel2 = new JPanel(new GridLayout(1,3));JPanel jPanel3 = new JPanel(new GridLayout(1,3));jPanel.add(new JButton("1"));jPanel.add(new JButton("2"));jPanel.add(new JButton("3"));jPanel1.add(new JButton("1"));jPanel1.add(new JButton("2"));jPanel1.add(new JButton("3"));jPanel2.add(new JButton("1"));jPanel2.add(new JButton("2"));jPanel2.add(new JButton("3"));jPanel3.add(new JButton("1"));jPanel3.add(new JButton("2"));jPanel3.add(new JButton("3"));container.add(jPanel);container.add(jPanel1);container.add(jPanel2);container.add(jPanel3);setVisible(true);setBounds(100,100,300,300);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJPane();}
}
JScrollPonel 带有滚动条的 卷轴面板
import javax.swing.*;
import java.awt.*;public class JScrollDemo extends JFrame {public JScrollDemo(){Container container = this.getContentPane();//文本域JTextArea textArea = new JTextArea(200 ,400);textArea.setText("待到秋来九月八 我花开后百花杀!!!");//Scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setBounds(100,100,400,400);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}
按钮
普通按钮 JButton
import javax.swing.*;
import java.awt.*;
import java.net.URL;public class TestJButton extends JFrame {public TestJButton() {Container container = this.getContentPane();//将图片变为一个图标URL url = TestJButton.class.getResource("gougou.jpg");ImageIcon icon = new ImageIcon(url);//把图片放在按钮上JButton jButton = new JButton();jButton.setIcon(icon);jButton.setSize(100,100);jButton.setToolTipText("图片按钮");//addcontainer.add(jButton);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJButton();}
}
单选 JRadioButton
import javax.swing.*;
import java.awt.*;
import java.net.URL;public class TestJButton extends JFrame {public TestJButton() {Container container = this.getContentPane();//单选框JRadioButton jRadioButton1 = new JRadioButton("1");JRadioButton jRadioButton2 = new JRadioButton("2");JRadioButton jRadioButton3 = new JRadioButton("3");//由于单选框只能选一个所以我们要分组,一个组中只能选一个ButtonGroup group = new ButtonGroup();group.add(jRadioButton1);group.add(jRadioButton2);group.add(jRadioButton3);container.add(jRadioButton1,BorderLayout.CENTER);container.add(jRadioButton2,BorderLayout.NORTH);container.add(jRadioButton3,BorderLayout.SOUTH);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJButton();}
}
复选 JCheckBox
import javax.swing.*;
import java.awt.*;
import java.net.URL;public class TestJButton extends JFrame {public TestJButton() {Container container = this.getContentPane();JCheckBox jCheckBox = new JCheckBox("1");JCheckBox jCheckBox1 = new JCheckBox("2");container.add(jCheckBox,BorderLayout.SOUTH);container.add(jCheckBox1,BorderLayout.NORTH);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJButton();}
}
列表
下拉框 JComboBox
应用:选择地区,或一些单个选项
import javax.swing.*;
import java.awt.*;public class TestJCombobox extends JFrame{public TestJCombobox() {Container container = this.getContentPane();JComboBox jComboBox = new JComboBox();jComboBox.addItem(null);jComboBox.addItem("正在上映");jComboBox.addItem("已下架");jComboBox.addItem("即将上映");container.add(jComboBox);this.setBounds(100,100,300,300);this.setBackground(Color.CYAN);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJCombobox();}
}
列表 JList
import javax.swing.*;
import java.awt.*;public class TestJCombobox extends JFrame{public TestJCombobox() {Container container = this.getContentPane();//生成列表内容String[] contents = {"1","2","3"};//列表需要放入内容JList jList = new JList(contents);container.add(jList);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestJCombobox();}
}
文本框
文本框 JTextField
import javax.swing.*;
import java.awt.*;public class TestText1 extends JFrame{public TestText1() {Container container = getContentPane();JTextField jTextField = new JTextField("强啊");JTextField jTextField1 = new JTextField("真的吗 666666666",20);//布局container.add(jTextField, BorderLayout.NORTH);container.add(jTextField1,BorderLayout.SOUTH);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestText1();}
}
密码框 JPasswordField
有默认值,但也可以设置 jPasswordField.setEchoChar(’+’);
import javax.swing.*;
import java.awt.*;public class TestText1 extends JFrame{public TestText1() {Container container = getContentPane();JPasswordField jPasswordField = new JPasswordField(); //有默认值,但也可以设置//jPasswordField.setEchoChar('+');container.add(jPasswordField);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TestText1();}
}
文本域 JTextArea
import javax.swing.*;
import java.awt.*;public class JScrollDemo extends JFrame {public JScrollDemo(){Container container = this.getContentPane();//文本域JTextArea textArea = new JTextArea(200 ,400);textArea.setText("待到秋来九月八,我花开后百花杀,");textArea.append("\r\n");textArea.append("冲天香阵透长安,满城尽带黄金甲。");//Scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setBounds(100,100,400,400);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}
}
GUI编程--03--Swing相关推荐
- Java GUI编程:swing JTree实现树形菜单代码示例
package com.zxl;import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode;/*** @Descripti ...
- Java GUI编程:swing实现上传tiff文件至hdfs功能
上传tiff文件至hdfs pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...
- Java GUI编程:swing创建窗体代码详解
package com.zxl;import java.awt.Container; import java.awt.FlowLayout;import javax.swing.JButton; im ...
- java gui编程:swing创建窗体和进度条代码示例
package com.zxl;import javax.swing.*; import java.awt.*; import javax.swing.event.ChangeEvent; impor ...
- The package javax.swing is not accessible(java GUI 编程时引用swing包和awt包时会报错怎么办)
Java GUI 编程时引用swing包和awt包时会报错怎么办 一.环境与错误现象 使用的编译器为eclipse,情况为: 二.解决方法 问题主要是由工程中的module-info.java这个文件 ...
- java swing开发窗体程序开发(一)GUI编程
Java SWing就是java提供的一个类库专门用于开发窗体程序的,相对于C#的winform,java swing的开发要比C#更加麻烦,他不像C#拥有可视化的界面编辑工具 而是需要像QT一样纯代 ...
- java gui 单选_java GUI编程(swing)之三swing单选框复选框组件
swing复选框(JCheckBox) 单选框(JRadioButton) 特别说明:同一组单选按钮,必须先创建一个ButtonGroup,然后把单选按钮放到ButtonGroup 中 package ...
- Java基础-GUI编程讲解
GUI编程 组件 窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具 简介 GUI的核心技术:Swing和AWT 1.界面不美观 2.需要jre环境 3.GUI是MVC的 ...
- Python_017 GUI编程
2019独角兽企业重金招聘Python工程师标准>>> GUI编程(Tkinter) python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: ...
- Python基础教程(十二):GUI编程、版本区别、IDE
python GUI编程(Tkinter) python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是P ...
最新文章
- 句法分析是什么?成分句法分析、依存句法分析又是什么?他们之间有什么区别?
- 从程序员的角度深入理解MySQL
- Java中的锁的概念大汇总
- 不推荐重写service
- 数据级并行--计算机体系结构
- spring(7)spring mvc 的高级技术
- python自动化元素定位_Appium+Python自动化 4 -appium元素定位
- 使用 JQueryElement ResponseProgress 显示页面执行进度
- 你真的知道 Python 字符串怎么用吗?
- GB28181之H264打包PS
- 如何从一段视频中一次性修整多个片段
- ARCore之路-环境准备
- 生产计划排产计划的模式有哪几种?
- 哪种款式的耳机不伤耳朵,五款不伤害耳朵听力的骨传导耳机推荐
- 空间点过程与随机测度(二):测度的故事
- STM32MP157系列教程连载-Linux应用开发篇1:STM32MP1微处理器之Ubuntu安装与体验
- MATLAB_LSB_隐藏水印和提取,附代码
- 初次使用github
- 高维数据惩罚回归方法:主成分回归PCR、岭回归、lasso、弹性网络elastic net分析基因数据...
- 必备的 DevOps 工具链大盘点