gpt4 book ai didi

java - GroupLayout 的列未对齐

转载 作者:行者123 更新时间:2023-12-01 09:18:44 26 4
gpt4 key购买 nike

最左边的列是我在这里引用的内容:

/image/zXIcF.png

大约一周前,我说服我爸爸放弃 Windows 10,转而使用 Linux Mint。考虑到他多么喜欢简单的界面,到目前为止,转变相当坎坷。他不愿意使用命令行,所以我花了大半天的时间尝试制作一个 java 用户界面,这样他就不会再提示他如何无法运行脚本了。不过,我确实希望它是完美的,这种错位让我很恼火。老实说,我不明白该专栏与其他专栏相比有什么不同。

这是我的格式方法,它执行所有 GroupLayout 操作

    private void format() {
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(inputLabel)
.addComponent(outputLabel)
.addComponent(exitButton)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(inputTextField, 300, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(outputTextField, 300, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)
.addGroup(layout.createParallelGroup(LEADING).addComponent(inputButton).addComponent(outputButton)
.addComponent(compressButton)));

layout.linkSize(SwingConstants.HORIZONTAL, inputButton, outputButton);

layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(inputLabel)
.addComponent(inputTextField, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(inputButton))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(outputLabel)
.addComponent(outputTextField, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(outputButton))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(exitButton)
.addComponent(compressButton)));

setTitle("PDF CONVERTER");
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

任何帮助将不胜感激。提前致谢!

最佳答案

问题出在水平布局组中。您没有用右圆括号完成前两组。

我还对代码进行了一些改进。我通过垂直组中的基线对齐来抑制文本字段的垂直增长。

此外,最好在文本字段中定义预期的列数,而不是在布局中硬编码最小大小。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class PdfConverterEx extends JFrame {

public PdfConverterEx() {

initUI();
}

private void initUI() {

JLabel inputLabel = new JLabel("Input:");
JLabel outputLabel = new JLabel("Output:");

JTextField field1 = new JTextField(25);
JTextField field2 = new JTextField(25);

JButton browseBtn1 = new JButton("Browse");
JButton browseBtn2 = new JButton("Browse");

JButton exitBtn = new JButton("Exit");
JButton compressBtn = new JButton("Compress");

createLayout(inputLabel, outputLabel, field1, field2,
browseBtn1, browseBtn2, exitBtn, compressBtn);

setTitle("PDF Converter");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

private void createLayout(JComponent... arg) {

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[6]))
.addGroup(layout.createParallelGroup()
.addComponent(arg[2])
.addComponent(arg[3]))
.addGroup(layout.createParallelGroup()
.addComponent(arg[4])
.addComponent(arg[5])
.addComponent(arg[7]))
);

layout.linkSize(SwingConstants.HORIZONTAL, arg[5], arg[6]);

layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(arg[0])
.addComponent(arg[2])
.addComponent(arg[4]))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(arg[1])
.addComponent(arg[3])
.addComponent(arg[5]))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(arg[6])
.addComponent(arg[7])));

pack();

}

public static void main(String[] args) {

EventQueue.invokeLater(() -> {
PdfConverterEx ex = new PdfConverterEx();
ex.setVisible(true);
});
}
}

祝您迁移到 Linux 顺利。 :)

这是屏幕截图:

enter image description here

关于java - GroupLayout 的列未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40316722/

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