2013-04-30 108 views
1

我有關於製作特定佈局的問題,首先我會展示示例,然後添加一些額外的說明。Java Swing - 使用LayeredPane實現佈局

佈局,當朋友和消息都是關閉的: enter image description here

佈局,當朋友和消息被打開: enter image description here

我打算讓這個佈局的Java Swing。

我的意圖是首先將幀分成三個區域,頂層菜單行,主面板和底層菜單行。 我正在考慮爲這部分使用BorderLayout。

然後朋友和消息按鈕應該是切換按鈕的,並且在切換時它們應該在主面板(或其他任何地方)上顯示疊加層,其包含朋友列表和消息區域。我意識到我需要爲此使用LayeredPane。

另一個重要的部分是佈局應該可以以任何大小進行查看,也就是說用戶可以調整應用程序的大小,並且可以在不同的分辨率下使用,所以我並不需要固定寬度的任何東西和身高。

但是我真的失去了如何結合這個,所以我請你的幫助。

希望我已經解釋了足夠的情況。

問候。

+0

什麼是主面板即是如此無關緊要,你能買得起用兩個「浮動」面板掩蓋呢?聽起來像是「另一個不可用的GUI」。 – 2013-04-30 10:08:59

+0

它用於2D回合制遊戲。如果你想看到你的消息,那麼你點擊消息按鈕(或者最好使用一個快捷方式),當你不想再看到它們時,再次隱藏它。所有你的選擇,消息當然不需要在玩遊戲時看到。但它也可以用於調試反饋,例如。 – skiwi 2013-04-30 10:20:48

回答

2

這可能是大約覆蓋,因爲JPanel中可以包含其他JComponents

  • 使用JLayer(Java7)的基礎上JXLayer(的Java6),

  • 使用玻璃面板與JComponents layed to rellative to....

  • 最簡單可以使用JDialog(undecorated)放置到Point(setLocation(int,int)),setVisible()必須包裝成invokeLater

+0

我會在第二時間看看這個。但是您是否考慮過我希望其他面板(朋友列表和消息區)與框架結合在一起,而不是與主面板結合使用?這樣,如果必要的話,我仍然可以取代東西,當然如果這是我必須適應的唯一方法。 – skiwi 2013-04-30 10:10:39

+0

@skiwi主佈局可以通過使用BorderLayout來實現,利用GlassPane或LayerPane的框架,可以覆蓋其他組件。查看[JRootPane](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html)以獲取更多詳細信息 – MadProgrammer 2013-04-30 10:14:10

+0

對於您可以將任何JComponent放置在JPanel中的每個選擇,更改在運行時)Dimension,屏幕座標不用擔心沒關係,直到使用LayoutManager,使用GlassPane,直​​到透明是沒有必要的,否則看看JLayer,未裝飾的JDialog(儘可能簡單) – mKorbel 2013-04-30 10:17:14

1

我將使用gridBagLayout。

下面是小例子包括按鈕,隱藏自己的黃色面板:

package Core; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Panel; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GridBagLayoutDemo { 

    public static void addComponentsToPane(Container pane) { 
     pane.setLayout(new GridBagLayout()); 

     add1row(pane); 
     addmainRow(pane); 
     addLastRow(pane); 
    } 

    private static void addLastRow(Container pane) { 
     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     c.anchor = GridBagConstraints.PAGE_END; 
     JPanel bottonPanel = new JPanel(); 
     bottonPanel.setBackground(Color.BLUE); 
     bottonPanel.setLayout(new GridBagLayout()); 
     pane.add(bottonPanel, c); 

     JPanel messagePanel = new JPanel(); 
     messagePanel.setBackground(Color.GRAY); 
     messagePanel.add(new JLabel("MESSAGES")); 
     c.fill = GridBagConstraints.VERTICAL; 
     c.anchor = GridBagConstraints.LINE_END; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     bottonPanel.add(messagePanel, c); 
    } 

    private static void addmainRow(Container pane) { 
     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.BOTH; 
     c.gridx = 0; 
     c.gridy = 1; 
     c.weightx = 1; 
     c.weighty = 1; 
     c.anchor = GridBagConstraints.CENTER; 
     JPanel mainManel = new JPanel(); 
     mainManel.setBackground(Color.GREEN); 
     mainManel.setLayout(new GridBagLayout()); 
     pane.add(mainManel, c); 

     final JPanel friendListPanel = new JPanel(); 
     friendListPanel.setBackground(Color.YELLOW); 
     friendListPanel.add(new JLabel("FRIEND LIST")); 
     c.fill = GridBagConstraints.NONE; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     c.weighty = 1; 
     c.anchor = GridBagConstraints.FIRST_LINE_END; 
     mainManel.add(friendListPanel, c); 

     c.fill = GridBagConstraints.NONE; 
     c.gridx = 0; 
     c.gridy = 1; 
     c.weightx = 0; 
     c.weighty = 0; 
     c.anchor = GridBagConstraints.CENTER; 
     JButton button = new JButton("On/Off"); 
     mainManel.add(button, c); 

     final JPanel messageAreaPanel = new JPanel(); 
     messageAreaPanel.setBackground(Color.YELLOW); 
     messageAreaPanel.add(new JLabel("MESSAGE PANEL")); 
     c.fill = GridBagConstraints.NONE; 
     c.gridx = 0; 
     c.gridy = 2; 
     c.weightx = 1; 
     c.weighty = 1; 
     c.anchor = GridBagConstraints.LAST_LINE_END; 
     mainManel.add(messageAreaPanel, c); 

     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       friendListPanel.setVisible(!friendListPanel.isVisible()); 
       messageAreaPanel.setVisible(!messageAreaPanel.isVisible()); 
      } 
     }); 

    } 

    private static void add1row(Container pane) { 
     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.anchor = GridBagConstraints.PAGE_START; 
     Panel headerPanel = new Panel(); 
     headerPanel.setLayout(new GridBagLayout()); 
     headerPanel.setBackground(Color.BLUE); 
     pane.add(headerPanel, c); 

     JPanel quitPanel = new JPanel(); 
     quitPanel.setBackground(Color.GRAY); 
     quitPanel.add(new JLabel("QUIT")); 
     c.fill = GridBagConstraints.VERTICAL; 
     c.anchor = GridBagConstraints.LINE_START; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     headerPanel.add(quitPanel, c); 

     JPanel friendsPanel = new JPanel(); 
     friendsPanel.setBackground(Color.GRAY); 
     friendsPanel.add(new JLabel("FRIENDS")); 
     c.fill = GridBagConstraints.VERTICAL; 
     c.anchor = GridBagConstraints.LINE_END; 
     c.gridx = 1; 
     c.gridy = 0; 
     headerPanel.add(friendsPanel, c); 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("GridBagLayoutDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     addComponentsToPane(frame); 
     // uncoment to use full screen 
     // frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
     frame.setSize(new Dimension(400, 400)); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
}