gpt4 book ai didi

java - 如何使 Jbutton 周围的边框变粗?

转载 作者:行者123 更新时间:2023-12-01 07:23:57 26 4
gpt4 key购买 nike

我正在构建的 GUI 界面上有以下 JButton。

http://i.imgur.com/MucqvGj.png

我想让按钮周围的边框更粗,这样它就会从背景中脱颖而出。在 Java 中可以做到这一点吗?

最佳答案

您可以简单地使用LineBorder

JButton btn = ...;
btn.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));

看看How to Use Borders了解更多细节和想法

根据模型状态更新边界状态

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

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

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

protected static final Border NORMAL_BORDER = BorderFactory.createLineBorder(Color.BLACK, 4);
protected static final Border ROLLOVER_BORDER = BorderFactory.createLineBorder(Color.RED, 4);

public TestPane() {

JButton btn = new JButton("Click me!");
btn.setContentAreaFilled(false);
btn.setBorder(NORMAL_BORDER);
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
btn.setBorder(ROLLOVER_BORDER);
} else {
btn.setBorder(NORMAL_BORDER);
}
}
});

setLayout(new GridBagLayout());
add(btn);

}

}

}

关于java - 如何使 Jbutton 周围的边框变粗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com