簡體   English   中英

Java Nimbus L&F問題工具欄JButton

[英]Java Nimbus L&F issue Toolbar JButton

當我在工具欄中使用JToolbar和JButton時,我對Nimbus外觀有問題。 如果我使用Metal,我只遇到Nimbus的問題,那么按鈕會正確顯示。

如果單擊或將鼠標懸停在工具欄中,則按鈕僅可見;而在金屬中,按標准可見。 我的問題或疑問,這個問題有解決方案嗎?

示例代碼:

按鈕:test,test1在工具欄中,不可見,按鈕test2在工具欄中,但看起來不正常,我希望按鈕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();
        }
    }
}

您的問題似乎是不同的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM