gpt4 book ai didi

java - GroupLayout 和组件大小跨度

转载 作者:行者123 更新时间:2023-12-02 06:20:20 24 4
gpt4 key购买 nike

我正在使用 SWING 创建一个 Java 应用程序作为 UI。我对 LayoutManager 的选择是 GroupLayout,但现在我遇到了组件问题。

enter image description here

基本布局由三列和两行组成(有嵌套行,如下面的代码所示),第二行仅包含一个带有 JCheckBox 列表的 JPane。

我的问题是我想以跨列的方式插入 JPanel,而不影响其他列的大小(即 Canvas 必须是正方形而不是矩形)。

是否可以或者我必须更改 LayoutManager?

代码如下:

    //create and set LayoutManager
GroupLayout gp = new GroupLayout(this.getContentPane());
gp.setAutoCreateContainerGaps(true);
gp.setAutoCreateGaps(true);
this.setLayout(gp);
//set alignment criteria
GroupLayout.Alignment hAlign = GroupLayout.Alignment.TRAILING;
GroupLayout.Alignment vAlign = GroupLayout.Alignment.BASELINE;

//add component into layout
//set horizontal group
gp.setHorizontalGroup(gp.createSequentialGroup()
.addGroup(gp.createParallelGroup(hAlign)
.addComponent(imageCanvas)
.addComponent(densitiesPanel))
.addGroup(gp.createParallelGroup(hAlign)
.addComponent(projectPathField)
.addComponent(sourceDensityLabel)
.addComponent(sourceSizeLabel))
.addGroup(gp.createParallelGroup(hAlign)
.addComponent(projectPathButton)
.addComponent(sourceDensityComboBox)
.addComponent(sourceSizeTextField))
);

//set vertical group
gp.setVerticalGroup(gp.createSequentialGroup()
.addGroup(gp.createParallelGroup(vAlign)
.addComponent(imageCanvas)
.addGroup(gp.createSequentialGroup()
.addGroup(gp.createParallelGroup(vAlign)
.addComponent(projectPathField)
.addComponent(projectPathButton))
.addGroup(gp.createParallelGroup(vAlign)
.addComponent(sourceDensityLabel)
.addComponent(sourceDensityComboBox))
.addGroup(gp.createParallelGroup(vAlign)
.addComponent(sourceSizeLabel)
.addComponent(sourceSizeTextField)))
)
.addComponent(densitiesPanel)
);

最佳答案

我给大家分享一个GroupLayout的例子。我希望你喜欢它:)

enter image description here

import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.Alignment.LEADING;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class MyForm extends JFrame {

public MyForm() {

JLabel jlblTitle = new JLabel("Form Builder");

JLabel jlblUsername = new JLabel("Username:");
JLabel jlblGroup = new JLabel("Group:");
JLabel jlblAddress = new JLabel("Address:");

JComboBox jcmbxUsername = new JComboBox();
JComboBox jcmbxGroup = new JComboBox();
JComboBox jcmbxAddress = new JComboBox();

JButton jbtnUsername = new JButton("New Username");
JButton jbtnGroup = new JButton("New Group");
JButton jbtnAddress = new JButton("New Address");

JButton jbtnCreate = new JButton("Create");

JPanel jpanUser = new JPanel();

GroupLayout layout = new GroupLayout(jpanUser);

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(jlblTitle)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(jlblUsername)
.addComponent(jlblGroup)
.addComponent(jlblAddress))
.addGroup(
layout.createParallelGroup(LEADING)
.addComponent(jcmbxUsername)
.addComponent(jcmbxGroup)
.addComponent(jcmbxAddress)
.addComponent(jbtnCreate, Alignment.CENTER))
.addGroup(
layout.createParallelGroup(LEADING)
.addComponent(jbtnUsername)
.addComponent(jbtnGroup)
.addComponent(jbtnAddress)))));

layout.linkSize(SwingConstants.HORIZONTAL, jbtnUsername, jbtnGroup,
jbtnAddress);

layout.setVerticalGroup(layout
.createSequentialGroup()
.addGroup(
layout.createParallelGroup(BASELINE)
.addComponent(jlblTitle))
.addGroup(
layout.createParallelGroup(LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(BASELINE)
.addComponent(jlblUsername)
.addComponent(jcmbxUsername)
.addComponent(jbtnUsername))
.addGroup(
layout.createParallelGroup(BASELINE)
.addComponent(jlblGroup)
.addComponent(jcmbxGroup)
.addComponent(jbtnGroup))
.addGroup(
layout.createParallelGroup(BASELINE)
.addComponent(jlblAddress)
.addComponent(jcmbxAddress)
.addComponent(jbtnAddress))
.addComponent(jbtnCreate))));

jpanUser.setLayout(layout);

BoxLayout layoutMain = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
getContentPane().setLayout(layoutMain);

add(jpanUser);

setTitle("My form");
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager
.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
// UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
new MyForm().setVisible(true);
}
});
}
}

关于java - GroupLayout 和组件大小跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075863/

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