Java 为JPanel设置灵活的大小

Java 为JPanel设置灵活的大小,java,swing,Java,Swing,我的ContentPane包含几个jpanel,它们依次包含jtabbepane和其他内容。如何使这些JPanel在用户收缩或扩展窗口时更改其大小。有没有办法设置百分比大小 JPanel panelOne = new JPanel; panelOne.setSize( // % of ); 可以在内容窗格上使用多种布局方法。对于基于与画布相关的大小进行缩放的内容,您可以尝试使用并定义与内容窗格本身相关的边框。其他元素可以约束到该锚定元素或内容窗格 可以使用约束基于ContentPane或其他元

我的
ContentPane
包含几个
jpanel
,它们依次包含
jtabbepane
和其他内容。如何使这些JPanel在用户收缩或扩展窗口时更改其大小。有没有办法设置百分比大小

JPanel panelOne = new JPanel;
panelOne.setSize( // % of );

可以在内容窗格上使用多种布局方法。对于基于与画布相关的大小进行缩放的内容,您可以尝试使用并定义与内容窗格本身相关的边框。其他元素可以约束到该锚定元素或内容窗格

可以使用约束基于ContentPane或其他元素拉伸面板


您可以使用for Eclipse直观地规划SpringLayout(以及其他可能的布局,如Mig、Grid等)。根据版面的大小和比例,根据您的要求或偏好,您可能会发现其他版面更实用。

您可以在内容窗格上使用多种版面方法。对于基于与画布相关的大小进行缩放的内容,您可以尝试使用并定义与内容窗格本身相关的边框。其他元素可以约束到该锚定元素或内容窗格

可以使用约束基于ContentPane或其他元素拉伸面板


您可以使用for Eclipse直观地规划SpringLayout(以及其他可能的布局,如Mig、Grid等)。根据版面的大小和比例,根据您的要求或偏好,您可能会发现其他版面更实用。

使用适当的版面管理器,这就是它们的用途。它们负责监视父容器的更改并根据更改计算所需的设置所涉及的所有脏活

看看

导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridBagLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTabbedPane;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.border.CompoundBorder;
导入javax.swing.border.EmptyBorder;
导入javax.swing.border.LineBorder;
公共类TestSizeableComponents{
公共静态void main(字符串[]args){
新的TestSizeableComponents();
}
公共TestSizeableComponents(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JTabbedPane tabbedPane=新的JTabbedPane();
对于(int-index=0;index<10;index++){
添加(Integer.toString(索引),新建选项卡窗格(Integer.toString(索引));
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新的HeaderPane(),BorderLayout.NORTH);
frame.add(选项卡窗格);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公营班机扩展JPanel{
公共总烷(){
setLayout(新的GridBagLayout());
添加(新JLabel(“看,妈妈,没有手”);
setboorder(新的复合边框(新的线条边框(颜色为红色),新的EmptyBorder(10,10,10,10));
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200100);
}
}
公共类选项卡窗格扩展了JPanel{
公共选项卡窗格(字符串名称){
setLayout(新的GridBagLayout());
添加(新JLabel(名称));
新订单(新的空订单(10,10,10,10));
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
}
}

使用适当的布局管理器,这就是它们的作用所在。它们负责监视父容器的更改并根据更改计算所需的设置所涉及的所有脏活

看看

导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridBagLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTabbedPane;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.border.CompoundBorder;
导入javax.swing.border.EmptyBorder;
导入javax.swing.border.LineBorder;
公共类TestSizeableComponents{
公共静态void main(字符串[]args){
新的TestSizeableComponents();
}
公共TestSizeableComponents(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JTabbedPane tabbedPane=新的JTabbedPane();
对于(int-index=0;index<10;index++){
添加(Integer.toString(索引),新建选项卡窗格(Integer.toString(索引));
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新HeaderPane(),BorderLayout。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class TestSizableComponents {

    public static void main(String[] args) {
        new TestSizableComponents();
    }

    public TestSizableComponents() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTabbedPane tabbedPane = new JTabbedPane();
                for (int index = 0; index < 10; index++) {
                    tabbedPane.add(Integer.toString(index), new TabPane(Integer.toString(index)));
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new HeaderPane(), BorderLayout.NORTH);
                frame.add(tabbedPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class HeaderPane extends JPanel {

        public HeaderPane() {

            setLayout(new GridBagLayout());
            add(new JLabel("Look ma, no hands"));
            setBorder(new CompoundBorder(new LineBorder(Color.RED), new EmptyBorder(10, 10, 10, 10)));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 100);
        }

    }

    public class TabPane extends JPanel {

        public TabPane(String name) {

            setLayout(new GridBagLayout());
            add(new JLabel(name));
            setBorder(new EmptyBorder(10, 10, 10, 10));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}