Java GridBagConstraints 的问题

http://img4.mukewang.com/60ee96ec0001941b05940247.jpg

对于我个人项目的一部分,我想在我的程序中添加一个音乐播放器。但是,当我尝试使用 GridBagConstraintsLayout 在标签正下方制作 3 个按钮(播放、暂停和停止)时,布局看起来就像我附加的图片一样奇怪。我不想让播放按钮很长。我希望所有 3 个按钮的长度相等并直接位于标签下方。有人可以帮我解决这个问题,也许可以发布代码来帮助我吗?附上我现在拥有的图片,我还包含了一些代码。


panel = new JPanel(new GridBagLayout());

        add(panel);

        GridBagConstraints c = new GridBagConstraints();

        label1 = new JLabel("This is the MaryLand State Song. After exiting, press enter"); 

        c.gridx = 0;

        c.gridy = 0;


        panel.add(label1,c);

        play = new JButton("Play");

        c.gridx = 0;

        c.gridy = 2;

        c.fill = GridBagConstraints.HORIZONTAL;

        panel.add(play,c);

        pause = new JButton("Pause");

        c.gridx = 1;

        c.gridy = 2;

        c.fill = GridBagConstraints.HORIZONTAL;

        panel.add(pause,c);

        stop = new JButton("Stop");

        c.gridx = 2;

        c.gridy = 2;

        c.fill = GridBagConstraints.HORIZONTAL;

        panel.add(stop,c);


DIEA
浏览 208回答 2
2回答

茅侃侃

希望这可以帮助GridBagLayout lay = new GridBagLayout();// three columns with same weightslay.columnWeights = new double[] {1,1,1};panel = new JPanel(lay);add(panel);GridBagConstraints c = new GridBagConstraints();label1 = new JLabel("This is the MaryLand State Song. After exiting, press enter"); c.gridx = 0; c.gridy = 0;// make label span all next to last columns (cells)c.gridwidth = GridBagConstraints.REMAINDER;panel.add(label1,c);c.gridwidth = GridBagConstraints.BOTH;c.fill = GridBagConstraints.HORIZONTAL;play = new JButton("Play");c.gridx = 0; c.gridy = 1;panel.add(play,c);pause = new JButton("Pause");c.gridx = 1; c.gridy = 1;panel.add(pause,c);stop = new JButton("Stop");c.gridx = 2; c.gridy = 1;panel.add(stop,c);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java