这个JAVA程序怎么让他堆满整个屏幕啊

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class aaa {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int x = 0;
int y = 0;
for (int i = 0; i < 20; i++) {
JFrame frame = new JFrame();

JTextArea area = new JTextArea("welcom to textarea!");
area.setText("lol");
frame.add(area);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
x= x+200;

frame.setBounds(x , y , 300 , 100);
Thread.sleep(100);
}

}
}

获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public static Point getMidDimesion(Dimension d)
{
Point p = new Point();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
p.setLocation((dim.width - d.width)/2,(dim.height - d.height)/2);
return p;
}
public static void main(String[] args)
{
new testFrame(300,200);
}
}
温馨提示:答案为网友推荐,仅供参考