狂神说java GUI笔记(二)Swing部分

2.Swing

1.窗口 JFrame

import javax.swing.*;
import java.awt.*;
import java.util.ConcurrentModificationException;
import java.util.jar.JarEntry;

public class TestJFrame {
    public static void main(String[] args) {
        new TestJFrame().init();
    }
    public void init(){
        JFrame jFrame=new JFrame("JFrame");
        jFrame.setVisible(true);
        jFrame.setBounds(200,200,200,200);
        //相较于Frame,JFrame引入容器Container  背景颜色由Container决定
        Container container=new Container();
        container.setBackground(Color.MAGENTA);
        //添加标签
        JLabel jLabel=new JLabel("Jlabel");
        jFrame.add(jLabel);
        //标签居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}


2.JDialog弹窗

JDialog默认有关闭事件

package GUI__SwingLesson;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestJDialog extends JFrame {
    public static void main(String[] args) {
        new TestJDialog();
    }
    public TestJDialog(){
        this.setVisible(true);
        this.setBounds(500,500,400,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);//使得下面的 bt1.setBounds参数为相对位置
        Button bt1=new Button("java learning");
        bt1.setBounds(200,200,100,100);
        Container contentPane = this.getContentPane();
        contentPane.add(bt1);
        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
    }
}
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(200,200,200,200);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.标签JLabel.图标

label

new JLabel();

icon图标

import javax.swing.*;
import java.awt.*;
import java.net.URL;

//图标需要实现类
public class IconDemo extends JFrame implements Icon{
    private int width;
    private int height;
    public IconDemo(){}
    public IconDemo(int width,int height){
        this.width=width;
        this.height=height;
    }
    public void init(){
        //图片作为图标
        URL url = IconDemo.class.getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JLabel imageLabel=new JLabel(imageIcon);
        imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        IconDemo iconDemo=new IconDemo(20,20);
        //图标可放在按钮,标签上
       JButton button=new JButton("icon",iconDemo);
        JLabel label=new JLabel("label",iconDemo,SwingConstants.CENTER);
        Container container=getContentPane();
        container.setLayout(new FlowLayout());
        container.add(button);
        container.add(label);
        container.add(imageLabel);
        this.setBounds(500,500,500,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,20,20);
    }

    @Override
    public int getIconWidth() {
        return width;
    }

    @Override
    public int getIconHeight() {
        return height;
    }
}

4.面板

JPanel 和JScrollPane(可滚动面板)

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public static void main(String[] args) {
        new JPanelDemo();
    }
    public JPanelDemo(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new GridLayout(1,6,6,6));
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(2,4,5,5));
        Button bt1=new Button("1");
        Button bt2=new Button("2");
        Button bt3=new Button("3");
        Button bt4=new Button("4");
        Button bt5=new Button("5");
        Button bt6=new Button("6");
        Button bt7=new Button("7");
        Button bt8=new Button("8");
        jPanel.add(bt1);
        jPanel.add(bt2);
        jPanel.add(bt3);
        jPanel.add(bt4);
        jPanel.add(bt5);
        jPanel.add(bt6);
        jPanel.add(bt7);
        contentPane.add(jPanel);
        contentPane.add(bt8);
        this.setBounds(500,500,400,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollPanelDemo();
    }
    public JScrollPaneDemo(){

        //文本域
        JTextArea jTextArea = new JTextArea(20,20);
        jTextArea.setText("可向下滚动哦");
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        Container container = this.getContentPane();
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(500,500,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

5.按钮

图片按钮 JButton
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJButton extends JFrame {
    public TestJButton(){
        Container container = this.getContentPane();

        URL url = this.getClass().getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片按钮");
        container.add(jButton);

        this.setBounds(200,300,500,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJButton();
    }
}

单选按钮 JRadioButton

引入分组概念,ButtonGroup

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJRadioButton extends JFrame {
    public TestJRadioButton(){
        Container container = this.getContentPane();

        JRadioButton jRadioButton1 = new JRadioButton("1号");
        JRadioButton jRadioButton2 = new JRadioButton("2号");
        JRadioButton jRadioButton3 = new JRadioButton("3号");
        //引入分组的概念,一个组只能选一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);
        this.setBounds(200,300,500,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJRadioButton();
    }
}

复选按钮 JCheckBox
import javax.swing.*;
import java.awt.*;

public class TestJCheckBox extends JFrame {
    public TestJCheckBox(){
        Container container = this.getContentPane();

        JCheckBox jCheckBox1 = new JCheckBox("1");
        JCheckBox jCheckBox2 = new JCheckBox("2");
        JCheckBox jCheckBox3 = new JCheckBox("3");
        container.setLayout(new FlowLayout());
        container.add(jCheckBox1);
        container.add(jCheckBox2);
        container.add(jCheckBox3);


        this.setBounds(200,300,500,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestJCheckBox();
    }
}

6.列表

下拉框 JComboBox

应用场景:选择一些单个选项(单个选项有两个,建议用JRadioButton)

import javax.swing.*;
import java.awt.*;

public class JComboboxDemo extends JFrame {
    public static void main(String[] args) {
        new JComboboxDemo();
    }
    public JComboboxDemo(){
        Container container = this.getContentPane();

        JComboBox<Object> jComboBox = new JComboBox<>();
        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("已下映");
        jComboBox.addItem("即将上映");
        container.add(jComboBox);

        this.setBounds(500,600,800,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

列表框 JList

应用场景:展示信息,一般为动态扩容

import javax.swing.*;
import java.awt.*;

public class JListDemo extends JFrame {
    public JListDemo(){
        Container container = this.getContentPane();
        //准备列表中要展示的内容
        String [] str={"11111111","22222222222222","333333333"};

        JList<Object> jList = new JList<>(str);
        container.add(jList);

        this.setBounds(500,600,800,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JListDemo();
    }
}

7.文本框

文本框 JTextField
import javax.swing.*;
import java.awt.*;

public class JTestFieldDemo extends JFrame {
    public JTestFieldDemo(){
        Container container = this.getContentPane();

        JTextField jTextField = new JTextField("22222222222", 25);
        container.add(jTextField,BorderLayout.CENTER);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(500,500,600,600);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new JTestFieldDemo();
    }
}
密码框 JPassWordField
import javax.swing.*;
import java.awt.*;

public class JPassWordDemo extends JFrame {
    public JPassWordDemo(){
        Container container = this.getContentPane();
        JPasswordField jPasswordField = new JPasswordField(20);
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);
        this.setVisible(true);
        this.setBounds(500,500,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JPassWordDemo();
    }
}
文本域 JTextArea
import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollPanelDemo();
    }
    public JScrollPaneDemo(){

        //文本域
        JTextArea jTextArea = new JTextArea(20,20);
        jTextArea.setText("可向下滚动哦");
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        Container container = this.getContentPane();
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(500,500,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 狂神Java培训领域中的知名人物,他所开设的Java课程备受关注和好评。在Java学习过程中,配套笔记是至关重要的。狂神Java配套笔记,就是他自己编写的一份针对Java初学者的笔记。这份笔记内容详实,包括Java基础语法、面向对象编程、数据库操作等多个方面。并且,狂神还会根据学员的反馈及时修改和完善这份笔记Java配套笔记对于Java学习的初学者来,是一份非常好的辅助资料。通过这份笔记,初学者可以系统地学习Java编程涉及到的各个方面,同时也可以在学习过程中及时记录下重要的知识点,方便日后复习和查询。同时,这份笔记编写者是一位经验丰富的Java教育者,他对Java的理解和知识点的讲解都非常到位和深入浅出。 总之,Java配套笔记是一份非常重要的学习资料,它不仅可以帮助初学者更好地学习Java编程,还可以对Java学习者在日后的实际工作中提供有效的参考和帮助。如果你想学好Java,一份好的Java配套笔记绝对是必不可少的。 ### 回答2: 狂神Java配套笔记是非常好的学习资料,对于刚学习Java的初学者来尤其有用。这些笔记详细介绍了Java编程的一些基础知识,例如数据类型、变量、循环结构、方法等。而随着学习的深入,笔记也逐渐开始讲解Java中的高级特性,例如面向对象编程、异常处理等。 狂神Java配套笔记的另一个优点是非常易于理解和学习。这些笔记使用了简单易懂的语言和生动的示例来阐述概念,使得学习者可以快速入门。此外,笔记中还包含了大量的练习题,可以帮助学习者巩固所学知识和加深理解。 除了配合视频课程使用,狂神Java配套笔记也可以作为一个独立的学习资料来使用。学习者可以通过自学的方式来学习Java编程,掌握Java编程的基础知识和实践技能。 总之,狂神Java配套笔记是一份非常优秀的学习资料,无论是初学者还是有一定Java基础的学习者都可以从中受益。学习者只需要按照笔记的步骤一步一步地学习,就可以轻松掌握Java编程的基础知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值