Java Swing中的统一工具栏工作不正常

Java Swing中的统一工具栏工作不正常,java,macos,swing,user-interface,Java,Macos,Swing,User Interface,我遵循了这个教程:在mac上创建一个本地外观的工具栏。问题可能是我没有正确地将其添加到JFrame中,或者可能误解了什么 该程序的工作方式应该是在工具栏上添加一个面板(或者在它下面,我想-它不清楚)。以防混淆:统一工具栏是常规工具栏,内部也只有按钮 这就是is的外观: 这就是它的外观:(我使用不同外观的按钮应该没关系吧?) 统一工具栏面板的代码: package gui; import java.awt.Color; import java.awt.Window; import javax

我遵循了这个教程:在mac上创建一个本地外观的工具栏。问题可能是我没有正确地将其添加到JFrame中,或者可能误解了什么

该程序的工作方式应该是在工具栏上添加一个面板(或者在它下面,我想-它不清楚)。以防混淆:统一工具栏是常规工具栏,内部也只有按钮

这就是is的外观:

这就是它的外观:(我使用不同外观的按钮应该没关系吧?)

统一工具栏面板的代码

package gui;
import java.awt.Color;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.Borders;

public class UnifiedToolbarPanel extends JPanel {

    public static final Color OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR =
            new Color(64, 64, 64);
    public static final Color OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR =
            new Color(135, 135, 135);    

    public UnifiedToolbarPanel() {
        // make the component transparent
        setOpaque(false);
        // create an empty border around the panel
        // note the border below is created using JGoodies Forms
        setBorder(Borders.createEmptyBorder("3dlu, 3dlu, 1dlu, 3dlu"));
    }



    @Override
    public Border getBorder() {
        Window window = SwingUtilities.getWindowAncestor(this);
        return window != null && window.isFocused()
                ? BorderFactory.createMatteBorder(0,0,1,0,
                        OS_X_UNIFIED_TOOLBAR_FOCUSED_BOTTOM_COLOR)
                : BorderFactory.createMatteBorder(0,0,1,0,
                       OS_X_UNIFIED_TOOLBAR_UNFOCUSED_BORDER_COLOR);
    }
}
JFrame的代码:

package gui;

import java.awt.EventQueue;
import gui.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Color;
import javax.swing.plaf.metal.*;
public class HaiCue extends JFrame{
    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HaiCue window = new HaiCue();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public HaiCue() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setForeground(Color.BLACK);
        setBounds(100, 100, 450, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        getRootPane().putClientProperty("apple.awt.brushMetalLook", Boolean.TRUE);

        JPanel panel = new UnifiedToolbarPanel();
//      panel.setLayout(new WrapLayout());
        add(panel, BorderLayout.NORTH); //<--- I think the problem may be how I add it... I have tried several different ways.

        ToolButton lblStop = new ToolButton("Stop", new ImageIcon(getClass().getResource("/images/stop.png")));
        panel.add(lblStop);

        ToolButton btnToolBox = new ToolButton("Tool Box", new ImageIcon(getClass().getResource("/images/Toolbox.png")));
        panel.add(btnToolBox);

        ToolButton btnInspector = new ToolButton("Inspector", new ImageIcon(getClass().getResource("/images/Toolbox.png")));
        panel.add(btnToolBox);
    }
}
packagegui;
导入java.awt.EventQueue;
导入图形用户界面。*;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入java.awt.BorderLayout;
导入javax.swing.ImageIcon;
导入javax.swing.JSplitPane;
导入javax.swing.JPanel;
导入javax.swing.JLabel;
导入javax.swing.UIManager;
导入javax.swing.UIManager.LookAndFeelInfo;
导入javax.swing.UnsupportedLookAndFeelException;
导入java.awt.Color;
导入javax.swing.plaf.metal.*;
公共类HaiCue扩展JFrame{
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
HaiCue窗口=新HaiCue();
window.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共海口(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
设置前景(颜色为黑色);
立根(100100450300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getRootPane().putClientProperty(“apple.awt.brushMetalLook”,Boolean.TRUE);
JPanel panel=新的UnifiedToolbarPanel();
//panel.setLayout(新的WrapLayout());

添加(panel,BorderLayout.NORTH);//请检查代码的最后留置权

 panel.add(btnInspector );// repalce btnInspector  with btnToolBox

工具栏中按钮的首选大小取决于所提供的
图标的大小。请注意,
pack()
对于使“
窗口
的大小适合其子组件的首选大小和布局至关重要。”下面的示例忽略了中引用的一些更精细的点,但它捕获了基本的几何图形。请使用
OptionPane.warningIcon
进行尝试以查看效果

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;

/**
 * @see http://stackoverflow.com/a/16694524/230513
 */
public class Main {

    private void display() {
        JFrame f = new JFrame("Main");
        f.getRootPane().putClientProperty("apple.awt.brushMetalLook", true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(createToolBar(f), BorderLayout.NORTH);
        f.add(new JTextArea(5, 16));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JToolBar createToolBar(final Component parent) {
        JToolBar bar = new JToolBar("Toolbar");
        bar.setFloatable(false);
        bar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        bar.add(createButton("FileChooser.detailsViewIcon"));
        bar.add(createButton("FileChooser.homeFolderIcon"));
        bar.add(createButton("FileChooser.newFolderIcon"));
        return bar;
    }

    private JButton createButton(String s) {
        JButton b = new JButton(UIManager.getIcon(s));
        b.setHorizontalTextPosition(JButton.CENTER);
        b.setVerticalTextPosition(JButton.CENTER);
        return b;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().display();
            }
        });
    }
}


问题是什么?您是否使用了任何第三方按钮控件?@AkshayJoy这可能不清楚,但问题是它工作不正常。我展示了教程所说的它应该是什么样子的图像,与它实际的样子(按钮只是普通的JButton,我只是解释了为什么它们不同)我在这个链接@AkshayJoy中保存了这个更新的代码。你做了什么更改?好像你用JButtons替换了按钮,去掉了边框…对我来说,按钮正确,你在表单中添加了两次相同的按钮。检查代码的最后一行谢谢,但出于某种原因“工具栏”在我的计算机上显示为与顶色不同的颜色。我准确地复制了代码。这是一张图片:同时,画笔OK在我的计算机上显示的颜色比你和其他人(在工具栏中)的颜色亮得多。可能是OSX版本还是JVM?是的&是的。我怀疑Mac OS 10.8上的Java 7忽略了客户端属性。使用Pixie或查看颜色。如果它是实心的,则有一个动态获取颜色的方案。您也可以查看。谢谢,但我有点困惑。如果什么是实心的?颜色,哪一种?一旦说颜色,wh至少我应该怎么做……我对java/swing/programming相当陌生