Gridbag 布局无法正常工作(Java Swing)

问题描述 投票:0回答:1

我正在向 gridbag 布局添加组件,但最后一行一直出现在不同的列中


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class SignUpFrame extends JFrame implements ActionListener{
    
    NameFieldPanel namePan, surnamePan, nickPan, passPan, mailPan, agePan;
    MyTextField name, surname, nick, pass, mail, age;
    MyLabel nameL, surnameL, nickL, passL, mailL, ageL;
    JButton submitBtn, link, pfpBtn;
    JPanel totalPanel;
    JComboBox tier;
    
    public SignUpFrame() {
        
        //////////////frame settings//////////////
        
        ImageIcon image = new ImageIcon("src\\inst.jpg");
        this.setIconImage(image.getImage());
        this.setTitle("MyApp");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        this.getContentPane().setBackground(Color.WHITE);
        this.setLayout(new GridBagLayout());
        
        
        //////////////buttons//////////////
                
        submitBtn = new JButton("Log In");
        submitBtn.setAlignmentX(CENTER_ALIGNMENT);
        submitBtn.setFont(new Font("Sans Serif", 20, 20));
        submitBtn.setForeground(Color.white);
        submitBtn.setPreferredSize(new Dimension(200, 40));
        submitBtn.setMaximumSize(new Dimension(200, 40));
        submitBtn.setBorder(new LineBorder(Color.black, 1));
        submitBtn.setFocusPainted(false);
        submitBtn.setBackground(Color.LIGHT_GRAY);
        submitBtn.addActionListener(this);
        
        link = new JButton("Already a member? Log in");
        link.setFont(new Font("Sans Serif", 20, 20));
        link.setForeground(Color.black);
        link.setBorder(null);
        link.setBackground(null);
        link.setFocusPainted(false);
        link.addActionListener(this);
        
        pfpBtn = new JButton("Profile Picture");
        pfpBtn.setFont(new Font("Sans Serif", 20, 20));
        pfpBtn.setForeground(Color.black);
        pfpBtn.addActionListener(this);
        pfpBtn.setBorder(new LineBorder(Color.black, 1));
        pfpBtn.setFocusPainted(false);
        
        String[] tiers = {"Free", "Hobbyist", "Professional"};
        tier = new JComboBox<String>(tiers);
        
        
        //////////////Labels//////////////

        nameL = new MyLabel("Name:");
        surnameL = new MyLabel("Surname:");
        nickL = new MyLabel("Nickname:");
        passL = new MyLabel("Password:");
        mailL = new MyLabel("Email");
        ageL = new MyLabel("Age: ");
        
        
        //////////////textareas//////////////
        
        name = new MyTextField();
        surname = new MyTextField();
        nick = new MyTextField();
        pass = new MyTextField();
        mail = new MyTextField();
        age = new MyTextField();
        
        
        //////////////Panels//////////////
        
        namePan = new NameFieldPanel(nameL, name);
        surnamePan = new NameFieldPanel(surnameL, surname);
        nickPan = new NameFieldPanel(nickL, nick);
        passPan = new NameFieldPanel(passL, pass);
        mailPan = new NameFieldPanel(mailL, mail);
        agePan = new NameFieldPanel(ageL, age);
        totalPanel = new JPanel();
        totalPanel.setBackground(Color.white);
        totalPanel.setLayout(new GridBagLayout());
        
        //////////////adding elements to page//////////////
        
        GridBagConstraints gc = new GridBagConstraints();
        gc.insets = new Insets(10, 10, 10, 10);
//      gc.gridheight = 3;
        gc.gridx = 0;
        gc.gridy = 0;
        totalPanel.add(namePan, gc);
        gc.gridx = 1;
        gc.gridy = 0;
        totalPanel.add(surnamePan, gc);
        gc.gridx = 0;
        gc.gridy = 1;
        totalPanel.add(nickPan, gc);
        gc.gridx = 1;
        gc.gridy = 1;
        totalPanel.add(passPan, gc);
        gc.gridx = 0;
        gc.gridy = 2;
        totalPanel.add(mailPan, gc);
        gc.gridx = 1;
        gc.gridy = 2;
        totalPanel.add(agePan, gc);
        gc.gridx = 0;
        gc.gridy = 3;
        totalPanel.add(pfpBtn);
        gc.gridx = 1;
        gc.gridy = 3;
        totalPanel.add(tier);
        
        GridBagConstraints g = new GridBagConstraints();
        g.gridx = 0;
        g.gridy = 0;
        this.add(totalPanel);
        g.gridx = 0;
        g.gridy = 1;
        g.insets = new Insets(10, 10, 10, 10);
        this.add(submitBtn, g);
        g.gridx = 0;
        g.gridy = 2;
        this.add(link, g);
        this.setVisible(true);
    }

显示的布局:

我试着改变它,添加一个新的 jpanel,改变东西的大小,但似乎没有任何效果,我不知道出了什么问题,这让我很痛苦

附言不介意裁员

java swing gridbaglayout
1个回答
0
投票

在totalPanel中添加pfpBtn和tier时没有使用gridbag约束。 请将 totalPanel.add(pfpBtn) 更改为 totalPanel.add(pfpBtn, gc) 并将 totalPanel.add(tier) 更改为 totalPanel.add(tier, gc) 并检查。

© www.soinside.com 2019 - 2024. All rights reserved.