JFrame中的两个jpanel,一个在另一个下。

时间:2022-02-25 15:00:40

I've got two panels in my frame, and I would like to set them one under other, and this first should have size like 9/10*screen frame, and this second 1/10.

在我的坐标系中有两个面板,我想把它们设为另一个,第一个应该有9/10*屏幕帧大小,这是第二个1/10。

I've tried with GridLayout (2 rows and one column) but I can't set them specific size.

我尝试过GridLayout(2行和1列),但是我不能设置它们的具体大小。

How should I do that?

我该怎么做呢?


ok maybe I will write some my code:

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

I am writing game - pacman, and in the first panel there is a whole game, and in this second I would like to display player info(like score, name etc.) This first I would like to set on 80% screen, and second on 20%.

我正在编写游戏- pacman,在第一个面板中有一个完整的游戏,在这一秒我想要显示玩家信息(比如score, name等)。首先我想设定在80%的屏幕上,其次是20%。

What is more my frame should be resizeable and all in it, sa I have to change size of Panels(keeping this 80% to 20%) when size of frame is changing. SO that I wrote this InitComponents().

更重要的是,我的框架应该是可调整的,我必须改变面板的大小(保持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();
      }  
  });  
}  



}

4 个解决方案

#1


3  

GridLayout is the wrong layout if you want different sized components. As the javadoc states:

如果您想要不同大小的组件,那么GridLayout是错误的布局。javadoc状态:

The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

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

If you've only got JPanels, you might want to consider a JSplitPane - see javadoc or tutorial.

如果您只有jpanel,那么您可能需要考虑JSplitPane——请参见javadoc或教程。

EDIT: Based on your edit/code, a JSplitPane really looks like the solution to your problem. You can then set the divider location after creation using setDividerLocation(double) - see the javadoc - e.g.

编辑:根据您的编辑/代码,JSplitPane看起来确实是您问题的解决方案。然后,您可以使用setDividerLocation(double)来设置分隔符位置,请参见javadoc。

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

Alternatively, since it's quite hard to suggest a layout without knowing your intentions for the GUI, you should consider taking a look at the Visual Guide to layouts.

或者,由于很难在不了解GUI的意图的情况下建议布局,所以您应该考虑查看布局的可视化向导。

#2


2  

Have a look at this output :

看看这个输出:

JFrame中的两个jpanel,一个在另一个下。

And here is the code for that, you are not suppose to use GridLayout when you need to adjust sizes for columns/rows, under such situations comes GridBagLayout to the rescue.

这里是代码,当您需要调整列/行的大小时,您不应该使用GridLayout,在这种情况下,会出现GridBagLayout来进行补救。

import java.awt.*;
import javax.swing.*;
// http://*.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


1  

Check out, Assuming that by under you mean one Jpanel is overlapping another!

假设你的意思是一个Jpanel是重叠的!

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

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

cheers!

干杯!

#4


1  

You can use border layout

您可以使用边框布局。

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

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

Set the bigger panel to be CENTER, and the smaller to be PAGE_END. Set the size of the bigger panel, and the smaller panel will use what space is left.

设置较大的面板为中心,较小的为PAGE_END。设置大面板的大小,较小的面板将使用剩余空间。

#1


3  

GridLayout is the wrong layout if you want different sized components. As the javadoc states:

如果您想要不同大小的组件,那么GridLayout是错误的布局。javadoc状态:

The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

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

If you've only got JPanels, you might want to consider a JSplitPane - see javadoc or tutorial.

如果您只有jpanel,那么您可能需要考虑JSplitPane——请参见javadoc或教程。

EDIT: Based on your edit/code, a JSplitPane really looks like the solution to your problem. You can then set the divider location after creation using setDividerLocation(double) - see the javadoc - e.g.

编辑:根据您的编辑/代码,JSplitPane看起来确实是您问题的解决方案。然后,您可以使用setDividerLocation(double)来设置分隔符位置,请参见javadoc。

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

Alternatively, since it's quite hard to suggest a layout without knowing your intentions for the GUI, you should consider taking a look at the Visual Guide to layouts.

或者,由于很难在不了解GUI的意图的情况下建议布局,所以您应该考虑查看布局的可视化向导。

#2


2  

Have a look at this output :

看看这个输出:

JFrame中的两个jpanel,一个在另一个下。

And here is the code for that, you are not suppose to use GridLayout when you need to adjust sizes for columns/rows, under such situations comes GridBagLayout to the rescue.

这里是代码,当您需要调整列/行的大小时,您不应该使用GridLayout,在这种情况下,会出现GridBagLayout来进行补救。

import java.awt.*;
import javax.swing.*;
// http://*.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


1  

Check out, Assuming that by under you mean one Jpanel is overlapping another!

假设你的意思是一个Jpanel是重叠的!

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

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

cheers!

干杯!

#4


1  

You can use border layout

您可以使用边框布局。

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

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

Set the bigger panel to be CENTER, and the smaller to be PAGE_END. Set the size of the bigger panel, and the smaller panel will use what space is left.

设置较大的面板为中心,较小的为PAGE_END。设置大面板的大小,较小的面板将使用剩余空间。