欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI基础编程

程序员文章站 2022-07-14 17:22:30
...

GUI基础编程 Day 06

标签

label:
new label("文本");
图标:
import javax.swing.*;
import java.awt.*;

//图标,需要实现类,继承Frame
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(){
        IconDemo iconDemo = new IconDemo(50, 50);

        //图标可以放在标签上,也可以放在按钮上
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

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

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

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

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

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

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

练习将按钮加入标签

图片:
import javax.swing.*;
import java.awt.*;

//图标,需要实现类,继承Frame
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(){
        IconDemo iconDemo = new IconDemo(50,50);

        //图标可以放在标签上,也可以放在按钮上
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

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

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

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

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

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

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

面板

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

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

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

        JPanel panel01 = new JPanel(new GridLayout(1, 3));
        JPanel panel02 = new JPanel(new GridLayout(1, 2));
        JPanel panel03 = new JPanel(new GridLayout(2, 1));
        JPanel panel04 = new JPanel(new GridLayout(3, 2));

        panel01.add(new JButton("1"));
        panel01.add(new JButton("1"));
        panel01.add(new JButton("1"));
        panel02.add(new JButton("2"));
        panel02.add(new JButton("2"));
        panel03.add(new JButton("3"));
        panel03.add(new JButton("3"));
        panel04.add(new JButton("4"));
        panel04.add(new JButton("4"));
        panel04.add(new JButton("4"));
        panel04.add(new JButton("4"));
        panel04.add(new JButton("4"));
        panel04.add(new JButton("4"));
        container.add(panel01);
        container.add(panel02);
        container.add(panel03);
        container.add(panel04);

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

    }

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

滚动框

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

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,500,600);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

按钮

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

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01(){
        Container container = this.getContentPane();
        container.setLayout(null);

        URL url = JButtonDemo01.class.getResource("工作人员 (1).png");
        Icon icon = new ImageIcon(url);

        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        button.setBounds(50,50,100,100);

        container.add(button);

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

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}
单选框按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame {

    public JButtonDemo02(){
        Container container = this.getContentPane();
        //将图片变为图标
        URL url = JButtonDemo02.class.getResource("工作人员 (1).png");
        Icon icon = new ImageIcon(url);

        //单选框
        JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton03 = new JRadioButton("JRadioButton03");

        //单选框只能选一个   分组    
        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(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
复选框按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo03 extends JFrame {

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

        URL url = JButtonDemo03.class.getResource("工作人员 (1).png");
        Icon icon = new ImageIcon(url);

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        JCheckBox checkBox03 = new JCheckBox("checkBox03");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.CENTER);
        container.add(checkBox03,BorderLayout.SOUTH);


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

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