GUI基础编程

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

? Adore ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值