Java Swing工具的布局问题

huangapple go评论113阅读模式
英文:

Layout problems with Java Swing Tools

问题

我正在尝试创建一个简单的GUI注册代码窗口,其中包含一个输入代码的文本框和一个用于验证输入的按钮,就像这张图片一样:

Java Swing工具的布局问题

所以我尝试使用Java Swing库来创建这个GUI,以下是我的代码:

import java.awt.*;
import javax.swing.*;

public class MainClass {

    public static void main(String[] args) {

        final JFrame appFrame = new JFrame();
        final JPanel gridPanel = new JPanel(new GridLayout(2,1));

        Font f = new Font(Font.SERIF, Font.PLAIN, 20);
        JButton registerButton = new JButton("Register!");
        registerButton.setFont(f);
        JTextField verificationCodeText = new JTextField("--在此输入您的注册码--");

        registerButton.setSize(30,20);
        verificationCodeText.setSize(50,20);
        verificationCodeText.setHorizontalAlignment(SwingConstants.CENTER);
        Border border = BorderFactory.createLineBorder(Color.BLACK, 5);
        verificationCodeText.setFont(f);
        verificationCodeText.setBorder(border);

        gridPanel.add(verificationCodeText);
        gridPanel.add(registerButton);

        appFrame.add(gridPanel);
        appFrame.setSize(400,400);
        appFrame.setVisible(true);

        System.out.println("End");
    }
}

但是我得到了这个结果:

Java Swing工具的布局问题

我可以如何使用布局来纠正这个问题?

(注意:忽略第一张图片中的外部黑色边框,它仅用于说明目的)

英文:

I am trying to make a simple GUI registration code window, that has a text to enter the code, and a button to verify if the code entered is right or wrong. Exactly like this image :

Java Swing工具的布局问题

So I tried to do this GUI with java using Java Swing library, and that is my code:

import java.awt.*;
import javax.swing.*;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		final JFrame appFrame = new JFrame();
		final JPanel gridPanel = new JPanel(new GridLayout(2,1));
		
		
		Font f = new Font(Font.SERIF, Font.PLAIN, 20);
		JButton registerButton = new JButton("Register!");
		registerButton.setFont(f);
		JTextField verificationCodeText = new JTextField("--Enter your registration key here--");
		
		registerButton.setSize(30,20);
		verificationCodeText.setSize(50,20);
		verificationCodeText.setHorizontalAlignment(SwingConstants.CENTER);
		Border border = BorderFactory.createLineBorder(Color.BLACK, 5);
		verificationCodeText.setFont(f);
		verificationCodeText.setBorder(border);
		
		gridPanel.add(verificationCodeText);
		gridPanel.add(registerButton);
	
		appFrame.add(gridPanel);
		appFrame.setSize(400,400);
		appFrame.setVisible(true);
		
		System.out.println("End");
	}
}

But that what I had obtained :

Java Swing工具的布局问题

What can I do with layouts to correct that?

(Note: Ignore the outer black frame in the first image, it is only for clarification purposes)

答案1

得分: 3

最好嵌套使用JPanels,根据需要混合布局,并使用填充,比绝对设置大小更好。例如,尝试类似以下代码的方式,下面的示例使用边界布局作为整体布局,然后在主要的JPanel上添加一个使用JPanel(默认JPanel布局)作为顶部和一个使用GridBagLayout的JPanel作为中心的FlowLayout。如果以默认方式添加GridBag,则会居中一个单独的组件:

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class RegistrationGUI extends JPanel {
    private JButton registerButton = new JButton("Register!");
    private JTextField verificationCodeText = new JTextField("--在此输入注册密钥--", 20);
    
    public RegistrationGUI() {
        JPanel topPanel = new JPanel();
        int gap = 35;
        topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        verificationCodeText.setHorizontalAlignment(SwingConstants.CENTER);
        topPanel.add(verificationCodeText);
        
        JPanel bottomPanel = new JPanel(new GridBagLayout());
        gap = 60;
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        bottomPanel.add(registerButton);

        
        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.PAGE_START);
        add(bottomPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        RegistrationGUI mainPanel = new RegistrationGUI();
        JFrame frame = new JFrame("Registration");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

Java Swing工具的布局问题

英文:

Best to nest JPanels, mix layouts as needed, and use padding such as via empty borders, which is better than setting size absolutely. Play with code similar to this for instance, the example below uses a BorderLayout as the overall layout, and then adds a FlowLayout using JPanel (default JPanel layout) to the top and a GridBagLayout-using JPanel to the center of the main JPanel. GridBag will center a single component if added in a default (no constraints) fashion:

Java Swing工具的布局问题

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class RegistrationGUI extends JPanel {
private JButton registerButton = new JButton("Register!");
private JTextField verificationCodeText = new JTextField("--Enter your registration key here--", 20);
public RegistrationGUI() {
JPanel topPanel = new JPanel();
int gap = 35;
topPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
verificationCodeText.setHorizontalAlignment(SwingConstants.CENTER);
topPanel.add(verificationCodeText);
JPanel bottomPanel = new JPanel(new GridBagLayout());
gap = 60;
bottomPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
bottomPanel.add(registerButton);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(bottomPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
RegistrationGUI mainPanel = new RegistrationGUI();
JFrame frame = new JFrame("Registration");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}

huangapple
  • 本文由 发表于 2020年8月1日 20:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63205327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定