我爱吃薯片儿 2021-06-24 15:21 采纳率: 20%
浏览 26

java实验四实验五代码是啥

本人大一学生,希望大佬能帮帮孩子

  • 写回答

1条回答 默认 最新

  • CSDN专家-Fay 2021-06-24 15:24
    关注

    计算器的:

    package com.ht.test;
    
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class Login extends JFrame implements ActionListener {
    	Button b1, b2, b3, b4;
    	JTextField text, text1, res;
    
    	public Login() {
    		super("计算器");
    
    		// 设置布局(设置没有布局,才能自己定位)
    		setLayout(null);
    
    		Label uname = new Label("操作数1:");
    		// 加入窗口中
    		add(uname);
    		// 设置组件位置 (组件的x坐标,组件的y坐标,组件的长,组件的高)
    		uname.setBounds(20, 50, 40, 20);
    
    		// 操作数1
    		text = new JTextField();
    		add(text);
    		text.setBounds(65, 50, 200, 20);
    
    		// 操作数2
    		Label pwd = new Label("操作数2:");
    		add(pwd);
    		pwd.setBounds(20, 80, 40, 20);
    
    		// 操作数2
    		text1 = new JTextField();
    		add(text1);
    		text1.setBounds(65, 80, 200, 20);
    
    		// 结果
    		Label rest = new Label("结果:");
    		add(rest);
    		rest.setBounds(20, 110, 40, 20);
    
    		// 结果
    		res = new JTextField();
    		add(res);
    		res.setBounds(65, 110, 200, 20);
    
    		// 按钮1
    		b1 = new Button("加法");
    		b1.setForeground(Color.red);
    		add(b1);
    		b1.setBounds(20, 140, 80, 30);
    
    		// 按钮2
    		b2 = new Button("减法");
    		b2.setForeground(Color.blue);
    		add(b2);
    		b2.setBounds(120, 140, 80, 30);
    
    		// 按钮2
    		b3 = new Button("乘法");
    		b1.setForeground(Color.green);
    		add(b3);
    		b3.setBounds(220, 140, 80, 30);
    
    		// 按钮2
    		b4 = new Button("除法");
    		b1.setForeground(Color.yellow);
    		add(b4);
    		b4.setBounds(320, 140, 80, 30);
    
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    
    		setSize(440, 250);
    		setVisible(true);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    
    	public static void main(String[] args) {
    		new Login();
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		float t1 = Float.parseFloat(text.getText());
    		float t2 = Float.parseFloat(text1.getText());
    		if(e.getSource()==b1){
    			res.setText((t1+t2)+"");
    		}else if(e.getSource()==b2){
    			res.setText((t1-t2)+"");
    		}else if(e.getSource()==b3){
    			res.setText((t1*t2)+"");
    		}else if(e.getSource()==b4){
    			res.setText((t1/t2)+"");
    		}
    	}
    }
    

    文件读取:

    package com.test.server;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    
    
    public class Test2 {
    	public static void main(String[] args) {
    		Reader fr = null;
    		int length = 0;
    		char ch [] = null;
    		//创建字符输入流对象,负责读取文件
    		try {
    			fr =new FileReader("C:\\text.txt");
    			//创建中转站数组,存放读取的内容
    			ch = new char[1024];
    			//读取文件内容到ch数组中
    			length = fr.read(ch);
    			//输出保存在ch数组中文件内容
    			System.out.println(new String(ch ,0,length));
    			
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			if(null!=fr){
    				try {
    					fr.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错
  • ¥15 服务器上的网站安装php5.6版本
  • ¥15 请大咖一起探索iptv 直播源的hls通过反向代理解密
  • ¥100 寻找技术员 云闪付tn转h5输入卡号付款的链接 重酬!