亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java GridBagConstraints 的問題

Java GridBagConstraints 的問題

DIEA 2021-07-09 17:11:38
對于我個人項目的一部分,我想在我的程序中添加一個音樂播放器。但是,當我嘗試使用 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);
查看完整描述

2 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

希望這可以幫助


GridBagLayout lay = new GridBagLayout();

// three columns with same weights

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


查看完整回答
反對 回復 2021-07-14
  • 2 回答
  • 0 關注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號