java之Swing学习总结

Swing

Swing是GUI(图形用户界面)开发工具包,内容有很多,这里会分块编写,但在进阶篇中只编写Swing中的基本要素,包括容器、组件和布局等,更深入的内容这里就不介绍了。想深入学习的朋友们可查阅有关资料或图书,比如《Java Swing图形界面开发与案例详解》——清华大学出版社。
早期的AWT(抽象窗口工具包)组件开发的图形用户界面,要依赖本地系统,当把AWT组件开发的应用程序移植到其他平台的系统上运行时,不能保证其外观风格,因此AWT是依赖于本地系统平台的。而使用Swing开发的Java应用程序,其界面是不受本地系统平台限制的,也就是说Swing开发的Java应用程序移植到其他系统平台上时,其界面外观是不会改变的。但要注意的是,虽然Swing提供的组件可以方便开发Java应用程序,但是Swing并不能取代AWT,在开发Swing程序时通常要借助与AWT的一些对象来共同完成应用程序的设计。

1. 窗口,面板

注意:该窗口默认有关闭事件

public class JFrameDemo {

    public void init(){
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.cyan);
        JLabel label = new JLabel("欢迎来看我的作业/笑哭");

        jf.add(label);

        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}

结果:
在这里插入图片描述

2.弹窗

小段代码:

public class DialogDemo extends JFrame {

    public DialogDemo(){
        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 DialogDemo();
    }
}
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new Label("没错,刘恒就是傻逼"));
    }
}

结果:
在这里插入图片描述

3. 标签

小段代码:

public class ImageIconDemo extends JFrame {

    public ImageIconDemo(){
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("tx.jpg");

        ImageIcon imageIcon = new ImageIcon(url); // //命名不要冲突了
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,200);
    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

4. 面板

小段代码:

public class JPanelDemo extends JFrame {

    public JPanelDemo() {
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10)); 


        JPanel panel1 = new JPanel(new GridLayout(1,3));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(2,1));
        JPanel panel4 = new JPanel(new GridLayout(3,2));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));


        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);
        this.setVisible(true);
        this.setSize(500,500);

    }

    public static void main(String[] args) {
        new JPanelDemo();
    }
}

结果:
在这里插入图片描述

5. 按钮

将一个图片设置为按钮

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01() {
        Container container = this.getContentPane();
        URL resource = JButtonDemo01.class.getResource("00005.jpg");
        Icon icon = new ImageIcon(resource);


        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        container.add(button);
        this.setVisible(true);
        this.setSize(500,300);

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

有单选按钮,也有多选按钮。

6. 列表
  • 下拉框:展示信息,一般是动态扩容。

显示电影的放映情况

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.setSize(500,350);
    }

    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}
  • 列表框
public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02() {

        Container container = this.getContentPane();

        Vector contents = new Vector();
        JList jList = new JList(contents);

        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");


        container.add(jList);


        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}

用于一些单个的选项

7. 文本框
public class TestTextDemo01  extends JFrame {
    public TestTextDemo01() {

        Container container = this.getContentPane();

        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);

        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
  • 密码框
public class TestTextDemo03  extends JFrame {
    public TestTextDemo03() {

        Container container = this.getContentPane();
        JPasswordField passwordField = new JPasswordField(); 
        passwordField.setEchoChar('*');

        container.add(passwordField);

        this.setVisible(true);
        this.setSize(500,350);
    }

    public static void main(String[] args) {
        new TestTextDemo03();
    }
}
  • 文本域
public class JScrollDemo extends JFrame {

    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎来到我的作业");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        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 JScrollDemo();
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习Java Swing可以通过以下步骤进行: 1. 了解Swing的基本概念和特点。Swing是一个用于开发Java应用程序用户界面的开发工具包,它是由纯Java实现的,并且不依赖于操作系统的支持。与AWT组件相比,Swing提供了更丰富、灵活的功能和模块化组件,可以创建优雅的用户界面。 2. 学习Swing的组件和包。Swing提供了近100个类和几十个接口,主要包含在javax.swing包中。除了JTableHeader类和JTextComponent类分别在swing.table包和swing.text包中,几乎所有的Swing组件都在javax.swing包中。此外,还有一些其他的相关包,如javax.swing.event、javax.swing.table、javax.swing.tree、javax.swing.filechooser、javax.swing.border等,它们提供了不同类型的组件和功能。 3. 熟悉Swing的事件处理机制。Swing的事件处理机制与AWT的事件处理机制类似,都是通过事件类和监听器接口来实现。了解如何使用Swing的事件处理机制可以帮助你对用户界面的交互做出响应。 4. 编写基于Swing的应用程序GUI。你可以创建一个继承自javax.swing.JFrame类的JFrame对象作为应用程序的主窗口。在构造函数中设置窗体的属性,如窗体的名称、大小、可见性和关闭操作。然后,你可以在窗体中添加各种Swing组件,如按钮、文本框、标签等,以构建你想要的用户界面。 下面是一个使用继承javax.swing.JFrame类创建JFrame对象的示例代码: ```java import javax.swing.JFrame; import javax.swing.WindowConstants; public class JFrameDemo extends JFrame { public JFrameDemo() { super("JFrameDemo"); // 设置窗体名称 this.setSize(500, 500); // 设置窗体宽高 this.setVisible(true); // 设置窗体显示 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 设置窗体关闭操作 } public static void main(String\[\] args) { new JFrameDemo(); // 创建JFrameDemo对象 } } ``` 通过学习Swing的基本概念、组件和包,以及掌握Swing的事件处理机制,你可以开始编写基于Swing的应用程序GUI。 #### 引用[.reference_title] - *1* [Java Swing学习](https://blog.csdn.net/weixin_44247784/article/details/102767655)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JavaSwing学习总结](https://blog.csdn.net/qq_27862695/article/details/48036983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java学习——Swing](https://blog.csdn.net/Asugsal/article/details/88825420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值