⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttondrawtest.java

📁 一些员创的简单源码
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonDrawTest
{
   public static void main(String[] args)
   {
      DrawFrame frame=new DrawFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}

class DrawFrame extends JFrame
{
   public DrawFrame()
   {
      Toolkit kit=Toolkit.getDefaultToolkit();
      Image img = kit.getImage("icon.gif");
      setIconImage(img);
      setTitle("ButtonDraw");
      setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

      DrawPanel panel=new DrawPanel();
      Container content=getContentPane();
      content.add(panel);
  }
   public static  final int DEFAULT_WIDTH=400;
   public static  final int DEFAULT_HEIGHT=400;
}

class DrawPanel extends JPanel
{
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawString("press the button ",MESSAGE_X,MESSAGE_Y);

	}
	public DrawPanel()
	{
		JButton not=new JButton("not");
		JButton hello=new JButton("hello");
		JButton world=new JButton("world");
		add(not);
		add(hello);
		add(world);
		DrawAction notAction=new DrawAction("not",70,100);
		DrawAction helloAction=new DrawAction("hello",170,100);
		DrawAction worldAction=new DrawAction("world",250,100);
		hello.addActionListener(helloAction);
		world.addActionListener(worldAction);
		not.addActionListener(notAction);
	}
	private class DrawAction implements ActionListener
	{
		public DrawAction(String s ,int leftX,int topY)
		{
			x=leftX;
			y=topY;
			string=s;
		}
		public void actionPerformed(ActionEvent event)
		{
            Graphics g=getGraphics();
            g.drawString(string,x,y);

		}
		private int x;
		private int y;
		private String string;
	}
	private int MESSAGE_X=200;
	private int MESSAGE_Y=200;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -