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

狂神说java GUI笔记(二)Swing部分,第1张

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.JDialogd窗

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);
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存