java - setBorderPainted(boolean) 不起作用

标签 java swing border jbutton imageicon

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/

相关文章:

html - 通过文本以及围绕文本创建边框

html - 悬停的边框使文字看起来像是在颤抖

java - 如何构建依赖于另一个项目的 Gradle 项目,但使本地路径不受版本控制?

java - JButton 上的 itemStateChanged

java - Swing 绘制在不正确的坐标上

java - JList渲染不可见 "selection marker"

css - 菜单项之间不需要的透明空间(边框不起作用)

java - 创建回文标识符

java - 如何在java中将这个程序放入循环中?

java - 可空的 Gson Retrofit 字段