我试图用它前面的GroupLayout创建一个小的Jpanel。尽可能多地遵循documentation并查看了许多StackOverflow问题,我仍然陷入困境。

错误如下:



我知道问题与按钮的连接位置有关。毕竟错误明确指出了这一点。但是,我只是不知道应该以哪种方式附加它们。有任何想法吗?

    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/

10-08 23:36