2016-05-24 4 views
0

JButton zwei Displays in 2 Stellen und Größen gibt es zwei KlassenMein JButton ist die Anzeige nicht konsequent, es ist nicht die gleiche Größe und nicht an der gleichen Stelle

Es ist nicht im Einklang mit der Größe und Lage Bitte helfen dank Der Code ist unten unten in zwei Abschnitten

final public class 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); 


} 

}

Klasse DrawPanelChess erstreckt JPanel {

private static final long serialVersionUID = 9166938856558245300L; 

public void paintcomponent (Graphics g) {

 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(); 

} }

Antwort

0

Es Ihre Hinzufügen der Schaltfläche mit dem Rahmen erscheint, nachdem Sie Ihre DrawPanelChess hinzufügen JPanel als contentPane für den Frame. Es positioniert sich neu, wenn Sie die Bildgröße ändern. Nicht eine große Arbeit um für die Zukunft, wenn Sie beabsichtigen, weitere Komponenten hinzufügen, aber es für die Zeit, zu vermeiden, dass hinzufügen

frame.setResizable(false); 

Ich würde auch vorschlagen, so etwas wie ein GridBagLayout mit angeben, wo Sie es wollen, sich positionieren entlang mit der Schachplatte.

Verwandte Themen