2013-04-30 53 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(); 
      } 
     }); 
    } 
}