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

效果如下
在这里插入图片描述

  • 1
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Java窗口抖动,可以使用Java的AWT和Swing库中的特定方法。以下是一个简单的示例程序,可以让窗口在屏幕上抖动: ```java import java.awt.*; import javax.swing.*; public class WindowShaker { public static void shake(Window window) { final int SHAKE_DISTANCE = 5; //抖动的距离 final int SHAKE_DURATION = 1000; //抖动的持续时间 final Point startPosition = window.getLocationOnScreen(); //窗口的起始位置 final long startTime = System.currentTimeMillis(); //抖动开始时间 Timer shakeTimer = new Timer(25, event -> { long elapsed = System.currentTimeMillis() - startTime; double waveOffset = (elapsed / (double) SHAKE_DURATION) * Math.PI; int yOffset = (int) (Math.sin(waveOffset) * SHAKE_DISTANCE); window.setLocation(startPosition.x, startPosition.y + yOffset); if (elapsed >= SHAKE_DURATION) { ((Timer) event.getSource()).stop(); window.setLocation(startPosition); } }); shakeTimer.start(); } public static void main(String[] args) { JFrame frame = new JFrame("Window Shaker"); frame.setSize(300, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton shakeButton = new JButton("Shake!"); shakeButton.addActionListener(event -> shake(frame)); frame.add(shakeButton, BorderLayout.CENTER); frame.setVisible(true); } } ``` 在上面的示例程序中,我们使用了Java的Timer类来定期更新窗口的位置,使之抖动。在抖动的过程中,我们使用了正弦函数来计算窗口的偏移量,以产生平滑的抖动效果。当抖动完成后,我们将窗口的位置还原到起始位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值