2013-05-22 400 views
1

我遵循本教程:http://explodingpixels.wordpress.com/2008/05/02/sexy-swing-app-the-unified-toolbar/在Mac上創建了本機外觀工具欄。問題可能是我沒有正確地將其添加到JFrame中,或者可能會誤解某些內容。Swing中的統一工具欄無法正常工作

該程序應該工作的方式是添加一個面板到工具欄(或在它下面,我認爲 - 它不清楚)。以防萬一出現混淆:統一的工具欄是常規工具欄,只有其中也有按鈕。

這是什麼,是應該像這樣: enter image description here

這是它看起來像:(?它不應該的問題,我使用不同的美觀的按鈕,它應該) enter image description here

代碼爲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

+0

啥子是問題?你在使用任何thirdPartyButton控件嗎? ? –

+0

@AkshayJoy也許這並不清楚,但問題是它無法正常工作。我展示了教程所說的應該看起來像什麼樣子,而不是實際看起來的樣子(按鈕只是普通的JButtons,我只是解釋它們爲什麼不同) –

+0

我在這個LINK中保留了這個更新的代碼http:// www.sendspace.com/file/i3d5ms –

回答

0

請檢查代碼的最後連戰,

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

的首選大小工具欄上的按鈕嚴格按照提供的Icon的尺寸鉸接。請注意,pack()對於導致「Window的大小適合其子組件的首選大小和佈局」至關重要。「下面的例子忽略了article中引用的一些更精細的點,但它捕獲了基本的幾何。試用OptionPane.warningIcon來查看效果。

image

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(); 
      } 
     }); 
    } 
} 
+0

謝謝,但出於某種原因,「工具欄」在我的計算機上顯示爲與頂部顏色不同的顏色。我完全複製了代碼。 –

+0

這是圖片:http://picpaste.com/pics/8b8028d8921d3d6c3dfa79ad46ebb2af.1369234691.png –

+0

另外brushMetalLook比我的和其他人(在工具欄中)顯示的要輕得多。它可能是OSX版本還是JVM? –