我已經搜索了幾個小時,但仍無法找到我需要的答案。GridBagLayout與JPanels
所以我試圖設置一個簡單的佈局,我想要的是一個GridBagLayout 它包含兩個JPanel。構建版面應該看起來像下面的圖片 。
我已經搜索了幾個小時,但仍無法找到我需要的答案。GridBagLayout與JPanels
所以我試圖設置一個簡單的佈局,我想要的是一個GridBagLayout 它包含兩個JPanel。構建版面應該看起來像下面的圖片 。
心中已經使用的BoxLayout得到它。
import java.awt.Color;
public class ProportionalPanels extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public ProportionalPanels() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
final JPanel panel_A = new JPanel();
panel_A.setBackground(new Color(255, 165, 0));
contentPane.add(panel_A);
final JPanel panel_B = new JPanel();
panel_B.setBackground(new Color(176, 196, 222));
contentPane.add(panel_B);
panel_A.setMinimumSize(new Dimension(0, 0));
panel_B.setMinimumSize(new Dimension(0, 0));
panel_A.setPreferredSize(new Dimension(0, 0));
panel_B.setPreferredSize(new Dimension(0, 0));
panel_A.setMaximumSize(new Dimension(Short.MAX_VALUE/5, Short.MAX_VALUE));
panel_B.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
System.out.printf("Panel width rate: %.2f %n",
panel_B.getSize().width/(double) panel_A.getSize().width);
}
});
}
/**
* Launch the application.
*/
public static void main(String[] args) {
ProportionalPanels frame = new ProportionalPanels();
frame.setVisible(true);
}
}
謝謝!你真棒:) – user3324382
如果它真的幫助你也可以標記答案是有用的) –
你嘗試過什麼?很可能沒有人會在這裏開始爲你的工作編碼。 – Zavior
答案是什麼?你怎麼了?你卡在哪裏?你需要的一切在這裏解釋http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html,第一個去的地方,你肯定已經讀了幾個小時的調查。現在,什麼不明確? – DSquare
網格袋佈局可以爲此工作,左側面板將有gridx = 0,右側會有gridx = 1。您可以使用GridBagConstraints的weightx字段獲取不同的大小。正如@Zavior所說,去試試這個併發布你明確的問題/你卡在哪裏,我很樂意幫助更多。 – hankd