简体   繁体   English

在新闻界停止JButton突出显示

[英]Stopping JButton highlighting on press

Any JButton that is pressed will seem to "highlight" itself when pressed like so: 任何按下的JButton在按下时都会“强调”自身:

按下按钮

I can't seem to find any way of disabling this. 我似乎无法找到任何禁用此功能的方法。

You can extend JButton class and design your own appearance or just override the default bahaviour as in this exemplary code: 您可以扩展JButton类并设计自己的外观,或者只是覆盖默认的bahaviour,如此示例代码中所示:

public class MyButton extends JButton {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isSelected()) {
            setBorder(BorderFactory.createEmptyBorder());
        } else {
            setBorder(BorderFactory.createLoweredBevelBorder());
        }
    }
}

我总是通过调用setFocusPainted(boolean b)来做到这一点:

btn.setFocusPainted(false);

Painting the background when pressed is a UI implementation, so you would need to change the UI. 按下时绘制背景是UI实现,因此您需要更改UI。

A simpler approach would be to create Icons of a specific color to add to your buttons. 一种更简单的方法是创建要添加到按钮的特定颜色的图标。 Something like: 就像是:

public class ColorIcon implements Icon
{
    private Color color;
    private int width;
    private int height;

    public ColorIcon(Color color, int width, int height)
    {
        this.color = color;
        this.width = width;
        this.height = height;
    }

    public int getIconWidth()
    {
        return width;
    }

    public int getIconHeight()
    {
        return height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }
}

Then you can display the text on top of the Icon by using: 然后,您可以使用以下方法在图标顶部显示文本:

JButton button = new JButton("1");
button.setIcon( new ColorIcon(Color.RED, 32, 32) );
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.CENTER);
button.setMargin( new Insets(0, 0, 0, 0) );

There are a number of ways you might achieve this... 你可以通过多种方式实现这一目标......

You Could... 你可以...

Override paintComponent and implement you own paint logic. 覆盖paintComponent并实现自己的绘制逻辑。 This is kind of dangerous and now means that for each state change you want to modify will either require a new JButton based class or some other serious of flags to implement. 这有点危险,现在意味着对于要修改的每个状态更改,要么需要新的基于JButton的类,要么需要其他一些严格的标志来实现。 It's also possible that this could effect other look and feels... 这也可能影响其他外观和感觉......

You Could... 你可以...

Create your own ButtonUI , which would normally be the preferred way, but it's not an insignificant amount of work and you'd need one for each platform you wanted to support 创建自己的ButtonUI ,这通常是首选的方式,但它不是一个微不足道的工作量,你需要一个你想要支持的每个平台

You Could... 你可以...

Use the icon property of the button to "simulate" the button boundaries. 使用按钮的icon属性“模拟”按钮边界。 This is preferred solution (over customising the the painting process) as it's easy to apply and doesn't require a specialised button to achieve. 这是首选的解决方案(超过定制绘画过程),因为它易于应用,并且不需要专门的按钮来实现。 It also overcomes some of the issues of how buttons are painted across different platforms (as not all buttons use the background color property the same) 它还克服了一些关于如何在不同平台上绘制按钮的问题(因为并非所有按钮都使用相同的background颜色属性)

You Could... 你可以...

Define your own ButtonModel which could ignore certain state changes (like pressed or rollover). 定义您自己的ButtonModel ,它可以忽略某些状态更改(如按下或翻转)。

This is a preferred solution as it works with the current look and feel to achieve your results. 这是一个首选的解决方案,因为它与当前的外观和感觉一起实现您的结果。

FixState

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestButton {

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

    public TestButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JButton normal = createButton("Normal", Color.RED);
            JButton fixed = createButton("Fixed", Color.BLUE);
            fixed.setModel(new FixedStateButtonModel());
            setLayout(new GridLayout(1, 0));
            add(normal);
            add(fixed);
        }

        protected JButton createButton(String text, Color background) {
            JButton btn = new JButton(text);
            btn.setFocusPainted(false);
            btn.setBackground(background);
            btn.setForeground(Color.WHITE);
            return btn;
        }

    }

    public class FixedStateButtonModel extends DefaultButtonModel    {

        @Override
        public boolean isPressed() {
            return false;
        }

        @Override
        public boolean isRollover() {
            return false;
        }

        @Override
        public void setRollover(boolean b) {
            //NOOP
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM