栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 实现窗口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 实现窗口

计算机二级大题–实现窗口创建
package AWT二级代码;

import java.awt.Button;
import java.awt.frame;
import java.awt.GridLayout;

public class ShowGrid extends frame 
{
	public ShowGrid()
	{
		super("GridLayout example");
		setLayout(new GridLayout(0,2));//布局形式
		add(new Button("Button 1"));
		add(new Button("Button 2"));
		add(new Button ("Button 3"));
		add(new Button ("Button 4"));
		add(new Button ("Button 5"));
		add(new Button ("Button 6"));
		setSize(240,240);
		setVisible(true);
		
	}
	public static void main(String args[])
	{
		ShowGrid gl = new ShowGrid();
	}
}

效果如下

创建按钮
package AWT二级代码;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.frame;

public class ShowFlow extends frame 
{
	public ShowFlow()
	{
		super("FlowLayout example");
		setSize(300,100);//长宽
		setLayout(new FlowLayout());
		add(new Button("Button 1"));//创建第一个按钮
		add(new Button("Button 2"));
		add(new Button("Button 3"));
		setVisible(true);//设置可视化
	}
	
	public static void main(String args[])
	{
		ShowFlow fl = new ShowFlow();//实例化

	}
 
}


效果如下

创建一个平面
package AWT二级代码;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.frame;

public class ShowBorder extends frame
{
	public ShowBorder()
	{
		super("BorderLayout example");
		setLayout(new BorderLayout());
		add("East",new Button("东"));
		add("South",new Button("南"));
		add("West",new Button("西"));
		add("North",new Button("北"));
		add(new Button("中"));
		setVisible(true);
		setSize(300,300);
	}
	public static void main(String args[])
	{
		ShowBorder bl = new ShowBorder();
	}
}

效果如下

在窗口写字
package 计算机二级考试;

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

public class shige
{
   public static void main(String[] args)
   {
      Fontframe frame = new Fontframe();
      frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

     //*********Found********
class Fontframe extends Jframe
{
   public Fontframe()
   {
      setTitle("沁园春.雪");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      FontPanel panel = new FontPanel();
      Container contentPane = getContentPane();
     //*********Found********
      contentPane.add(panel);
   }
   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;
}

     //*********Found********
class FontPanel extends JPanel
{
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      String message = "数风流人物,还看今朝!";
      Font f = new Font("隶书", Font.BOLD, 24);
      g2.setFont(f);
      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(message, context);
      double x = (getWidth() - bounds.getWidth()) / 2;
      double y = (getHeight() - bounds.getHeight()) / 2;
      double ascent = -bounds.getY();
      double baseY = y + ascent;
      g2.setPaint(Color.RED);
     //*********Found********
      g2.drawString(message, (int)x, (int)(baseY));
   }
}

效果如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号