gpt4 book ai didi

java - 使用 GridBagLayout

转载 作者:行者123 更新时间:2023-11-30 03:26:14 24 4
gpt4 key购买 nike

我以前从未使用过布局,据我了解, GridBagLayout 可能不是最容易开始的布局,但无论如何我都在尝试使用它。我正在尝试将 GridBagLayout 用于我的 JPanel,这样当我调整它的大小时,组件就会增长。我只需要它来保持组件的自然高度,但我尝试使用 GridBagConstraints.HORIZONTAL 来自动调整组件的宽度。我有代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.io.*;

public class NumberConverterDesignTest
{
JFrame numberConversionWindow = new JFrame("Number Conversion");

JPanel numberPanel = new JPanel();

////////COMPONENTS////////
JLabel lblTitle;
JLabel lblBase2;
JLabel lblBase8;
JLabel lblBase10;
JLabel lblBase16;
JTextField txtBase2;
JTextField txtBase8;
JTextField txtBase10;
JTextField txtBase16;

///////////Font///////////
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 45);

public void numberConvertGUI()
{
numberConversionWindow.setBounds(10, 10, 420, 300);

numberConversionWindow.setMinimumSize(new Dimension(420, 300));

numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

numberConversionWindow.setLayout(new GridLayout(1,1));

createNumberConversionPanel();

numberConversionWindow.getContentPane().add(numberPanel);

numberConversionWindow.setVisible(true);
}

public void createNumberConversionPanel()
{
numberPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

lblTitle = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
lblTitle.setText("Number Converter");
lblTitle.setHorizontalAlignment(JLabel.CENTER);
lblTitle.setFont(FontT5);
numberPanel.add(lblTitle, c);

lblBase2 = new JLabel();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
lblBase2.setText("Binary: ");
lblBase2.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase2);

lblBase8 = new JLabel();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
lblBase8.setText("Octal: ");
lblBase8.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase8);

lblBase10 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
lblBase10.setText("Decimal: ");
lblBase10.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase10);

lblBase16 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
lblBase16.setText("Hexadecimal: ");
lblBase16.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase16);

txtBase2 = new JTextField();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
txtBase2.setText("");
numberPanel.add(txtBase2);

txtBase8 = new JTextField();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
txtBase8.setText("");
numberPanel.add(txtBase8);

txtBase10 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
txtBase10.setText("");
numberPanel.add(txtBase10);

txtBase16 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 4;
txtBase16.setText("");
numberPanel.add(txtBase16);
}

public static void main(String[] args)
{
NumberConverterDesignTest nC = new NumberConverterDesignTest();
nC.numberConvertGUI();
}
}

编译后我得到

Incorrect Panel

我的目标是尝试并获得

What I am trying to get

我相信问题出在我设置的 weightx 上,但是我不确定如何设置它才能得到我想要的结果。任何帮助将不胜感激

最佳答案

每次调用 numberPanel.add 方法时,您都需要传递更新后的约束。

关于java - 使用 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30149490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com