2012-06-10 95 views
3

我在我的框架中有两个面板,并且我想将它们设置为一个在其他下面,并且这个第一个应该具有9/10 *屏幕框的大小,而第二个1/10。JFrame中的两个JPanel,其他一个

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

我该怎么做?


没关系,以后我会写我的一些代码:

我写游戏 - 吃豆子,并且在第一板上有一整场比赛,在这第二个我想显示播放信息(比如分数,名字等)首先我想在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(); 
     } 
    }); 
} 



} 
+0

是一个可调整大小,但其他不是?这两个面板中有什么? –

+0

你已经发布了一些代码......但你看过你收到的实际答案了吗? (另外,你应该看看Java [代码约定](http://www.oracle.com/technetwork/java/codeconv-138413.html) – amaidment

回答

3

如果你想要不同大小的组件,GridLayout是错误的布局。作为javadoc状态:

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

如果你只得到了JPanel S,你可能要考虑一个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

看一看这样的输出:

GRIDBAGLAYOUTEXAMPLE

这里是对的代码,你是不是想用GridLayout当你需要调整尺寸columns/rows,在这种情况下,来自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(); 
      }   
     }); 
    } 
} 
相关问题