二JPanels在JFrame中,另外一个下 [英] Two JPanels in JFrame , One under other

查看:231
本文介绍了二JPanels在JFrame中,另外一个下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个小组在我的框架,我想将它们设置一个其他的根据,这首先应该有大小像9月10日*屏幕边框,而本次的1/10。

我试过用网格布局(2行一列),但我不能将它们设置特定大小。

我应该怎么办呢?


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

我写的游戏 - 吃豆子,并在第一盘有一整场比赛,在这第二个我想显示播放信息(如成绩,姓名等),这首先我想在80%设定屏幕上,并且第二对20%。

更重要的是我的框架应该是调整大小和所有在里面,SA我必须改变面板的大小(这保持80%〜20%)时,帧大小是变化的。所以,我写这个的initComponents()。

 包吃豆子;进口java.awt.BorderLayout中;
进口java.awt.Dimension中;
进口java.awt.EventQueue中;
进口java.awt.GridLayout中;
进口java.awt.Toolkit中;
进口javax.imageio.ImageIO中;
进口javax.swing.JFrame中;
进口java.awt.Image中;
进口java.awt.event.ComponentAdapter;
进口java.awt.event.ComponentEvent;公共类豆子扩展JFrame的实现项目
{
   形象画像;
市民吃豆子(){
    的initComponents();
    尝试{
      图像= ImageIO.read(Pac.class.getResourceAsStream(/ IMG / Pac02.gif));
    }赶上(例外五){
        的System.out.println(BLAD PRZ otwieraniu+ E);
        System.exit(0);
       }
    INT SCREEN_WIDTH = Toolkit.getDefaultToolkit()getScreenSize()宽度。
    INT SCREEN_HEIGHT = Toolkit.getDefaultToolkit()getScreenSize()高度。
    this.setLocation(SCREEN_WIDTH / 3,SCREEN_HEIGHT / 3);    this.setLayout(新的BorderLayout());
    。this.getContentPane()加(面板,BorderLayout.CENTER);
    。this.getContentPane()加(是Panel2,BorderLayout.PAGE_END);    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setIconImage(图片);的setTitle(.. ::吃豆子:: ..);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.set preferredSize(新尺寸(416438));
this.pack();
setLocationRelativeTo(NULL);
调用setVisible(真);
}
私人无效的initComponents(){
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.addComponentListener(新ComponentAdapter(){
      @覆盖
  公共无效的componentResized(ComponentEvent E){        INT宽度= e.getComponent()的getSize()宽度。
           INT高度= e.getComponent()的getSize()高度。
            panel.setSize(宽度,高度* 8/10);
           panel2.setSize(宽度,高度* 2/10);}
});
}公共静态无效的主要(字串[] args){
  EventQueue.invokeLater(新的Runnable(){      @覆盖
      公共无效的run(){
         新的吃豆子();
      }
  });
}}


解决方案

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


  

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


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

编辑:根据您的编辑/ code,一个真正的JSplitPane貌似解决您的问题。然后,您可以创建后,使用设定的分隔位置 setDividerLocation(双) - 看到<一个href=\"http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JSplitPane.html#setDividerLocation%28double%29\"相对=nofollow>的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.

这篇关于二JPanels在JFrame中,另外一个下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆