我正在尝试创建一个带有GroupLayout的小Jpanel。尽可能地跟踪documentation并查看了一些StackOverflow问题,我仍然陷入困境。
错误如下:
线程“AWT-EventQueue-0”中的异常 java.lang.IllegalStateException: javax.swing.JButton中[,0,0,0x0,无效,alignmentX = 0.0,alignmentY = 0.5,边界= com.apple.laf.AquaButtonBorder $动态@ 5eef2e7c,旗帜= 288,MAXIMUMSIZE =,=的minimumSize,首选大小=,的DefaultIcon =,disabledIcon =,disabledSelectedIcon =,余量= javax.swing.plaf.InsetsUIResource [顶= 0,左= 2,底部= 0,右= 2],的paintBorder =真,paintFocus =真,pressedIcon =,rolloverEnabled =假,rolloverIcon =,rolloverSelectedIcon =,selectedIcon =,text = Invest,defaultCapable = true]未附加到垂直组
我知道问题与按钮的连接位置有关。在所有错误明确说明之后。但是,我无法弄清楚我应该以什么方式附加它们。有什么想法吗?
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"))
)
);
答案 0 :(得分:7)
new JButton("Invest")
会创建一个新按钮,该按钮与之前使用新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)));