import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics;
public class CenteredFrameTest {
public static void main (String [] args){
CenteredFrame frm = new CenteredFrame();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frm.pack();
frm.setVisible(true);//frm.setBackColor(Color.green);
}
}
class CenteredFrame extends JFrame {
public CenteredFrame(){
Toolkit tkt = Toolkit.getDefaultToolkit();
//int w = tkt.getScreenSize().width;
//int h = t
Dimension screenSize = tkt.getScreenSize();
int screenHeight = screenSize.height;
int screenWeight = screenSize.width;
setSize(screenWeight/2,screenHeight/2);
setLocation(screenWeight/4,screenHeight/4);
setTitle( "Centered Frame ");
//setTitle( "Centered Frame ");
Image img = tkt.getImage( "icon.gif ");
//setBackground(Color.green);
setIconImage(img);
}
public void paint(Graphics g){
//g.setBackColor(Color.green);在这也不行,那再在哪试呢???
g.setColor(Color.red);
g.drawString( "not a hello world!! ",150,80);
}//只写这个,它的显示效果是:
//1,没有Frame背景色设置,2,Frame上一切与整个Windons的背景混在一起。3.只有在先把它最小化再还原时才能显示出想要的效果。
//先加上Frame的背景色设置。
}
我用这个也不行getContentPane().setBackground(Color.white);怎么回事呢???
------解决方案--------------------
setBackground(Color.white);直拉用这个试试