2016-05-24 1 views
0

JButtonの二つのディスプレイでは、2つのクラス私のJButtonが一貫して表示されていない、助けてくださいそれは同じ大きさではなく、2つの場所やサイズが同じ場所

がありませんそれはサイズと位置 と一致していませんおかげ コードは、2つのセクション

最終パブリッククラスChessMain {

JFrame frame; 

public static void main(String[] args) { 
    new ChessMain().go(); 
} 

private void go() { 
    frame = new JFrame("Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // Container contentPane = frame.getContentPane(); 
    frame.setContentPane(new DrawPanelChess()); 
    frame.setVisible(true); 
    frame.setExtendedState(Frame.MAXIMIZED_BOTH); 

    Icon warnIcon = new ImageIcon("white_King_Chess.png"); 
    JButton button2 = new JButton(warnIcon); 
    button2.setLayout(null); 
    button2.setBounds(100,0, 100, 100); 
    frame.add(button2); 


} 

}

にダウン以下であります10

クラスDrawPanelChessはJPanelの{

private static final long serialVersionUID = 9166938856558245300L; 

ます。public void paintComponent(グラフィックスグラム){

 g.setColor(Color.BLACK); 
     g.fillRect(0, 0, this.getWidth(), this.getHeight());  


     g.setColor(Color.WHITE); 

     {//row 1       
     g.fillRect(0, 0, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/4, 0, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/2, 0, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 6, 0, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 2       
     g.fillRect(this.getWidth()/8, this.getHeight()/8, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 3, this.getHeight()/8, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 5, this.getHeight()/8, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 7, this.getHeight()/8, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 3       
     g.fillRect(0, this.getHeight()/4, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/4, this.getHeight()/4, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/2, this.getHeight()/4, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 6, this.getHeight()/4, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 4       
     g.fillRect(this.getWidth()/8, this.getHeight()/8 * 3, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 3, this.getHeight()/8 * 3, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 5, this.getHeight()/8 * 3, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 7, this.getHeight()/8 * 3, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 5       
     g.fillRect(0, this.getHeight()/2, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/4, this.getHeight()/2, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/2, this.getHeight()/2, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 6, this.getHeight()/2, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 6       
     g.fillRect(this.getWidth()/8, this.getHeight()/8 * 5, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 3, this.getHeight()/8 * 5, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 5, this.getHeight()/8 * 5, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 7, this.getHeight()/8 * 5, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 7       
     g.fillRect(0, this.getHeight()/8 * 6, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/4, this.getHeight()/8 * 6, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/2, this.getHeight()/8 * 6, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 6, this.getHeight()/8 * 6, this.getWidth()/8, this.getHeight()/8);       
     } 

     {//row 8       
     g.fillRect(this.getWidth()/8, this.getHeight()/8 * 7, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 3, this.getHeight()/8 * 7, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 5, this.getHeight()/8 * 7, this.getWidth()/8, this.getHeight()/8);       
     g.fillRect(this.getWidth()/8 * 7, this.getHeight()/8 * 7, this.getWidth()/8, this.getHeight()/8);       
     } 

     // System.out.println("\u2654"); 

     repaint(); 

} }あなたがあなたのDrawPanelChessを追加した後のフレームにボタンを追加するようだ

答えて

0

を拡張しますJPanelをフレームのcontentPaneとして返します。フレームサイズを変更すると、フレームの位置が変わります。将来のために周りにいない偉大な仕事、あなたがより多くのコンポーネントを追加する予定が、時間のためにそれを避けるためには

frame.setResizable(false); 

を追加している場合、私はまた、あなたがそれに沿って自分自身を配置する場所を指定するには、GridBagLayoutのようなものを使用することをお勧めしチェスのパネルで。

関連する問題