gpt4 book ai didi

java - setBorderPainted(boolean) 不起作用

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

JPanel droppan = new JPanel ();

droppan.setLayout (new FlowLayout ());


bi1 = new ImageIcon ("");
b1 = new JButton (bi1);
//b1.setBorderPainted (false);
b1.setBorder (BorderFactory.createEmptyBorder (2,3,2,3));
b1.setContentAreaFilled (false);
b1.setSize (100,38);
//b1.setBounds(3,3,100,38);

droppan.add(b1);
b1.addMouseListener (this);

add(droppan);

这实际上是一排按钮,但上面的代码只是第一个按钮的片段。最初,第一个按钮有一个边框,除非我单击另一个按钮,然后该按钮现在有边框,就好像它“转移”,如果你明白我的意思的话。

我做错了什么?setborderpainted 是一个注释,因为即使它不在注释中,边框仍然存在

这是框架内的面板

最佳答案

我“认为”你所说的是用于显示哪个按钮具有焦点的焦点边框...

尝试使用b1.setFocusPainted(false);代替

例如...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonRows {

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

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

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

for (int index = 0; index < 10; index++) {
JButton b1 = new JButton(Integer.toString(index));
b1.setFocusPainted(false);
b1.setBorder(BorderFactory.createEmptyBorder(2, 3, 2, 3));
b1.setContentAreaFilled(false);

frame.add(b1);
}

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

此外,setSizesetBounds 的使用很可怕,您应该让布局管理器处理这些细节。

我不知道为什么您要在按钮上使用 MouseListener,但在触发按钮时获取通知的首选方法是通过 ActionListener 接口(interface),作为一个按钮,我通过数字方式触发,而不仅仅是鼠标单击

仔细看看How to Use Buttons了解更多详情

关于java - setBorderPainted(boolean) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593321/

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