【Java GUI 编程】Swing 用户界面开发工具包

ゝ一世哀愁。 2022-12-16 09:27 328阅读 0赞

文章目录

    • Swing
    • 窗口
    • 弹窗
    • 标签
    • 面板
    • 滚动条
    • 按钮
    • 列表
    • 文本框

Swing

用户界面开发工具包,比 AWT 更加高级一点,Swing 可以使用任何可插拔的外观风格 ,用很少的代码就可以创建优雅的用户界面,工具包中所有的包都是以swing作为名称

窗口

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. /** * @Title: Test13JFrame * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 13:57 */
  5. public class Test13JFrame {
  6. public static void main(String[] args) {
  7. new MyJFrame().init();
  8. }
  9. }
  10. class MyJFrame extends JFrame{
  11. public void init(){
  12. // 获得一个容器
  13. Container contentPane = this.getContentPane();
  14. contentPane.setBackground(Color.red);
  15. JLabel label = new JLabel("欢迎来到 Java !");
  16. this.add(label);
  17. // 水平居中标签
  18. label.setHorizontalAlignment(SwingConstants.CENTER);
  19. this.setVisible(true);
  20. this.setBounds(1,1,200,200);
  21. }
  22. }

弹窗

JDialog 用来被弹出窗口

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. /** * @Title: Test15Dialog * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 17:31 */
  7. public class Test15Dialog extends JFrame{
  8. public Test15Dialog(){
  9. this.setVisible(true);
  10. this.setSize(700,500);
  11. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  12. // 放东西,容器
  13. Container container = this.getContentPane();
  14. container.setLayout(null); // 绝对布局
  15. JButton button = new JButton("点击弹出一个对话框");
  16. button.setBounds(30,30,200,50);
  17. // 当点击这个按钮的时候弹窗,监听器
  18. button.addActionListener(new ActionListener() {
  19. @Override
  20. public void actionPerformed(ActionEvent e) {
  21. new MyDialogDemo();
  22. }
  23. });
  24. container.add(button);
  25. }
  26. public static void main(String[] args) {
  27. new Test15Dialog();
  28. }
  29. }
  30. // 弹窗的窗口
  31. class MyDialogDemo extends JDialog{
  32. public MyDialogDemo(){
  33. this.setVisible(true);
  34. this.setBounds(100,100,500,500);
  35. Container container = this.getContentPane();
  36. container.setLayout(null); //绝对定位
  37. container.add(new Label("学 Java GUI 编程"));
  38. }
  39. }

在这里插入图片描述

标签

label

画了一个圆作为标签

  1. public class Test16IconDemo1 extends JFrame implements Icon {
  2. private int width;
  3. private int height;
  4. public Test16IconDemo1(){ }
  5. public Test16IconDemo1(int width,int height){
  6. this.width = width;
  7. this.height = height;
  8. }
  9. public void init(){
  10. Test16IconDemo1 iconDemo1 = new Test16IconDemo1(15,15);
  11. // 图标放在标签上,也可以放在按钮上
  12. JLabel jLabel = new JLabel("icontest",iconDemo1,SwingConstants.CENTER);
  13. Container container = getContentPane();
  14. container.add(jLabel);
  15. this.setVisible(true);
  16. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17. this.setBounds(100,100,500,500);
  18. }
  19. public static void main(String[] args) {
  20. new Test16IconDemo1().init();
  21. }
  22. // 画了一个圆作为标签
  23. @Override
  24. public void paintIcon(Component c, Graphics g, int x, int y) {
  25. g.fillOval(x,y,width,height);
  26. }
  27. @Override
  28. public int getIconWidth() {
  29. return this.width;
  30. }
  31. @Override
  32. public int getIconHeight() {
  33. return this.height;
  34. }
  35. }

效果如下

在这里插入图片描述

自定以图片作为标签

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.net.URL;
  5. /** * @Title: * @Package * @Description: * @author: maze * @date 2020/10/20下午 18:10 */
  6. public class Test16IconDemo1 extends JFrame{
  7. public void init(){
  8. JLabel jLabel = new JLabel("icontest");
  9. // 获取图片地址
  10. URL url = Test16IconDemo1.class.getResource("1.png");
  11. // 加载 url
  12. ImageIcon icon = new ImageIcon(url);
  13. // 设置标签属性
  14. jLabel.setIcon(icon);
  15. jLabel.setHorizontalAlignment(SwingConstants.CENTER);
  16. // 把标签添加到容器中
  17. Container container = getContentPane();
  18. container.add(jLabel);
  19. // 设置 JFrame 窗口
  20. this.setVisible(true);
  21. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  22. this.setBounds(100,100,500,500);
  23. }
  24. public static void main(String[] args) {
  25. new Test16IconDemo1().init();
  26. }
  27. }

如图所示

在这里插入图片描述

面板

JPainel

  1. public class Test17JPanelDemo1 extends JFrame {
  2. public static void main(String[] args) {
  3. new Test17JPanelDemo1();
  4. }
  5. public Test17JPanelDemo1() {
  6. Container container = this.getContentPane();
  7. container.setLayout(new GridLayout(2,1,10,10));
  8. JPanel panel = new JPanel(new GridLayout(1,3));
  9. JPanel panel2 = new JPanel(new GridLayout(1,2));
  10. JPanel panel3 = new JPanel(new GridLayout(2,3));
  11. panel.add(new JButton("1"));
  12. panel.add(new JButton("1"));
  13. panel.add(new JButton("1"));
  14. panel2.add(new JButton("2"));
  15. panel2.add(new JButton("2"));
  16. panel3.add(new JButton("3"));
  17. panel3.add(new JButton("3"));
  18. panel3.add(new JButton("3"));
  19. panel3.add(new JButton("3"));
  20. panel3.add(new JButton("3"));
  21. panel3.add(new JButton("3"));
  22. panel3.add(new JButton("3"));
  23. container.add(panel);
  24. container.add(panel2);
  25. container.add(panel3);
  26. this.setVisible(true);
  27. this.setSize(500,500);
  28. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  29. }
  30. }

滚动条

JScrollPanel

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. /** * @Title: Test18JScrollPanel * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 21:54 */
  5. public class Test18JScrollPanel extends JFrame {
  6. public Test18JScrollPanel() {
  7. // 容器
  8. Container container = this.getContentPane();
  9. //文本域
  10. TextArea area = new TextArea(20,100);
  11. area.setText("欢迎来到 Java 的世界,这里是 GUI Swing 工具包的滚动条实现!\n" +
  12. " Java是目前最为广泛的网络编程语言。它具有简单,面向对象,稳定等特点。\n" +
  13. " 2.Java 语言简单是指这门语言既易学好用。不要将简单误解为这门语言很干瘪。\n" +
  14. "如果你学习过 C++语言,你会感觉 Java很眼熟,因为 Java中许多基本语句的语法和 C++一样\n" +
  15. "。如果从语言的简单性方面看...nhhhhhhhhhhhhhhh");
  16. // 添加一个Scroll 面板
  17. JScrollPane scrollPane = new JScrollPane(area);
  18. container.add(scrollPane);
  19. this.setVisible(true);
  20. this.setBounds(100,100,300,350);
  21. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  22. }
  23. public static void main(String[] args) {
  24. new Test18JScrollPanel();
  25. }
  26. }

如下图

在这里插入图片描述

按钮

  • 图片按钮

    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 {

    1. public Test19JButton() {
    2. Container container = this.getContentPane();
    3. // 将一个图片变成图标
    4. URL url = Test19JButton.class.getResource("1.png");
    5. ImageIcon imageIcon = new ImageIcon(url);
    6. // 把这个图标放在按钮上
    7. JButton button = new JButton();
    8. button.setIcon(imageIcon);
    9. button.setToolTipText("图片按钮");
    10. //add
    11. container.add(button);
    12. this.setVisible(true);
    13. this.setSize(50,30);
    14. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    15. }
    16. public static void main(String[] args) {
    17. new Test19JButton();
    18. }

    }

  • 单选按钮

将三个按钮放在一个组里,在组里只能有一个被选中

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.net.URL;
  5. /** * @Title: Test20Button * @Package * @Description: * @author: maze * @date 2020/10/20下午 22:49 */
  6. public class Test20Button extends JFrame{
  7. public Test20Button() throws HeadlessException {
  8. Container container = this.getContentPane();
  9. // 将一个图片变成图标
  10. URL url = Test19JButton.class.getResource("1.png");
  11. ImageIcon imageIcon = new ImageIcon(url);
  12. //单选框
  13. JRadioButton radioButton01 = new JRadioButton("男");
  14. JRadioButton radioButton02 = new JRadioButton("女");
  15. JRadioButton radioButton03 = new JRadioButton("未知");
  16. // 由于是单选框只能选择一个,分组,一个组中只能选择一个
  17. ButtonGroup group = new ButtonGroup();
  18. group.add(radioButton01);
  19. group.add(radioButton02);
  20. group.add(radioButton03);
  21. container.add(radioButton01,BorderLayout.CENTER);
  22. container.add(radioButton02,BorderLayout.NORTH);
  23. container.add(radioButton03,BorderLayout.SOUTH);
  24. this.setVisible(true);
  25. this.setSize(50,30);
  26. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  27. }
  28. public static void main(String[] args) {
  29. new Test20Button();
  30. }
  31. }

如下图

在这里插入图片描述

  • 复选按钮

去掉分组框,就变成复选按钮了

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.net.URL;
  5. /** * @Title: Test20Button * @Package * @Description: * @author: maze * @date 2020/10/20下午 22:49 */
  6. public class Test20Button extends JFrame{
  7. public Test20Button() throws HeadlessException {
  8. Container container = this.getContentPane();
  9. // 将一个图片变成图标
  10. URL url = Test19JButton.class.getResource("1.png");
  11. ImageIcon imageIcon = new ImageIcon(url);
  12. //单选框
  13. JRadioButton radioButton01 = new JRadioButton("写代码");
  14. JRadioButton radioButton02 = new JRadioButton("读书");
  15. JRadioButton radioButton03 = new JRadioButton("陪女朋友逛街");
  16. JRadioButton radioButton04 = new JRadioButton("打游戏");
  17. // 由于是单选框只能选择一个,分组,一个组中只能选择一个
  18. container.setLayout(new GridLayout(1,4));
  19. container.add(radioButton01);
  20. container.add(radioButton02);
  21. container.add(radioButton03);
  22. container.add(radioButton04);
  23. this.pack();
  24. this.setVisible(true);
  25. this.setSize(50,30);
  26. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  27. }
  28. public static void main(String[] args) {
  29. new Test20Button();
  30. }
  31. }

在这里插入图片描述

列表

  1. package JavaGUI;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. /** * @Title: Test21Combobox * @Package JavaGUI * @Description: * @author: maze * @date 2020/10/20下午 23:29 */
  5. public class Test21Combobox extends JFrame {
  6. public Test21Combobox() {
  7. Container container = this.getContentPane();
  8. JComboBox comboBox = new JComboBox();
  9. comboBox.addItem(null);
  10. comboBox.addItem("正在上映");
  11. comboBox.addItem("即将上映");
  12. comboBox.addItem("已下架");
  13. container.add(comboBox);
  14. this.setVisible(true);
  15. this.setSize(500,350);
  16. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17. }
  18. public static void main(String[] args) {
  19. new Test21Combobox();
  20. }
  21. }

在这里插入图片描述

列表框

  1. public class Test21Combobox extends JFrame {
  2. public Test21Combobox() {
  3. Container container = this.getContentPane();
  4. // 生成列表内容
  5. String[] contents = { "1","2","3"};
  6. // 列表中需要放入的内容
  7. JList jList = new JList(contents);
  8. container.add(jList);
  9. this.setVisible(true);
  10. this.setSize(500,350);
  11. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  12. }
  13. public static void main(String[] args) {
  14. new Test21Combobox();
  15. }
  16. }

文本框

  • 文本框

    public class Test21Combobox extends JFrame {

    1. public Test21Combobox() {
    2. Container container = this.getContentPane();
    3. JTextField field1 = new JTextField("hello");
    4. JTextField field2 = new JTextField("hello",20);
    5. container.add(field1,BorderLayout.NORTH);
    6. container.add(field2,BorderLayout.SOUTH);
    7. this.setVisible(true);
    8. this.setSize(500,350);
    9. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    10. }
    11. public static void main(String[] args) {
    12. new Test21Combobox();
    13. }

    }

  • 密码框

    public class Test21Combobox extends JFrame {

    1. public Test21Combobox() {
    2. Container container = this.getContentPane();
    3. JPasswordField passwordField = new JPasswordField();
    4. passwordField.setEchoChar('*');
    5. container.add(passwordField);
    6. this.setVisible(true);
    7. this.setSize(500,350);
    8. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    9. }
    10. public static void main(String[] args) {
    11. new Test21Combobox();
    12. }

    }

  • 文本域

    public class Test21Combobox extends JFrame {

    1. public Test21Combobox() {
    2. Container container = this.getContentPane();
    3. JTextArea textArea = new JTextArea(20, 50);
    4. textArea.setText("欢迎来到 Java GUI 系列的学习");
    5. //面板
    6. JScrollPane scrollPane = new JScrollPane(textArea);
    7. container.add(scrollPane);
    8. this.setVisible(true);
    9. this.setSize(500,350);
    10. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    11. }
    12. public static void main(String[] args) {
    13. new Test21Combobox();
    14. }

    }

到这里 GUI 的重点知识就差不多了

写一篇博客是,利用 GUI 编程实现一个贪吃蛇游戏

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读