2011-08-28 119 views
7

我有一個真正奇怪的方法,用於隱藏JInternalFrame的標題欄。現在的困境是低於Windows平臺上的方法,Java方法適用於Windows,但不適用於Macintosh?

((javax.swing.plaf.basic.BasicInternalFrameUI) aXInternalFrame.getUI()).setNorthPane(null); 

但不是在Macintosh上!任何專家都有任何想法可以解釋這種方法在Mac上不可用的內部過程。

是否有任何方法可以在兩個平臺上工作以隱藏標題欄JInternalFrame

謝謝

回答

4

在Mac OS X,的com.apple.laf.AquaInternalFrameUI實例定義了內部框架的外觀。您可以通過專門設置isPalette屬性並禁用Mac OS X上的框架圖標來最大限度地減少差異,如下所示。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.plaf.basic.BasicInternalFrameUI; 

/** @see http://stackoverflow.com/questions/7218971 */ 
public class InternalFrame { 

    private static final int DELTA = 40; 
    private JDesktopPane desktop = new JDesktopPane(); 
    private int offset = DELTA; 

    public InternalFrame() { 
     JFrame f = new JFrame("Add Frame"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setPreferredSize(new Dimension(400, 400)); 
     JPanel p = new JPanel(); 
     p.add(new JButton(new AbstractAction("Add") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       createInternalFrame(); 
      } 
     })); 
     f.add(p, BorderLayout.SOUTH); 
     createInternalFrame(); 
     f.add(desktop, BorderLayout.CENTER); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private void createInternalFrame() { 
     JInternalFrame internalFrame = new JInternalFrame(
      "Internal Frame");//, true, true, true, true); 
     desktop.add(internalFrame); 
     internalFrame.setLocation(offset, offset); 
     offset += DELTA; 
     if (System.getProperty("os.name").startsWith("Mac OS")) { 
      internalFrame.putClientProperty("JInternalFrame.isPalette", true); 
     } else { 
      ((BasicInternalFrameUI) internalFrame.getUI()).setNorthPane(null); 
     } 
     internalFrame.add(createTabbedPane()); 
     internalFrame.pack(); 
     internalFrame.setVisible(true); 
    } 

    // take up some space 
    private JTabbedPane createTabbedPane() { 
     JTabbedPane jtp = new JTabbedPane(); 
     createTab(jtp, "One"); 
     createTab(jtp, "Two"); 
     return jtp; 
    } 

    private void createTab(JTabbedPane jtp, String s) { 
     jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER)); 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       InternalFrame myInternalFrame = new InternalFrame(); 
      } 
     }); 
    } 
} 
1

這可能是一個Swing的毛茸茸的角落。據我所知,這個功能直到java 1.5才被添加到swing中。

你試過Frame.setUndecorated方法嗎?

http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Frame.html#setUndecorated%28boolean%29

如果不工作,你可能需要向下掉落和做一些JNI基礎本機窗口對象。我必須在1.4 jvm的windows上使用類似的功能。

+0

['JInternalFrame'](http://download.oracle.com/javase/6/docs/api/javax/swing/JInternalFrame.html)是一個輕量級組件;外觀由UI委託「InternalFrameUI」提供。 – trashgod

相關問題