我按照本教程:http ://explodingpixels.wordpress.com/2008/05/02/sexy-swing-app-the-unified-toolbar/在 Mac 上创建了一个具有原生外观的工具栏。问题可能是我没有将它正确添加到 JFrame,或者可能误解了某些东西。
程序应该工作的方式是在工具栏上添加一个面板(或者在它下面,我认为 - 不清楚)。以防万一:统一的工具栏是常规的工具栏,里面也只有按钮。
这应该是这样的:
这就是它的样子:(我使用不同外观的按钮应该没关系,不是吗?)
代码Unified Toolbar Panel
:
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);
}
}
我正在运行 OSX 10.8.2 和 Java 1.7.0_13