137.全屏 Dimesion d = Toolkit.getDefaultToolkit().getScreenSize() ; frame.setBounds(0,0, d.width, d.height);
或者用这种方法,用robot做。附:将Frame最大化的程序源码 RobotDemo.java 注:在WindowsNT 4.0 SP6 及 Windows 2000下测试通过,使用Sun JDK1.3为JVM。 /** * File name: RobotDemo.java * Description: Demo use java.awt.Robot to maximize a frame. * Copyright: Copyright (c) 2001 * Organization: Java board at http://bbs.whnet.edu.cn * @author Jack */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RobotDemo { public static void main(String args[]) { // set up frames and its location JFrame frame = new JFrame("RobotDemo"); frame.setSize(100,100); frame.setLocation(100, 100); frame.setVisible(true); //listen the Window closing event. frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); maximizeFrame(frame); } /** * use java.awt.Robot to create a mouse click event at Frame Maximize button * @param frame The frame to be maximized */ static void maximizeFrame(JFrame frame){ try{ // create a robot to feed in GUI events Robot robot = new Robot( ); // move cursor to the top, right corner, the window Maximize button Point p = frame.getLocationOnScreen( ); Dimension s=frame.getSize(); robot.mouseMove(p.x+s.width - 30, p.y + 5); // press and release left mouse button, i.e. Click the mouse right button robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); }catch(AWTException exception){ exception.printStackTrace(); } } }参考资料 Sun JDK的bug report 4071554:不能最大化窗体 http://developer.java.sun.com/developerWorks/bugParade/bugs/4071554.html 更多的关于java.awt.Robot请参见http://java.sun.com/j2se/1.3/docs/api/java/awt/Robot.html 关于键盘事件(例如具体的键盘字符的映射),请见http://java.sun.com/j2se/1.3/docs/api/java/awt/event/KeyEvent.html 要查阅鼠标事件,请见http://java.sun.com/j2se/1.3/docs/api/java/awt/event/MouseEvent.html
import java.awt.*; import java.awt.event.*; import javax.swing.*;public class FullScreenTest { public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); FullScreenWindow myWindow = new FullScreenWindow(); if ( gd.isFullScreenSupported() ) gd.setFullScreenWindow(myWindow); else System.out.println("Unsupported full screen."); } }class FullScreenWindow extends JWindow { public FullScreenWindow() { this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { quit(); } }); }
public void quit() { this.dispose(); }
public void paint(Graphics g) { g.setFont(new Font("Arial",Font.BOLD,30)); g.setColor(Color.RED); g.drawString("?是全屏幕模式",100,100);
Dimesion d = Toolkit.getDefaultToolkit().getScreenSize() ;
frame.setBounds(0,0, d.width, d.height);
* File name: RobotDemo.java
* Description: Demo use java.awt.Robot to maximize a frame.
* Copyright: Copyright (c) 2001
* Organization: Java board at http://bbs.whnet.edu.cn
* @author Jack
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class RobotDemo {
public static void main(String args[]) {
// set up frames and its location
JFrame frame = new JFrame("RobotDemo");
frame.setSize(100,100);
frame.setLocation(100, 100);
frame.setVisible(true);
//listen the Window closing event.
frame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
maximizeFrame(frame);
} /**
* use java.awt.Robot to create a mouse click event at Frame Maximize button
* @param frame The frame to be maximized
*/
static void maximizeFrame(JFrame frame){
try{
// create a robot to feed in GUI events
Robot robot = new Robot( ); // move cursor to the top, right corner, the window Maximize button
Point p = frame.getLocationOnScreen( );
Dimension s=frame.getSize();
robot.mouseMove(p.x+s.width - 30, p.y + 5); // press and release left mouse button, i.e. Click the mouse right button
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK); }catch(AWTException exception){
exception.printStackTrace();
}
}
}参考资料 Sun JDK的bug report 4071554:不能最大化窗体
http://developer.java.sun.com/developerWorks/bugParade/bugs/4071554.html 更多的关于java.awt.Robot请参见http://java.sun.com/j2se/1.3/docs/api/java/awt/Robot.html 关于键盘事件(例如具体的键盘字符的映射),请见http://java.sun.com/j2se/1.3/docs/api/java/awt/event/KeyEvent.html 要查阅鼠标事件,请见http://java.sun.com/j2se/1.3/docs/api/java/awt/event/MouseEvent.html
import java.awt.event.*;
import javax.swing.*;public class FullScreenTest
{
public static void main(String[] args)
{ GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
FullScreenWindow myWindow = new FullScreenWindow();
if ( gd.isFullScreenSupported() )
gd.setFullScreenWindow(myWindow);
else
System.out.println("Unsupported full screen."); }
}class FullScreenWindow extends JWindow
{
public FullScreenWindow()
{
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
quit();
}
});
}
public void quit()
{
this.dispose();
}
public void paint(Graphics g)
{
g.setFont(new Font("Arial",Font.BOLD,30));
g.setColor(Color.RED);
g.drawString("?是全屏幕模式",100,100);
}
}