且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

二JPanels在JFrame中,另外一个下

更新时间:2023-12-05 22:19:28

GridLayout的是错误的布局,如果你想不同尺寸的组件。由于的javadoc 规定:


  

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


块引用>

如果你只得到了的JP​​anel S,你可能要考虑一个调整JSplitPane - 看到的的javadoc 或的教程

编辑:根据您的编辑/ code,一个真正的JSplitPane貌似解决您的问题。然后,您可以创建后,使用设定的分隔位置 setDividerLocation(双) - 看到的javadoc - 例如

 分裂的JSplitPane =新JSplitPane(JSplitPane.VERTICAL);
split.setTopComponent(topPanel);
split.setBottomComponent(bottomPanel);
split.setDividerLocation(0.8);

另外,因为它是相当困难不知道你的意图的GUI建议的布局,就应该考虑采取一看的视觉指南来布局。

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.

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

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%.

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().

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();
      }  
  });  
}  



}

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

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.

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 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.