2013年8月12日星期一

Seek guidance getPreferredSize () usage

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Graphics;
public class ShowRect extends JFrame{
private static final long serialVersionUID = 1L;
public ShowRect(){
setTitle("ShowRect");
getContentPane().add( new RectJPanel() );
}

public static void main(String[] args){
ShowRect frame = new ShowRect();
frame.setSize(450,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class RectJPanel extends JPanel{
private static final long serialVersionUID = 1L;

protected void paintComponent( Graphics g ){
super.paintComponent(g);
int xx,yy=0;
for(int i = 1;i<=4;i++){
xx = 0;
for( int j=1;j<=4;j++){
g.setColor(Color.white);
g.fillRect(xx,yy,50,50);
xx += 50;
g.setColor(Color.BLACK);
g.fillRect(xx,yy,50,50);
xx += 50;
}
yy +=50;
xx = 0;
for( int j=1;j<=4;j++){
g.setColor(Color.BLACK);
g.fillRect(xx,yy,50,50);

xx += 50;
g.setColor(Color.WHITE);
g.fillRect(xx,yy,50,50);
xx += 50;
}
yy += 50;
}

}
public Dimension getPreferredSize(){
return new Dimension( 200,200 );
}
}
}


I want out of this display screen change automatically as the window changes , but with getPreferredSize () does not work, ask how to use this method , how to get out of the picture as the window display changes automatically change ~ < br> ------ Solution ----------------------------------------- ---
swing with setPreferredSize methods generally do not override the getPreferredSize method .

JFrame calling pack
------ For reference only ------------------------------- --------
top
up ...
------ For reference only ------------------------------ ---------
main inside plus a
frame.pack ();
In addition to your black and white plaid number , size is limited, a total of eight grid

没有评论:

发表评论