【问题标题】:Java Nimbus L&F issue Toolbar JButtonJava Nimbus L&F 问题工具栏 JButton
【发布时间】:2014-09-09 15:21:29
【问题描述】:

当我在工具栏中使用 JToolbar 和 JButton 时,我遇到了 Nimbus 外观问题。 如果我使用 Metal,我只有 Nimbus 的问题,按钮显示正确。

ToolBar 中的按钮仅在被单击或鼠标悬停时才可见,而在 Metal 中它们是标准可见的。我的问题或疑问,是否有解决此问题的方法?

示例代码:

Button: test, test1 在工具栏中,不可见,Button test2 不在工具栏中,看起来很正常,我希望 Button test, test1 看起来像 test2,只是它们在工具栏中。

import java.awt.FlowLayout;

import javax.swing.*;

public class nimbus extends JFrame{

    public nimbus()
    {
        //setLook("javax.swing.plaf.metal.MetalLookAndFeel");
        setLook("javax.swing.plaf.nimbus.NimbusLookAndFeel");

        FlowLayout fl = new FlowLayout();
        fl.setAlignment(fl.LEFT); 

        this.setLayout(fl);
        this.setSize(100, 200);

        JToolBar tbar = new JToolBar();
        tbar.setLayout(fl);
        tbar.setSize(800, 40);
        tbar.setFloatable(false);

        JButton btn = new JButton("test");
        btn.setSize(50, 23);
        tbar.add(btn);

        btn = new JButton("test1");
        btn.setSize(50, 23);
        tbar.add(btn);

        this.add(tbar);

        btn = new JButton("test2");
        btn.setSize(50, 23);
        this.add(btn);
    }

    public static void main(String[] args){
        nimbus n = new nimbus();
        n.setVisible(true);
    }
    public void setLook(String look)
    {

        try {
            UIManager.setLookAndFeel(look);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java swing look-and-feel nimbus jtoolbar


    【解决方案1】:

    您的问题似乎是不同的 LookAndFeel 行为。
    特别是,NimbusLookAndFeel-ToolbarUI 的实现与 MetalLookAndFeel-ToolbarUI 不同。

    正如我在另一个线程中发布的那样,MetalLookAndFeel-ToolbarUI impl 看起来像:

    public void update(Graphics g, JComponent c) {
       AbstractButton button = (AbstractButton)c;
       if ((c.getBackground() instanceof UIResource) &&
             button.isContentAreaFilled() && c.isEnabled()) {
           ButtonModel model = button.getModel();
           if (!MetalUtils.isToolBarButton(c)) {
               if (!model.isArmed() && !model.isPressed() &&
                       MetalUtils.drawGradient(
                       c, g, "Button.gradient", 0, 0, c.getWidth(),
                       c.getHeight(), true)) {
                   paint(g, c);
                   return;
               }
           }
      ...
    

    看看:MetalUtils.isToolBarButton(c)

    要改变这种行为,恐怕你必须创建自己的 ToolbarUI

    【讨论】:

    • 感谢您的回答!!我担心我需要编写自己的工具栏,但在此之前,我想确定没有其他(更简单)的方法。
    猜你喜欢
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多