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

📄 work5.java

📁 按照要求采用Graphics类中绘图方法绘制出如图的图表
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Mycanvas extends JPanel
{  Mycanvas canvas; //创建Mycanvas
	public void paintComponent(Graphics g)
	   { super.paintComponent(g);  //调用父类方法
		 g.setColor(Color.black);  //设置字体颜色
	     Font f=new Font("隶书",Font.BOLD,40); //设置字体
	     g.setFont(f);
	     g.drawString("== 企业奖金发放标准 ==",230,40); //绘制标题 
	     Font f1=new Font("宋体",Font.BOLD,20);
	     g.setFont(f1);
	     g.drawString("提成 (%)",190,70);  // 绘制坐标轴项
	     g.drawString("利润(万)",720,400);
	     Font f2=new Font("宋体",Font.PLAIN,15); //设置字体
	     g.setFont(f2);
	     g.drawString("12%",200,88); // 绘制坐标轴数值
	     g.drawString("10%",200,135);
	     g.drawString("8%",200,180);
	     g.drawString("6%",200,235);
	     g.drawString("4%",200,285);
	     g.drawString("2%",200,335);
	     g.drawString("0%",200,385);
	     g.drawString("<=10",260,400);
	     g.drawString("10~20",330,400);
	     g.drawString("20~40",410,400);
	     g.drawString("40~60",500,400);
	     g.drawString("60~100",570,400);
	     g.drawString(">100",650,400);
	     
	     g.setColor(Color.lightGray); // 设置背景颜色
		 g.fillRect(230,80,480,300);  // 绘制背景矩形
		 g.setColor(Color.black);
		 g.drawLine(230,130,710,130 );
		 g.drawLine(230,180,710,180 );
		 g.drawLine(230,230,710,230 );
		 g.drawLine(230,280,710,280 );
		 g.drawLine(230,330,710,330 );
		 g.drawLine(230,380,710,380 );
		 
		 g.drawLine(230,80,230,380 );
		 g.drawLine(310,375,310,380 );
		 g.drawLine(390,375,390,380 );
		 g.drawLine(470,375,470,380 );
		 g.drawLine(550,375,550,380 );
		 g.drawLine(630,375,630,380 );
		 g.drawLine(710,375,710,380 );
		 
		 g.setColor(Color.orange);   // 设置矩形域颜色
		 g.fillRect(252,180,35,200);
		 g.setColor(Color.blue);
		 g.fillRect(330,195,35,185);
		 g.setColor(Color.magenta);
		 g.fillRect(410,255,35,125);
		 g.setColor(Color.yellow);
		 g.fillRect(490,305,35,75);
		 g.setColor(Color.pink);
		 g.fillRect(570,340,35,40);
		 g.setColor(Color.green);
		 g.fillRect(650,355,35,25);
		 
	   }
	}
public class work5 extends JApplet implements ActionListener 
{  int x;  // 定义x为整型 
   double y;  // 定义y为双精度型
   String str;  //变量str用来输出信息
   JPanel panel; //创建JPanel
   Mycanvas canvas; //创建Mycanvas
   JTextField txt;  //利润值
   JLabel lbltxt,msg; //当月利润 
   JButton button;  //"确定"按钮
   public void init()
   {
   canvas=new Mycanvas();
   canvas.setLocation(50,20);
   canvas.setBackground(Color.white); //设置背景颜色
   panel=new JPanel(new FlowLayout(FlowLayout.LEADING,100,50));
   panel.setBorder(BorderFactory.createTitledBorder("请输入当月利润")); //加边框效果
   lbltxt=new JLabel("当月利润:");
   txt=new JTextField(10);  //创建一个文本框
   txt.setBorder(BorderFactory.createLineBorder(Color.blue));//加边框效果
   msg=new JLabel("请输入利润数,然后按[确定]按钮");
   button=new JButton("确定");
   button.addActionListener(this); //监听组件button
   panel.add(lbltxt);  // 添加组件
   panel.add(txt);
   panel.add(button);
   panel.add(msg);
   Container con=getContentPane();
   con.setLayout(new BorderLayout(0,0)); //设置容器的布局方式
   con.add(panel,BorderLayout.PAGE_END);
   con.add(canvas,BorderLayout.CENTER);
	}
   public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource()==button)
		  {x=Integer.parseInt(txt.getText()); 
		   if(x<=10)  //对利润值进行判断
		   { y=x*0.1;}
		   if(10<x&&x<=20)
		   { y=(x-10)*0.075;}
		   if(20<x&&x<=40)
		   { y=(x-20)*0.05;}
		   if(40<x&&x<=60)
		   { y=(x-40)*0.03;}
		   if(60<x&&x<=100)
		   { y=(x-60)*0.015;}
		   if(x>100)
		   { y=(x-100)*0.01;}
		   str="当月应发奖金为"+y+"万元";
		   msg.setText(str);
		  }
	 }
}  

⌨️ 快捷键说明

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