2013-03-02 86 views
2

我想知道是否有可能展开JFrame,然后只显示顶部的关闭按钮或类似的东西..任何帮助将不胜感激。 细节: 其实我正在做一个项目,这是一个基于GUI的软件,我已经在其中使用JdesktopPane,并且我使用JInternalFrames,因此在顶部显示标题栏不仅适合我。有什么办法可以解决我的问题吗?只显示JFrame上的关闭按钮undecorated

+1

你应该为此创建一个自定义按钮。 – tmwanik 2013-03-02 04:13:55

+0

我可以拥有,但是不会显示在右上角。它可以在那里显示? – nsthethunderbolt 2013-03-02 04:15:39

+1

是的,可能是使用布局管理器。 – tmwanik 2013-03-02 04:19:08

回答

3

基本上,一旦您删除了边框,就会对其功能(包括移动)负责。

本示例基本上使用JPanel作为“标题”窗格的占位符,并使用BorderLayout来布局标题和内容。

实际关闭按钮是一个简单的JButton,它使用一些图像来表示关闭操作。显然,这只会看的权利在Windows上,这些按钮都是正常的由OS本身......而我们没有访问该层中产生的......

enter image description hereenter image description here

public class TestUndecoratedFrame { 

    public static void main(String[] args) { 
     new TestUndecoratedFrame(); 
    } 

    public TestUndecoratedFrame() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       TitlePane titlePane = new TitlePane(); 

       JFrame frame = new JFrame("Testing"); 
       frame.setUndecorated(true); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       ((JComponent)frame.getContentPane()).setBorder(new LineBorder(Color.BLACK)); 
       frame.add(titlePane, BorderLayout.NORTH); 
       frame.add(new JLabel("This is your content", JLabel.CENTER)); 
       frame.setSize(200, 200); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TitlePane extends JPanel { 

     public TitlePane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new CloseControl(), gbc); 
     } 
    } 

    public class CloseControl extends JButton { 

     public CloseControl() { 
      setBorderPainted(false); 
      setContentAreaFilled(false); 
      setFocusPainted(false); 
      setOpaque(false); 
      setBorder(new EmptyBorder(0, 0, 8, 12)); 
      try { 
       setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Highlighted.png")))); 
       setRolloverEnabled(true); 
       setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Normal.png")))); 
      } catch (IOException ex) { 
       Logger.getLogger(TestUndecoratedFrame.class.getName()).log(Level.SEVERE, null, ex); 
      } 

      addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        SwingUtilities.getWindowAncestor(CloseControl.this).dispose(); 
       } 
      }); 
     } 
    } 
} 
+0

谢谢..一种新的方式。 – nsthethunderbolt 2013-03-02 05:06:22

+2

@nsthethunderbolt:在这种情况下,您可能可以利用内部框架图标,见[此处](http://stackoverflow.com/a/10360374/230513)。 – trashgod 2013-03-02 06:40:07

+0

@trashgod +1好主意 – MadProgrammer 2013-03-02 07:29:24