java 按钮边框颜色,在Java Swing中更改JButton的边框颜色,以保留插图

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

I have tried the following:

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();

}

}

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?

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

b31a0543a90ba6fe492c60fcccdd232e.png

解决方案

Instead of creating a LineBorder, use a CompoundBorder:

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)));

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

My answer is based on @MadProgrammer answer for this question

ad6f0c5c4bf7b53c62fa6ba149f68824.png

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

Also don't forget to call

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值