您的位置:首页 > 编程语言 > Java开发

java swing-基本的框架结构

2016-06-07 17:56 323 查看
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);}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: