この質問に多少関連しています。
幅が 2:1 の比率の 2 つのパネル (左側のパネルが右側のパネルよりも広い) を持つウィンドウを作成しようとしており、GridBagLayout を使用しています。
基本的に、次のようになります。
____________________ | | | | | | | | 左パネル |右- | | | |パネル | | | |->| | | |__________|__|________| | | | | 両方のパネルが分離 縦区切りで
と の両方を保持
するトップレベルJPanel
(私はそれを と呼んでいます) を作成するコードは次のとおりです。totalGui
leftPanel
rightPanel
private JPanel createContentPane(){ //creates a top-level JPanel
//that is returned to the JFrame's
//setContentPane() method
int WINHEIGHT = 600;
int WINWIDTH = 800;
JPanel totalGui = new JPanel(); //this is the JPanel that will be returned
totalGui.setBackground(Color.white);
totalGui.setMinimumSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setPreferredSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setMaximumSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setLayout(new GridBagLayout());
totalGui.setOpaque(true);
GridBagConstraints c = new GridBagConstraints();
/* totalGui (the top-level JPane) has two JPanes called
leftPanel and rightPanel. The widths of leftPanel and rightPanel
are in the ratio 2:1. So, leftPanel takes up 2/3rd of the totalGui's
width, and rightPanel gets the remaining 1/3rd */
/* Top-level left panel */
JPanel leftPanel = new JPanel();
int LEFTPANELWIDTH = WINWIDTH*2/3;
leftPanel.setMinimumSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
leftPanel.setPreferredSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
leftPanel.setMaximumSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
c.gridx = 0;
c.gridy = 0;
c.gridheight=4; //both leftPanel and rightPanel are 4 cells high
c.gridwidth=2; //leftPanel is 2 cells wide, rightPanel is 1 cell wide (code below)
c.fill = GridBagConstraints.VERTICAL;
totalGui.add(leftPanel, c);
/* Vertical separator between leftPanel and rightPanel */
c.gridx = 2; //'2' because leftPanel is 2 cells wide and the separator's leading
//edge must lie in the third cell (gridx = 2) if it is to be
//sandwiched between leftPanel and rightPanel
c.gridy = 0;
c.gridwidth=0; //I've also tried gridwidth=1, but it doesn't solve the problem
c.fill = GridBagConstraints.NONE;
c.anchor=GridBagConstraints.CENTER;
JSeparator verticalsep = new JSeparator(SwingConstants.VERTICAL);
verticalsep.setPreferredSize(new Dimension(6,WINHEIGHT-10)); //the 'WINHEIGHT-10' bit is purely cosmetic
//I just wanted some space between the
//separator edges and the window border
//(also why the anchor is set to CENTER)
totalGui.add(verticalsep,c); //PROBLEM: verticalsep is not displayed where I expected it to be;
//please see attached image in post
/* Top-level right panel */
JPanel rightPanel = new JPanel();
int RIGHTPANELWIDTH = WINWIDTH*1/3;
rightPanel.setBackground(Color.blue);
rightPanel.setMinimumSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
rightPanel.setPreferredSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
rightPanel.setMaximumSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
c.gridx = 2;
c.gridy = 0;
c.gridwidth=1; //rightPanel is 1 cell wide, leftPanel is 2 cells wide (code above)
c.gridheight=4; //both leftPanel and rightPanel are 4 cells high
c.fill = GridBagConstraints.VERTICAL;
totalGui.add(rightPanel, c);
return totalGui;
}
私の問題は、区切り記号が正しい場所に表示されていないことです。これが私が得たものです:画像へのリンク(申し訳ありませんが、画像を投稿するのに十分な評判がありません).
誰が私が間違っているのか教えてもらえますか?
あなたが与えることができるどんな助けにも感謝します。