在Java Swing中更改JButton的边框颜色,以保留插图 [英] Change Border Color of a JButton in Java Swing preserving the insets

查看:126
本文介绍了在Java Swing中更改JButton的边框颜色,以保留插图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改Java Swing中JButton组件的边框颜色.

I want to change the border color of a JButton component in Java Swing.

我尝试了以下操作:

package com.example.test;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JFrame {

    public Test() {

        JPanel panel = new JPanel();
        JButton button1 = new JButton("Test Button 1");
        JButton button2 = new JButton("Test Button 2");
        button2.setBorder(BorderFactory.createLineBorder(Color.RED));

        panel.add(button1);
        panel.add(button2);

        this.add(panel);


        setSize(400, 400);
        setVisible(true);

    }

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        Test t = new Test();
    }
}

这会生成两个按钮,在button2组件上,我尝试更改边框颜色,但它删除了填充.无论如何,是否可以保留标准JButton的原始插图并只是更改颜色?

This generates two buttons, on button2 component I try to change border color but it removes the padding. Is there anyway to preserve the original insets of a standard JButton and just change the color?

注意:我假设在分配新的Border时将移除插图.但是我对此不是100%肯定.

Note: I assume that the insets are being removed when assigning the new Border. But I am not 100% sure about it.

推荐答案

使用CompoundBorder代替创建LineBorder:

button2.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.RED, 1), 
            BorderFactory.createEmptyBorder(
                button1.getBorder().getBorderInsets(button1).top, 
                button1.getBorder().getBorderInsets(button1).left, 
                button1.getBorder().getBorderInsets(button1).bottom, 
                button1.getBorder().getBorderInsets(button1).right)));

我把BorderInsets换成button1,以便它们的大小相同.

I took the BorderInsets for the button1 so that both of them have the same size.

我的答案基于@MadProgrammer针对此问题

My answer is based on @MadProgrammer answer for this question

顺便说一句,不要扩展JFrame,而是为其创建一个实例,如果您确实需要扩展组件,可以将其作为JPanel:

Btw don't extend JFrame, create an instance of it instead and if you really need to extend a component, be it a JPanel: Extends JFrame vs. creating it inside the program

也别忘了打电话

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

在您的JFrame实例上,以便您的程序在关闭时终止.

On your JFrame instance, so that your program terminates when you close it.

您还错过了将程序放在EDT上的信息,请参见此答案

And also you missed to place your program on the EDT, see more of this on this answer

这篇关于在Java Swing中更改JButton的边框颜色,以保留插图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆