Java 2 JPanel 在一个 JFrame 布局中 [英] Java 2 JPanel's in one JFrame layout

查看:40
本文介绍了Java 2 JPanel 在一个 JFrame 布局中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 2 个 JPanel 添加到采用 JFrame 全宽和高度的 JFrame.我设法使用 GridBagLayout() 添加它们,但我似乎无法使用 setsize 设置 JPanel 的大小().我也尝试过使用 ipady 和 ipadx ,虽然在我添加了一些按钮后似乎开始工作,整个布局变得一团糟.这是我的代码:

Hi I am trying to add 2 JPanel's to a JFrame that take the full width and height of the JFrame.I managed to add them with GridBagLayout() but I can't seem to set the size of the JPanels using the setsize().I have also tryied to used ipady and ipadx while that seemed to work at first after I aded some buttons the whole layout became a mess.Here is my code:

           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 的宽度为 300 像素,高度为 400 像素?

How can I make that each JPanel is 300px width and 400px in height?

推荐答案

for GridBaglayout 你必须设置

  • 填充

weightx 和 weighty

weightx and weighty

gridx/gridy(取决于方向)

gridx / gridy (depend of orientations)

然后是可能的,例如

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

on most cases will be better use another LayoutManager

这篇关于Java 2 JPanel 在一个 JFrame 布局中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆