首页 文章

Java 2 JPanel在一个JFrame布局中

提问于
浏览
3

嗨我想尝试将2个JPanel添加到JFrame中,该JFrame占用JFrame的全部宽度和高度 . 我设法使用GridBagLayout()添加它们但我似乎无法使用setsize()设置JPanel的大小 . 我也尝试过使用ipady和ipadx,但是当我按下一些按钮后整个布局变得一团糟时,它似乎起作用了 . 这是我的代码:

JFrame tradeframe = new JFrame("Trade");
           JPanel P1panel = new JPanel();         
           P1panel.setBackground(Color.red);
           JPanel P2panel = new JPanel();
           P2panel.setBackground(Color.BLACK);


           tradeframe.setVisible(true);
           tradeframe.setSize(600, 400);
           tradeframe.setResizable(false);
           tradeframe.setLocationRelativeTo(null);
           tradeframe.setLayout(new GridBagLayout());

           P1panel.add(new JButton ("P1 Agree"));

           P2panel.add(new JButton ("P2 Agree"));


           GridBagConstraints a = new GridBagConstraints();
           a.gridx = 0;
           a.gridy = 0;
           a.weightx = 360;
           a.weighty = 300;
           //a.fill = GridBagConstraints.HORIZONTAL;
           tradeframe.add(P1panel , a);

           GridBagConstraints b = new GridBagConstraints();
           b.gridx = 1;
           b.gridy = 0;
           b.weightx = 360;
           b.weighty = 300;
          // b.fill = GridBagConstraints.HORIZONTAL;
           tradeframe.add(P2panel , b);

如何使每个JPanel的宽度为300px,高度为400px?

5 回答

  • 3

    这是这样做的方法:

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagLayoutTest
    {
        public GridBagLayoutTest()
        {
            JFrame frame = new JFrame("GridBag Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            Container container = frame.getContentPane();
            container.setLayout(new GridBagLayout());
    
            JPanel leftPanel = new JPanel();
            leftPanel.setBackground(Color.WHITE);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.5;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;
    
            container.add(leftPanel, gbc);
    
            JPanel rightPanel = new JPanel();
            rightPanel.setBackground(Color.BLUE);
            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.weightx = 0.5;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.FIRST_LINE_END;
            gbc.fill = GridBagConstraints.BOTH;
    
            container.add(rightPanel, gbc);
    
            frame.setSize(600, 400);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    new GridBagLayoutTest();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }
    

    输出:

    GRIDBAG LAYOUT TEST

  • 2

    对于GridBaglayout你必须设置

    • weightx和重量

    • gridx / gridy(取决于方向)

    那么例如是可能的

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class BorderPanels extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public BorderPanels() {
            setLayout(new GridBagLayout());// set LayoutManager
            GridBagConstraints gbc = new GridBagConstraints();
            JPanel panel1 = new JPanel();
            Border eBorder = BorderFactory.createEtchedBorder();
    
            panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
            gbc.gridx = gbc.gridy = 0;
            gbc.gridwidth = gbc.gridheight = 1;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.weightx = gbc.weighty = 20;
            add(panel1, gbc); // add compoenet to the COntentPane
    
            JPanel panel2 = new JPanel();
            panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "60pct"));
            gbc.gridy = 1;
            gbc.weightx = gbc.weighty = 60;
            //gbc.insets = new Insets(2, 2, 2, 2);
            add(panel2, gbc); // add component to the COntentPane
    
            JPanel panel3 = new JPanel();
            panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
            gbc.gridy = 2;
            gbc.weightx = gbc.weighty = 20;
            gbc.insets = new Insets(2, 2, 2, 2);
            add(panel3, gbc);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
            pack();
            setVisible(true); // important
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
    
                public void run() {
                    BorderPanels borderPanels = new BorderPanels();
                }
            });
        }
    }
    

    在大多数情况下将更好地使用另一个 LayoutManager

  • 0
    JFrame tradeframe = new JFrame("Trade");
        JPanel P1panel = new JPanel();         
        P1panel.setBackground(Color.red);
        JPanel P2panel = new JPanel();
        P2panel.setBackground(Color.BLACK);
        tradeframe.setSize(600, 400);
        tradeframe.setResizable(false);
        tradeframe.setLocationRelativeTo(null);
    
        Box content = new Box(BoxLayout.X_AXIS);
    
        P1panel.add(new JButton ("P1 Agree"));
    
        P2panel.add(new JButton ("P2 Agree"));
    
        content.add(P1panel);
        content.add(P2panel);
    
        tradeframe.setContentPane(content);
        tradeframe.setVisible(true);
    
  • 2

    在面板对象上调用 setPreferredSize(new Dimension(int width, int height)); 方法 .

  • 4

    您正在使用 setSize() 而不是 setPreferredSize() . 差异有点误导,我认为这是java中的问题 . 关于两者之间的区别的更多信息可以找到here.

    我链接的文章有一些其他的陷阱/陷阱和一个有用的读取,如果你是Java的新手 .

相关问题