GUI标签,面板,对话框

本文详细介绍了Swing库中的GUI组件,包括文字标签、图标、图片展示,以及如何创建消息提示对话框。同时,讨论了JPanel的使用,以及如何在面板中添加滚动框JScrollPane。此外,还涵盖了不同类型的按钮,如图片按钮、单选按钮和复选按钮的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swing

标签

  1. 文字标签
new JLable("xxx")
  1. 图标
package com.yuecheng.guistudy.Demo01;

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

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(20,20);
        //图标放在标签上,也可以放在按钮上
        JLabel icontest = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
        //获取容器
        Container contentPane = getContentPane();
        //将标签放入容器
        contentPane.add(icontest);
        this.setVisible(true);
        this.setSize(300,300);
        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,width,height);
    }

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

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

  1. 图片
package com.yuecheng.guistudy.Demo01;

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

public class Demo02 extends JFrame {
    public Demo02(){
        JLabel jLabel = new JLabel("imageIcon");
        //获取图片地址
        URL url = Demo02.class.getResource("手机2.png");
        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(500,500);

    }

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

消息提示对话框

package com.yuecheng.guistudy.Demo01;

import javax.swing.*;

public class Demo03 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setBounds(0,0,500,500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //showMassageDialog的用法
        //JOptionPane.showMessageDialog(null,"友情提示");
        //JOptionPane.showMessageDialog(null,"提示消息","标题",JOptionPane.WARNING_MESSAGE);
        //JOptionPane.showMessageDialog(null,"提示消息","标题",JOptionPane.ERROR_MESSAGE);

        String n = JOptionPane.showInputDialog("请输入的名字","彭于晏");
        JOptionPane.showConfirmDialog(null,"你真的是"+n+"?");
    }
}

面板

JPanel

package com.yuecheng.guistudy.Demo01;

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

public class JpanelDemo01 extends JFrame{

    public JpanelDemo01(){
        //设置容器
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new GridLayout(2,1,10,10));//后面两个是上下间距
        //new出面板,并设置布局
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel1 = new JPanel(new GridLayout(1,2));
        JPanel jPanel2= new JPanel(new GridLayout(2,1));
        JPanel jPanel3 = new JPanel(new GridLayout(1,3));
        //将按钮放入吗,面板
        jPanel.add(new JButton("11"));
        jPanel.add(new JButton("11"));
        jPanel.add(new JButton("11"));
        jPanel1.add(new JButton("22"));
        jPanel1.add(new JButton("22"));
        jPanel2.add(new JButton("33"));
        jPanel2.add(new JButton("33"));
        jPanel3.add(new JButton("44"));
        jPanel3.add(new JButton("44"));
        jPanel3.add(new JButton("44"));


        //设置可见
        this.setSize(500,500);
        this.setVisible(true);
        //关闭事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       //将面板添加入容器
        contentPane.add(jPanel);
        contentPane.add(jPanel1);
        contentPane.add(jPanel2);
        contentPane.add(jPanel3);
    }
    public static void main(String[] args) {
        //new出该类
         new JpanelDemo01();
    }
}

滚动框JScroll

package com.yuecheng.guistudy.lesson03;

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

public class Demo01  extends JFrame {
    public Demo01(){
        Container contentPane = this.getContentPane();
        TextArea textArea = new TextArea(20,50);
        //设置文本框默认文本
        textArea.setText("欢迎学习java");
        //contentPane.add(textArea);
        //new一个面板jscroll
        JScrollPane jScrollPane = new JScrollPane(textArea);
        contentPane.add(jScrollPane);

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

    }
    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();

    }
}

按钮

  • 图片按钮
package com.yuecheng.guistudy.lesson03;

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

public class Demo03 extends JFrame {

    public Demo03(){
        Container contentPane = this.getContentPane();
        URL resource = Demo03.class.getResource("手机3.png");
        //图片变为一个图标
        Icon icon =new ImageIcon(resource);

        //吧2图标放到按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        contentPane.add(button);
        this.setSize(600,600);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Demo03();
    }
}
  • 单选按钮
package com.yuecheng.guistudy.lesson03;

import com.yuecheng.oop.Demo05.B;

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

public class Demo04 extends JFrame {
        public Demo04(){
            Container contentPane = this.getContentPane();
            URL resource = Demo03.class.getResource("手机3.png");
            //图片变为一个图标
            Icon icon =new ImageIcon(resource);

            JRadioButton jRadioButton01 = new JRadioButton("我是一个单选框");
            JRadioButton jRadioButton02  = new JRadioButton("我是一个");
            JRadioButton jRadioButton03  = new JRadioButton("我是");
            //由于单选框只能选择一个,所以经常会进行分组
            ButtonGroup buttonGroup = new ButtonGroup();
            buttonGroup.add(jRadioButton01);
            buttonGroup.add(jRadioButton02);
            buttonGroup.add(jRadioButton03);

            contentPane.add(jRadioButton01,BorderLayout.CENTER);
            contentPane.add(jRadioButton02,BorderLayout.NORTH);
            contentPane.add(jRadioButton03,BorderLayout.SOUTH);


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

        }

    public static void main(String[] args) {
        new Demo04();
    }
}
  • 复选按钮
package com.yuecheng.guistudy.lesson03;

import com.yuecheng.oop.Demo05.B;

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

public class Demo04 extends JFrame {
        public Demo04(){
            Container contentPane = this.getContentPane();
            URL resource = Demo03.class.getResource("手机3.png");
            //图片变为一个图标
            Icon icon =new ImageIcon(resource);

            //多选框的操作
            Checkbox checkbox01 = new Checkbox("checkbox01");
            Checkbox checkbox02 = new Checkbox("checkbox02");

            contentPane.add(checkbox01,BorderLayout.NORTH);
            contentPane.add(checkbox02,BorderLayout.SOUTH);


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

        }

    public static void main(String[] args) {
        new Demo04();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玥骋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值