问题
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();
}
});
}
}
回答1:
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 JPanel
s, 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.
回答2:
Have a look at this output :
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.
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:
Check out,
Assuming that by under you mean one Jpanel
is overlapping another!
http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
cheers!
回答4:
You can use border layout
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.
来源:https://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other