【问题标题】:Two JPanels in JFrame , One under otherJFrame 中的两个 JPanel,另一个下的一个
【发布时间】:2012-06-13 16:39:18
【问题描述】:

我的框架中有两个面板,我想将它们设置在另一个之下,第一个的大小应该是 9/10*screen frame,第二个应该是 1/10。

我尝试过使用 GridLayout(2 行和 1 列),但无法设置它们的特定大小。

我该怎么做?


好吧,也许我会写一些我的代码:

我正在写游戏 - 吃豆人,在第一个面板中有一个完整的游戏,在这第二个中我想显示玩家信息(如分数、姓名等)。第一个我想设置为 80%屏幕,第二个在 20%。

更重要的是,我的框架应该可以调整大小并且全部包含在内,例如,当框架大小发生变化时,我必须更改面板的大小(保持 80% 到 20%)。所以我写了这个 InitComponents()。

package pacman;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class Pacman extends JFrame implements items
{
   Image image;
public Pacman()

{    
    initComponents();
    try {           
      image = ImageIO.read( Pac.class.getResourceAsStream("/img/Pac02.gif"));         
    } catch (Exception e) {
        System.out.println("Blad prz otwieraniu " + e);
        System.exit(0);
       }


    int screen_width = Toolkit.getDefaultToolkit().getScreenSize().width;
    int screen_height = Toolkit.getDefaultToolkit().getScreenSize().height;      
    this.setLocation(screen_width/3, screen_height/3); 

    this.setLayout(new BorderLayout());
    this.getContentPane().add(panel, BorderLayout.CENTER);
    this.getContentPane().add(panel2, BorderLayout.PAGE_END);



    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setIconImage(image);

setTitle("..::Pacman::..");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(416,438));
this.pack();
setLocationRelativeTo(null);


setVisible(true);
}
private void initComponents() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  this.addComponentListener(new ComponentAdapter() {
      @Override
  public void componentResized(ComponentEvent e) {

        int width = e.getComponent().getSize().width;
           int height = e.getComponent().getSize().height;
            panel.setSize(width, height*8/10) ;
           panel2.setSize(width, height*2/10);

}
});


} 

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

      @Override  
      public void run() {  
         new Pacman();
      }  
  });  
}  



}

【问题讨论】:

  • 一个可以调整大小,而另一个不能吗?这两个面板是什么?
  • 您已经发布了一些代码...但是您是否查看过您已经收到的实际答案? (除此之外,你真的应该看看 Java code conventions

标签: java swing awt jpanel layout-manager


【解决方案1】:

如果您想要不同大小的组件,GridLayout 是错误的布局。正如javadoc 所说:

容器被分成大小相等的矩形,一个 组件被放置在每个矩形中。

如果您只有JPanels,您可能需要考虑JSplitPane - 请参阅javadoctutorial

编辑:根据您的编辑/代码,JSplitPane 看起来确实可以解决您的问题。然后,您可以在创建后使用 setDividerLocation(double) 设置分隔线位置 - 请参阅 javadoc - 例如

JSplitPane split = new JSplitPane(JSplitPane.VERTICAL);
split.setTopComponent(topPanel);
split.setBottomComponent(bottomPanel);
split.setDividerLocation(0.8);

另外,由于在不知道您对 GUI 的意图的情况下很难建议布局,您应该考虑查看 Visual Guide 布局。

【讨论】:

    【解决方案2】:

    看看这个输出:

    这里是代码,当你需要调整columns/rows 的大小时,你不应该使用GridLayout,在这种情况下GridBagLayout 来救援。

    import java.awt.*;
    import javax.swing.*;
    // http://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other
    public class GridBagLayoutExample
    {
        private void displayGUI()
        {
            JFrame frame = new JFrame("GridBagLayout Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new GridBagLayout());
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 0.9;
    
            JPanel topPanel = new JPanel();
            topPanel.setOpaque(true);
            topPanel.setBackground(Color.WHITE);
            contentPane.add(topPanel, gbc);
    
            gbc.weighty = 0.1;
            gbc.gridy = 1;
    
            JPanel bottomPanel = new JPanel();
            bottomPanel.setOpaque(true);    
            bottomPanel.setBackground(Color.BLUE);
            contentPane.add(bottomPanel, gbc);
    
            frame.setContentPane(contentPane);
            frame.setSize(200, 300);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new GridBagLayoutExample().displayGUI();
                }           
            });
        }
    }
    

    【讨论】:

      【解决方案3】:

      退房, 假设你的意思是一个Jpanel 与另一个重叠!

      http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

      干杯!

      【讨论】:

        【解决方案4】:

        你可以使用边框布局

        http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

        将较大的面板设置为 CENTER,将较小的面板设置为 PAGE_END。设置大面板的大小,小面板会占用剩余空间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-05
          • 1970-01-01
          • 2014-04-12
          • 2019-06-08
          • 1970-01-01
          相关资源
          最近更新 更多