Java 如何在1个JPanel内获得2个JPanel,可调整大小,固定比例?
假设一个Java 如何在1个JPanel内获得2个JPanel,可调整大小,固定比例?,java,swing,user-interface,layout-manager,Java,Swing,User Interface,Layout Manager,假设一个JFrame只包含1个JPanel。此JPanel分为2个JPanels,分别占据JFrame高度的0.75和0.25。我希望所有这些都可以随着窗口大小调整大小 我不知道如何在Java中实现这一点 我是Java的新手。我读过一些关于布局的书,但我能看到的只是如何在构造函数中设置首选大小(当达到这个数字时停止调整大小)或通过设置边框获得的一些固定大小。JFrame带有边框布局,在其上,添加一个JPanel带有网格布局。将其他两个面板添加到此面板上 有关详细信息,请参阅和 import ja
JFrame
只包含1个JPanel
。此JPanel
分为2个JPanel
s,分别占据JFrame
高度的0.75
和0.25
。我希望所有这些都可以随着窗口大小调整大小
我不知道如何在Java中实现这一点
我是Java的新手。我读过一些关于布局的书,但我能看到的只是如何在构造函数中设置首选大小(当达到这个数字时停止调整大小)或通过设置边框获得的一些固定大小。
JFrame
带有边框布局
,在其上,添加一个JPanel
带有网格布局
。将其他两个面板添加到此面板上
有关详细信息,请参阅和
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0.75;
JPanel top = new JPanel(new GridBagLayout());
top.add(new JLabel("Top"));
top.setBackground(Color.RED);
add(top, gbc);
gbc.gridy++;
gbc.weighty = 0.25;
JPanel bottom = new JPanel(new GridBagLayout());
bottom.add(new JLabel("Bottom"));
bottom.setBackground(Color.BLUE);
add(bottom, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
JFrame
带有BorderLayout
,在其上添加一个JPanel
带有gridbagloayout
。将其他两个面板添加到此面板上
有关详细信息,请参阅和
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0.75;
JPanel top = new JPanel(new GridBagLayout());
top.add(new JLabel("Top"));
top.setBackground(Color.RED);
add(top, gbc);
gbc.gridy++;
gbc.weighty = 0.25;
JPanel bottom = new JPanel(new GridBagLayout());
bottom.add(new JLabel("Bottom"));
bottom.setBackground(Color.BLUE);
add(bottom, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
JFrame
带BorderLayout
,JPanel
带gridbagloayout
的框架中。有关使用BorderLayout
的JFrame
、使用gridbagloayout
的框架中的JPanel
的更多详细信息,请参见和。有关详细信息,请参阅和