2013-02-04 67 views
0

下面的代碼產生9個單獨的JPanel,其中有9個按鈕。這九個JPanel使用GridLayout排列在基本JPanel上。然後使用邊框佈局將此基本JPanel放置到ContentPane上。我爲JButtons和每個單獨的JPanel使用邊界來清楚地定義它們的分離。每個JPanel內部的JButton看起來都很好,但是JPanel之間存在差距,導致出現了一個雙線,這個雙線正在竊聽我。我試着直接將每個JPanel添加到ContentPane中,但這沒有任何作用。完全難住,想知道有沒有人有任何建議?GridLayout中的JPanel之間的差距

P.S.我知道大量的重複代碼需要以不好的方式進行重新分解,並且這將僅用於模擬目的。

謝謝,

import java.awt.*; 
import javax.swing.*; 
import javax.swing.UIManager.*; 

public class View1 extends JFrame 
{ 
    private JPanel  basePanel; 
    private JPanel  childPanel1; 
    private JPanel  childPanel2; 
    private JPanel  childPanel3; 
    private JPanel  childPanel4; 
    private JPanel  childPanel5; 
    private JPanel  childPanel6; 
    private JPanel  childPanel7; 
    private JPanel  childPanel8; 
    private JPanel  childPanel9; 
    private int   row = 3; 
    private int   column = 3; 
    private JButton[][] squares1; 
    private JButton[][] squares2; 
    private JButton[][] squares3; 
    private JButton[][] squares4; 
    private JButton[][] squares5; 
    private JButton[][] squares6; 
    private JButton[][] squares7; 
    private JButton[][] squares8; 
    private JButton[][] squares9; 
    private Font  numberFont; 

public View1() 
{ 
    setSize(400,400); 
    setTitle("2013"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    basePanel = new JPanel(); 
    basePanel.setLayout(new GridLayout(row, column, 0, 0)); 
    numberFont = new Font("sansserif", Font.BOLD, 32); 

    childPanel1Setup(); 
    childPanel2Setup(); 
    childPanel3Setup(); 
    childPanel4Setup(); 
    childPanel5Setup(); 
    childPanel6Setup(); 
    childPanel7Setup(); 
    childPanel8Setup(); 
    childPanel9Setup(); 

    basePanel.add(childPanel1); 
    basePanel.add(childPanel2); 
    basePanel.add(childPanel3); 
    basePanel.add(childPanel4); 
    basePanel.add(childPanel5); 
    basePanel.add(childPanel6); 
    basePanel.add(childPanel7); 
    basePanel.add(childPanel8); 
    basePanel.add(childPanel9); 

    Container cp = getContentPane(); 
    cp.setLayout(new BorderLayout()); 
    cp.add(basePanel, BorderLayout.CENTER); 
} 

public void childPanel1Setup() 
{ 
    childPanel1 = new JPanel(); 
    childPanel1.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel1.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares1 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares1[rows][cols] = new JButton(); 
      squares1[rows][cols].setSize(32, 32); 
      squares1[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares1[rows][cols].setBackground(Color.WHITE); 
      childPanel1.add(squares1[rows][cols]); 
     } 
    } 
} 

public void childPanel2Setup() 
{ 
    childPanel2 = new JPanel(); 
    childPanel2.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares2 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares2[rows][cols] = new JButton(); 
      squares2[rows][cols].setSize(32, 32); 
      squares2[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares2[rows][cols].setBackground(Color.WHITE); 
      childPanel2.add(squares2[rows][cols]); 
     } 
    } 
} 

public void childPanel3Setup() 
{ 
    childPanel3 = new JPanel(); 
    childPanel3.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel3.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares3 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares3[rows][cols] = new JButton(); 
      squares3[rows][cols].setSize(32, 32); 
      squares3[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares3[rows][cols].setBackground(Color.WHITE); 
      childPanel3.add(squares3[rows][cols]); 
     } 
    } 
} 

public void childPanel4Setup() 
{ 
    childPanel4 = new JPanel(); 
    childPanel4.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel4.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares4 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares4[rows][cols] = new JButton(); 
      squares4[rows][cols].setSize(32, 32); 
      squares4[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares4[rows][cols].setBackground(Color.WHITE); 
      childPanel4.add(squares4[rows][cols]); 
     } 
    } 
} 

public void childPanel5Setup() 
{ 
    childPanel5 = new JPanel(); 
    childPanel5.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel5.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares5 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares5[rows][cols] = new JButton(); 
      squares5[rows][cols].setSize(32, 32); 
      squares5[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares5[rows][cols].setBackground(Color.WHITE); 
      childPanel5.add(squares5[rows][cols]); 
     } 
    } 
} 

public void childPanel6Setup() 
{ 
    childPanel6 = new JPanel(); 
    childPanel6.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel6.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares6 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares6[rows][cols] = new JButton(); 
      squares6[rows][cols].setSize(32, 32); 
      squares6[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares6[rows][cols].setBackground(Color.WHITE); 
      childPanel6.add(squares6[rows][cols]); 
     } 
    } 
} 

public void childPanel7Setup() 
{ 
    childPanel7 = new JPanel(); 
    childPanel7.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel7.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares7 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares7[rows][cols] = new JButton(); 
      squares7[rows][cols].setSize(32, 32); 
      squares7[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares7[rows][cols].setBackground(Color.WHITE); 
      childPanel7.add(squares7[rows][cols]); 
     } 
    } 
} 

public void childPanel8Setup() 
{ 
    childPanel8 = new JPanel(); 
    childPanel8.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel8.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares8 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares8[rows][cols] = new JButton(); 
      squares8[rows][cols].setSize(32, 32); 
      squares8[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares8[rows][cols].setBackground(Color.WHITE); 
      childPanel8.add(squares8[rows][cols]); 
     } 
    } 
} 

public void childPanel9Setup() 
{ 
    childPanel9 = new JPanel(); 
    childPanel9.setLayout(new GridLayout(row, column, 0, 0)); 
    childPanel9.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    squares9 = new JButton[row][column]; 

    for(int rows = 0; rows < this.row; rows++) 
    { 
     for(int cols = 0; cols < this.column; cols++) 
     { 
      squares9[rows][cols] = new JButton(); 
      squares9[rows][cols].setSize(32, 32); 
      squares9[rows][cols].setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      squares9[rows][cols].setBackground(Color.WHITE); 
      childPanel9.add(squares9[rows][cols]); 
     } 
    } 
} 

}

+6

上帝之母...學會使用數組/列表...你不應該甚至實物模型做到這一點,它是慢讀,慢寫,爲了解更糟糕... – Jimmt

+1

使用'MigLayout': ) –

+0

不要調用'setSize(400,400);'。相反,爲自然沒有的內容定義首選大小,並在'setVisible(true)'前設置'pack()'GUI。 –

回答

1

GridLayout的使所有的組分是相同的大小。如果需要在JPanel內部佈置3x3組件的矩陣(100x100像素),則每個組件將爲33x33像素,並且在右側和底部將存在額外的像素,因爲100不是3的因子。如果JPanel爲101x101像素,所有四個邊緣都會有額外的像素。

考慮使用GridBagLayout或Box或其他比GridLayout更智能的佈局。

3

我相信這是因爲JPanel s的佈局管理器是FlowLayout其中包括一個餘量。當你創建面板時,將它們的佈局設置爲GridLayout,它會給你0的餘量。

您還可以參加GridLayoutsetVgap(0);setHgap(0);嗎?