gpt4 book ai didi

java - GroupLayout:垂直和水平组

转载 作者:搜寻专家 更新时间:2023-11-01 01:53:32 24 4
gpt4 key购买 nike

我正在尝试创建一个前面带有 GroupLayout 的小型 Jpanel。关注了documentation尽可能多地查看了一些 StackOverflow 问题,我仍然被困住了。

错误如下:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@5eef2e7c,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Invest,defaultCapable=true] is not attached to a vertical group

我知道问题与按钮的安装位置有关。毕竟错误明确说明了。但是,我就是想不通应该以何种方式附加它们。有什么想法吗?

    JPanel panel = new JPanel();

GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);


panel.setMinimumSize(new Dimension(2000,100));
panel.setBorder(BorderFactory.createTitledBorder((cdo.getTicker()) + " : (" + cdo.getCurrency() + ")"));


layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(new JButton("Invest")))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(new JButton("Ignore")))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(new JButton("Article")))

);


layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(new JButton("Invest"))
.addComponent(new JButton("Ignore"))
.addComponent(new JButton("Article"))
)
);

最佳答案

new JButton("Invest") 创建一个新按钮,它不同于之前使用 new JButton("Invest") 创建的按钮。

将按钮的初始化移到布局之前:

JButton investButton = new JButton("Invest");
JButton articleButton = new JButton("Article");
JButton ignoreButton = new JButton("Ignore");

layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(investButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ignoreButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(articleButton)));

layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(investButton)
.addComponent(ignoreButton)
.addComponent(articleButton)));

关于java - GroupLayout:垂直和水平组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159812/

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