Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用不同布局嵌套JPanel_Java_Swing_Jlabel_Gridbaglayout - Fatal编程技术网

Java 使用不同布局嵌套JPanel

Java 使用不同布局嵌套JPanel,java,swing,jlabel,gridbaglayout,Java,Swing,Jlabel,Gridbaglayout,我目前正试图为自己的工作做一个小项目,以帮助我完成日常琐事。我试图在Swing中创建一个小表,并开始添加组件。然而,我面临着一些困难 首先,让我强调一下我需要实现的目标。下图: 根据下面的代码示例,我尝试创建一个主容器,我将其大小设置为600 x 300,然后创建了3个JPanel(左、右和上面板以包含其他两个) 我已经将GridLayout设置为UpperBothJPanel,使其具有相同大小的表,并将GridBagLayout设置为两个(左侧和右侧JPanel),以便可以分别修改其宽度。

我目前正试图为自己的工作做一个小项目,以帮助我完成日常琐事。我试图在Swing中创建一个小表,并开始添加组件。然而,我面临着一些困难

首先,让我强调一下我需要实现的目标。下图:

根据下面的代码示例,我尝试创建一个主容器,我将其大小设置为600 x 300,然后创建了3个JPanel(左、右和上面板以包含其他两个)

我已经将GridLayout设置为UpperBothJPanel,使其具有相同大小的表,并将GridBagLayout设置为两个(左侧和右侧JPanel),以便可以分别修改其宽度。 然后我在左侧添加了3个jlabel,在右侧添加了3个jlabel。 然后将右侧和左侧JPanel添加到上面板

然而,我遇到了一些困难:

  • 如果我更改主容器的大小,从600x300改为600x300,它似乎不再工作。我不明白如果不是主容器,我现在应该将尺寸更改给谁
  • 如果我将上面板大小设置为150x200,它将不起作用。它继续占据主容器的所有空间
  • 左侧和右侧JLabel偏移且未完全对齐,尽管左侧和右侧JLabel的代码相同

    导入java.awt.Color; 导入java.awt.Container; 导入java.awt.Dimension; 导入java.awt.GridBagConstraints; 导入java.awt.GridBagLayout; 导入java.awt.GridLayout; 导入java.awt.Toolkit; 导入javax.swing.JFrame; 导入javax.swing.JLabel; 导入javax.swing.JPanel

    公共类主框架{

    public String frameTitle, projectNameString, countryNameString, projectDetailString, requestDateString;
    private int offerRevisionVal;
    
    public JFrame frame;
    public Container mainContainer;
    private Toolkit toolkit;
    private JPanel upperBothPanels, upperLeftPanel, upperRightPanel, middlePanel, lowerPanel; // Main Panels where I added my smaller Objects
    private GridBagLayout upperLeftGridLayout, upperRightGridLayout;
    private GridLayout upperBothGridLayout;
    
    
    // UPPER OBJECTS
    private JLabel projectLabel, countryLabel, projectDetailLabel, offerLabel, requestDateLabel, emptyLabel;
    
    
    public Main(){
    
        frameTitle = "CRANES QUOTATIONS";
    
        // MAIN INSTANCE VARIABLES DEFINITION
        projectNameString = "Project Title"; // TO ADD LATER 
        countryNameString = "Brazil"; // TO ADD LATER
        projectDetailString = "32 Cranes biTravi"; // TO ADD LATER
        offerRevisionVal = 01; // TO ADD LATER
        requestDateString = "20-April-2017"; // tTO ADD LATER
    
    
        setTitle(frameTitle); // Added the main title of my App
        mainContainer = getContentPane(); // set up a Container to contain all my objects
        mainContainer.setBackground(Color.GREEN); // set the color of my ContentPane
    
        // UPER PANEL DEFINITION
        upperLeftPanel = new JPanel();
        //upperLeftPanel.setBorder(BorderFactory.createDashedBorder(Color.red, 5, 5));
    
    
        // Definition place of my upper Grid Bag Layouts. One to hold them all.
        upperBothGridLayout = new GridLayout(2,1);
    
            upperLeftGridLayout = new GridBagLayout();
    
    
    
    
        upperLeftPanel.setBackground(Color.GREEN);
        upperLeftPanel.setLayout(upperLeftGridLayout);
    
    
        // PLACE LEFT GRID
        GridBagConstraints c = new GridBagConstraints();    
    
            projectLabel = new JLabel(projectNameString);
            c.weighty = 0.5;
            c.fill = GridBagConstraints.BOTH;
            c.gridx=0;
            c.gridy=0;
            upperLeftPanel.add(projectLabel, c);
    
            countryLabel = new JLabel(countryNameString);
            c.weighty=0.5;
            c.fill = GridBagConstraints.BOTH;
            c.gridx=0;
            c.gridy=1;
            upperLeftPanel.add(countryLabel, c);
    
            projectDetailLabel = new JLabel(projectDetailString);
            c.weighty = 0.5;
            c.fill = GridBagConstraints.BOTH;
            c.gridx = 0;
            c.gridy = 2;
            upperLeftPanel.add(projectDetailLabel, c);
    
            // PLACE RIGHT GRID
            upperRightPanel = new JPanel();
            upperRightPanel.setBackground(Color.WHITE);
            upperRightGridLayout = new GridBagLayout();
            upperRightPanel.setLayout(upperRightGridLayout);
            //upperRightPanel.setBorder(BorderFactory.createDashedBorder(Color.BLUE, 5, 5));
    
            GridBagConstraints d = new GridBagConstraints();    
    
            offerLabel = new JLabel(String.valueOf(offerRevisionVal));
            d.weighty = 0.5;
            d.fill = GridBagConstraints.BOTH;
            d.gridx=0;
            d.gridy=0;
            upperRightPanel.add(offerLabel, d);
    
            requestDateLabel = new JLabel(requestDateString);
            d.weighty=0.5;
            d.fill = GridBagConstraints.BOTH;
            d.gridx=0;
            d.gridy=1;
            upperRightPanel.add(requestDateLabel, d);
    
            emptyLabel = new JLabel();
            d.weighty = 0.5;
            d.fill = GridBagConstraints.BOTH;
            d.gridx = 0;
            d.gridy = 2;
            upperRightPanel.add(emptyLabel, d);
    
    
    
            upperBothPanels = new JPanel(new GridLayout(1,2));
            upperBothPanels.setSize(150,200);
            upperBothPanels.add(upperLeftPanel);
            upperBothPanels.add(upperRightPanel);
    
    
        // ===========================================================================  ADD THE PANELS
    
    
        mainContainer.add(upperBothPanels);
    
        // =========================================================================== 
        setSize(600, 300); // set the height and width of my window
        centerToScreen ();
        setVisible(true); // set visibility 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // sets up what it does when I close the App.
    
    }
    
    // Method to center to screen
    public void centerToScreen (){
        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize(); // gets the screen size
        setLocation(size.width / 2 - getWidth() / 2, size.height / 2 - getHeight() / 2); // sets the location
    }
    
    }

  • /////////////////////

    import javax.swing.SwingUtilities;
    
    public class CranesApp {
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable(){
    
                @Override
                public void run() {
                    new Main();
    
                }
    
            });
    
    
        }
    
    }
    
    提前谢谢大家,,
    致以最诚挚的问候

    如何创建和使用JTable?如果我添加了一个JTable以包含我的3行2列(如上所述),那么我是否可以在一个新JTable的下面添加10行4列,并按照之前的JTable设置所有最大大小?