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

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

GridBagLayout - 意外添加/更改間距

GridBagLayout - 意外添加/更改間距

慕森王 2022-06-04 09:45:34
JTextAreas所以,當我添加面板時,面板(包含)之間的間距發生了變化,請參見這張圖片。:D例子當一個按鈕被按下時,第一次addTextArea()被調用。狀態 1 -> 圖中的狀態 2。問題是 panel_buttons 與新添加的WorkDescription( JTextArea) 不太接近。并且當多次按下按鈕時,它們之間的間距會發生變化。按鈕之前做了很大的跳躍,但是有; c.weighty = 0.1 - 0.3跳躍更小。// The panel is placed in the center of a JFrame (BorderLayout)public CONSTRUCTOR {    JPanel panel = new JPanel(new GridBagLayout());    GridBagConstraints c = new GridBagConstraints();    // ...    c.anchor = GridBagConstraints.NORTHWEST;    c.gridx = 0;    c.gridy = 0;    c.weightx = 1;    c.weighty = 1;    c.gridwidth = 1;    c.gridheight = 1;    // this is how everything looks at (first pic) start.    panel.add(panel_buttons, c);   // panel_buttons is at the right place}添加一個新的方法WorkDescription,即a JTextArea:public void addTextArea() {    WorkDescription wd = new WorkDescription(); //WorkDescription extends JPanel    panel.remove(panel_buttons);     c.weighty = 0.25;       // I've messed around with the weighty alot.                            // 0.2-0.25 makes the panel_buttons do the least amout of 'down-jump'    panel.add(wd, c);    if(c.gridy < 3 ) {        c.gridy ++;        c.weighty = 1;        panel.add(panel_buttons, c);    }    panel.revalidate();    panel.repaint();}
查看完整描述

1 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

我發現的最佳解決方案是,


從 切換GridBagLayout到GridLayout。


private JPanel panel_Center = new JPanel(new GridLayout(5,1)); 

然后,當然,刪除所有GridBagConstraints


public void addTextArea() {


    WorkDescription wd = new WorkDescription();


    panel.remove(panel_buttons); 

    panel.add(wd);


    if(addedWorks < 4 ) {       


        addedWorks++;

        panel.add(panel_buttons);


    }


    panel.revalidate();

    panel.repaint();

}


查看完整回答
反對 回復 2022-06-04
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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