1.简单的产生一个空白的框架
package bjfu.dianzi.wzz;
import java.awt.EventQueue;
import javax.swing.*;
public class SimpleFrameTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SimpleFrame frame=new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//define the action when the user close the frame
frame.setVisible(true);// set the frame visable
}
});
}
}
class SimpleFrame extends JFrame
{
private static final int width=300;
private static final int height=200;
SimpleFrame()
{
setSize(width,height);
}
}
2.根据当前电脑分辨率设置框架的大小以及图标
package bjfu.dianzi.wzz;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import javax.script.ScriptException;
import javax.swing.*;
public class SimpleFrameTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SimpleFrame frame=new SimpleFrame();
frame.setTitle("SizeJframe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
Toolkit kit =Toolkit.getDefaultToolkit();//get a object of toolkit
Dimension screenSize=kit.getScreenSize();//get the screenSize of current computer
int screenWidth=(int) screenSize.getWidth();
int screenHeight=(int) screenSize.getHeight();
setSize(screenWidth/2,screenHeight/2);
setLocationByPlatform(true);
Image img=new ImageIcon("icon.gif").getImage();
setIconImage(img);
}
}
3在框架内部加上一个字符
框架内部通过一个component组件实现这一功能
package notHelloWorld;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class NotHelloWorld {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame=new NotHelloWorldFrame();
frame.setTitle("NotHelloWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
add(new NotHelloWorldComponent());
pack();
}
}
class NotHelloWorldComponent extends JComponent
{
public static final int X=75;
public static final int Y=100;
private static final int width=300;
private static final int height=200;
public void paintComponent(Graphics g)
{
g.drawString("Not a hellow World program", X, Y);
}
public Dimension getPreferredSize(){return new Dimension(width, height);}
}