编程语言
首页 > 编程语言> > java – GroupLayout:垂直和水平组

java – GroupLayout:垂直和水平组

作者:互联网

我正在尝试创建一个带有GroupLayout的小Jpanel.尽可能多地关注documentation以及查看了一些StackOverflow问题,我仍然陷入困境.

错误如下:

Exception in thread “AWT-EventQueue-0”
java.lang.IllegalStateException:
javax.swing.JButton[,0,0,0×0,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"))
                    )
            );

解决方法:

新的JButton(“投资”)创建了一个新按钮,它与之前使用新JButton创建的按钮(“投资”)不同.

在布局之前移动按钮的初始化:

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,swing,grouplayout
来源: https://codeday.me/bug/20190612/1228207.html