java swing-基本的框架结构

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);}
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值