java基础-swing图形界面-按钮

1.常用方法

方法描述
JButton()创建一个没有文本或图标的按钮
JButton(Icon icon)创建一个带图标的按钮
JButton(String text)创建一个带文本的按钮
JButton(Action a)创建一个按钮,其中的属性取自提供的 Action
JButton(String text, Icon icon)创建一个带有初始文本和图标的按钮

2.基本示例

package test.jdk.swing.jbutton;

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

public class TestJButton extends JFrame{

    //面板
    private JPanel jPanel;
    //什么都没有的按钮
    private JButton jButton;
    //带文字的按钮
    private JButton jButton01;
    //带图片的按钮
    private JButton jButton02;
    //带事件的按钮
    private JButton jButton03;
    //按钮方法使用
    private JButton jButton04;


    public TestJButton(){
        jPanel = new JPanel();
        //什么都没有的按钮
        jButton = new JButton();
        //带文字的按钮
        jButton01 = new JButton("测试按钮");
        //带图片的按钮
        //图片位置问题,图片应放在项目根目录下,项目名是test,这里图片的位置就是test/image/3.png
        //如果是多模块工程,放在父级目录下
        ImageIcon imageIcon = new ImageIcon("image/3.png");
        jButton02 = new JButton(imageIcon);
        jButton02.setText("图片按钮");
        //带事件的按钮
        jButton03 = new JButton(new ColorAction(Color.red));
        jButton03.setText("事件按钮");
        System.out.println(jButton03.getText());
        //按钮常用方法
        jButton04 = new JButton();
        jButton04.setText("按钮常用方法");
        jButton04.setIcon(imageIcon);
        jButton04.setSize(400, 400); //没用
        jButton04.setBounds(40, 40, 400, 400); //没用
        jButton04.setLocation(500, 500); //没用
        //JButton在FlowLayout、BorderLayout等一些布局中使用setSize()设置大小没效果,可以使用setPreferredSize方法进行设置
        //这里高度不够会显示不出文字
        jButton04.setPreferredSize(new Dimension(300,300));
        jButton04.setHorizontalTextPosition(SwingConstants.CENTER); //文本相对于图标的水平位置
        jButton04.setVerticalTextPosition(SwingConstants.BOTTOM); //文本相对于图片的垂直位置
        jButton04.setOpaque(false);//设置控件是否透明,true为不透明,false为透明
        jButton04.setContentAreaFilled(false);//设置图片填满按钮所在的区域
        jButton04.setMargin(new Insets(10, 10, 10, 10));//设置按钮边框和标签文字之间的距离
        jButton04.setFocusPainted(false);//设置这个按钮是不是获得焦点
        jButton04.setBorderPainted(false);//设置是否绘制边框
        jButton04.setBorder(null);//设置边框
        //按钮添加点击事件
        jButton04.addActionListener(e -> System.out.println("点击了"));

        //面板添加按钮
        jPanel.add(jButton);
        jPanel.add(jButton01);
        jPanel.add(jButton02);
        jPanel.add(jButton03);
        jPanel.add(jButton04);
        //添加面板
        add(jPanel);
    }

    /**
     * 显示方法
     */
    public void showFrame(){
        setTitle("测试图形界面程序");
        //设置窗口的大小
        setSize(800, 800);
        //设置距离屏幕左上角的距离
        setLocation(500, 100);
        //必须添加完对应的组件之后设置显示
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class ColorAction extends AbstractAction {

        public ColorAction(Color c) {
            putValue("color", c);
        }

        public ColorAction(String name, Icon icon, Color c) {
            putValue(Action.NAME, name);
            putValue(Action.SMALL_ICON, icon);
            putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
            putValue("color", c);
        }

        public void actionPerformed(ActionEvent event) {
            Color c = (Color) getValue("color");
            jButton03.setBackground(c);
        }
    }


    public static void main(String[] args) {
        TestJButton testJButton = new TestJButton();
        testJButton.showFrame();
    }


}

显示效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LouD_dm

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

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

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

打赏作者

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

抵扣说明:

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

余额充值