作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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);
}
});
}
}
此外,setSize
和 setBounds
的使用很可怕,您应该让布局管理器处理这些细节。
我不知道为什么您要在按钮上使用 MouseListener
,但在触发按钮时获取通知的首选方法是通过 ActionListener
接口(interface),作为一个按钮,我通过数字方式触发,而不仅仅是鼠标单击
仔细看看How to Use Buttons了解更多详情
关于java - setBorderPainted(boolean) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593321/
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
JPanel droppan = new JPanel (); droppan.setLayout (new FlowLayout ()); bi1 = new ImageIcon (""); b1
我是一名优秀的程序员,十分优秀!