【Java GUI 编程】Swing 用户界面开发工具包
文章目录
- Swing
- 窗口
- 弹窗
- 标签
- 面板
- 滚动条
- 按钮
- 列表
- 文本框
Swing
用户界面开发工具包,比 AWT 更加高级一点,Swing 可以使用任何可插拔的外观风格 ,用很少的代码就可以创建优雅的用户界面,工具包中所有的包都是以swing作为名称
窗口
package JavaGUI;
import javax.swing.*;
import java.awt.*;
/** * @Title: Test13JFrame * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 13:57 */
public class Test13JFrame {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
// 获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.red);
JLabel label = new JLabel("欢迎来到 Java !");
this.add(label);
// 水平居中标签
label.setHorizontalAlignment(SwingConstants.CENTER);
this.setVisible(true);
this.setBounds(1,1,200,200);
}
}
弹窗
JDialog 用来被弹出窗口
package JavaGUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/** * @Title: Test15Dialog * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 17:31 */
public class Test15Dialog extends JFrame{
public Test15Dialog(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 放东西,容器
Container container = this.getContentPane();
container.setLayout(null); // 绝对布局
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
// 当点击这个按钮的时候弹窗,监听器
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new Test15Dialog();
}
}
// 弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null); //绝对定位
container.add(new Label("学 Java GUI 编程"));
}
}
标签
label
画了一个圆作为标签
public class Test16IconDemo1 extends JFrame implements Icon {
private int width;
private int height;
public Test16IconDemo1(){ }
public Test16IconDemo1(int width,int height){
this.width = width;
this.height = height;
}
public void init(){
Test16IconDemo1 iconDemo1 = new Test16IconDemo1(15,15);
// 图标放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("icontest",iconDemo1,SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new Test16IconDemo1().init();
}
// 画了一个圆作为标签
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
效果如下
自定以图片作为标签
package JavaGUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/** * @Title: * @Package * @Description: * @author: maze * @date 2020/10/20下午 18:10 */
public class Test16IconDemo1 extends JFrame{
public void init(){
JLabel jLabel = new JLabel("icontest");
// 获取图片地址
URL url = Test16IconDemo1.class.getResource("1.png");
// 加载 url
ImageIcon icon = new ImageIcon(url);
// 设置标签属性
jLabel.setIcon(icon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 把标签添加到容器中
Container container = getContentPane();
container.add(jLabel);
// 设置 JFrame 窗口
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new Test16IconDemo1().init();
}
}
如图所示
面板
JPainel
public class Test17JPanelDemo1 extends JFrame {
public static void main(String[] args) {
new Test17JPanelDemo1();
}
public Test17JPanelDemo1() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,3));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
container.add(panel);
container.add(panel2);
container.add(panel3);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
滚动条
JScrollPanel
package JavaGUI;
import javax.swing.*;
import java.awt.*;
/** * @Title: Test18JScrollPanel * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 21:54 */
public class Test18JScrollPanel extends JFrame {
public Test18JScrollPanel() {
// 容器
Container container = this.getContentPane();
//文本域
TextArea area = new TextArea(20,100);
area.setText("欢迎来到 Java 的世界,这里是 GUI Swing 工具包的滚动条实现!\n" +
" Java是目前最为广泛的网络编程语言。它具有简单,面向对象,稳定等特点。\n" +
" 2.Java 语言简单是指这门语言既易学好用。不要将简单误解为这门语言很干瘪。\n" +
"如果你学习过 C++语言,你会感觉 Java很眼熟,因为 Java中许多基本语句的语法和 C++一样\n" +
"。如果从语言的简单性方面看...nhhhhhhhhhhhhhhh");
// 添加一个Scroll 面板
JScrollPane scrollPane = new JScrollPane(area);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test18JScrollPanel();
}
}
如下图
按钮
图片按钮
package JavaGUI;
import javax.swing.;
import java.awt.;
import java.net.URL;/* @Title: Test19JButton @Package JavaGUI @Description: 实现一个图片按钮 @author: maze @date 2020/10/20下午 22:38 */
public class Test19JButton extends JFrame {public Test19JButton() {
Container container = this.getContentPane();
// 将一个图片变成图标
URL url = Test19JButton.class.getResource("1.png");
ImageIcon imageIcon = new ImageIcon(url);
// 把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(imageIcon);
button.setToolTipText("图片按钮");
//add
container.add(button);
this.setVisible(true);
this.setSize(50,30);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test19JButton();
}
}
单选按钮
将三个按钮放在一个组里,在组里只能有一个被选中
package JavaGUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/** * @Title: Test20Button * @Package * @Description: * @author: maze * @date 2020/10/20下午 22:49 */
public class Test20Button extends JFrame{
public Test20Button() throws HeadlessException {
Container container = this.getContentPane();
// 将一个图片变成图标
URL url = Test19JButton.class.getResource("1.png");
ImageIcon imageIcon = new ImageIcon(url);
//单选框
JRadioButton radioButton01 = new JRadioButton("男");
JRadioButton radioButton02 = new JRadioButton("女");
JRadioButton radioButton03 = new JRadioButton("未知");
// 由于是单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
container.add(radioButton01,BorderLayout.CENTER);
container.add(radioButton02,BorderLayout.NORTH);
container.add(radioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(50,30);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test20Button();
}
}
如下图
- 复选按钮
去掉分组框,就变成复选按钮了
package JavaGUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/** * @Title: Test20Button * @Package * @Description: * @author: maze * @date 2020/10/20下午 22:49 */
public class Test20Button extends JFrame{
public Test20Button() throws HeadlessException {
Container container = this.getContentPane();
// 将一个图片变成图标
URL url = Test19JButton.class.getResource("1.png");
ImageIcon imageIcon = new ImageIcon(url);
//单选框
JRadioButton radioButton01 = new JRadioButton("写代码");
JRadioButton radioButton02 = new JRadioButton("读书");
JRadioButton radioButton03 = new JRadioButton("陪女朋友逛街");
JRadioButton radioButton04 = new JRadioButton("打游戏");
// 由于是单选框只能选择一个,分组,一个组中只能选择一个
container.setLayout(new GridLayout(1,4));
container.add(radioButton01);
container.add(radioButton02);
container.add(radioButton03);
container.add(radioButton04);
this.pack();
this.setVisible(true);
this.setSize(50,30);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test20Button();
}
}
列表
package JavaGUI;
import javax.swing.*;
import java.awt.*;
/** * @Title: Test21Combobox * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 23:29 */
public class Test21Combobox extends JFrame {
public Test21Combobox() {
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("正在上映");
comboBox.addItem("即将上映");
comboBox.addItem("已下架");
container.add(comboBox);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test21Combobox();
}
}
列表框
public class Test21Combobox extends JFrame {
public Test21Combobox() {
Container container = this.getContentPane();
// 生成列表内容
String[] contents = { "1","2","3"};
// 列表中需要放入的内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test21Combobox();
}
}
文本框
文本框
public class Test21Combobox extends JFrame {
public Test21Combobox() {
Container container = this.getContentPane();
JTextField field1 = new JTextField("hello");
JTextField field2 = new JTextField("hello",20);
container.add(field1,BorderLayout.NORTH);
container.add(field2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test21Combobox();
}
}
密码框
public class Test21Combobox extends JFrame {
public Test21Combobox() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test21Combobox();
}
}
文本域
public class Test21Combobox extends JFrame {
public Test21Combobox() {
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎来到 Java GUI 系列的学习");
//面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test21Combobox();
}
}
到这里 GUI 的重点知识就差不多了
写一篇博客是,利用 GUI 编程实现一个贪吃蛇游戏
还没有评论,来说两句吧...