0

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

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

示例代码:

按钮:测试,test1 在工具栏中,不可见,按钮 test2 不在工具栏中,看起来很正常,我希望按钮测试,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();
        }
    }
}
4

1 回答 1

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

于 2014-09-09T21:29:18.327 回答