GroupLayout:垂直和水平组 [英] GroupLayout: Vertical and Horizontal Groups

查看:231
本文介绍了GroupLayout:垂直和水平组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个前面带有GroupLayout的小型Jpanel.尽可能遵循文档并进行了研究许多StackOverflow问题,我仍然陷入困境.

I'm attempting to create a small Jpanel with a GroupLayout infront of it. Having followed the documentation as much as possible as well as looked at a number of StackOverflow questions, I'm still stuck.

错误如下:

线程"AWT-EventQueue-0"中的异常 java.lang.IllegalStateException: javax.swing.JButton [,0,0,0x0,无效,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]未附加到垂直组

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

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

I know that the problem is related to where the buttons are being attached. After all the error says it explicitly. However, I just can't figure out in what manner I'm supposed to attach them. Any ideas?

    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")创建的按钮不同.

new JButton("Invest") creates a new button, which is different from the button previously created using new JButton("Invest").

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

Move the initializations of the buttons before the layout:

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)));

这篇关于GroupLayout:垂直和水平组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆