Java程序设计教程(第三版) 雍俊海 9.6 答案/代码

时间:2023-02-25 10:19:08

交个作业,交个作业


题目:

单位换算。请设计并编写小应用程序,要求实现至少5对单位之间的相互换算,

例如:镑与公斤之间换算:党输入12磅时,程序可以自动换算成5.346公斤




代码:


匿名内部类的:


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Translation extends JFrame{

	private JTextField [] t = {
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6)
	};

	//这里想要什么换算可以自己换
	private JLabel [] lb = {
			new JLabel("公斤"),
		    new JLabel("斤"),
		    new JLabel("米"),
		    new JLabel("厘米"),
		    new JLabel("元"),
		    new JLabel("毛"),
		    new JLabel("小时"),
		    new JLabel("分钟"),
		    new JLabel("分钟"),
		    new JLabel("秒")
	};
	
	private JButton [] b = {
			new JButton("="),
			new JButton("="),
			new JButton("="),
			new JButton("="),
			new JButton("=")
	};
	
	public Translation(){
		init();
	}
	
	public void showView(){
		setVisible(true);
	}
	
	
	public void init(){
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		//可以自行调试出,符合自己要求的大小
		setSize(300,300);
		
		//按照格式把各个符号加入
		for(int i = 0 ; i<5 ; i++){
			c.add(t[2*i]);
			c.add(lb[2*i]);
			c.add(b[i]);
			c.add(t[2*i+1]);
			c.add(lb[2*i+1]);
		}
		
		//因为每一种的换算方法会不一样,所以直接用匿名内部类了,各种换算方法可以自己调
		b[0].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t[1].setText(Double.parseDouble(t[0].getText())*2 + "");
			}
		});
		
		b[1].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t[3].setText(Double.parseDouble(t[2].getText())*10 + "");
			}
		});
		
		b[2].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t[5].setText(Double.parseDouble(t[4].getText())*10 + "");
			}
		});
		
		b[3].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t[7].setText(Double.parseDouble(t[6].getText())*60 + "");
			}
		});
		
		b[4].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t[9].setText(Double.parseDouble(t[8].getText())*60 + "");
			}
		});
		
	}
	
	
	public static void main(String[] args) {
		Translation tt =new Translation();
		tt.showView();
	}
	
}


外部的:


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class trans extends JApplet implements ActionListener{

	private JTextField [] t = {
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6),
			new JTextField(6)
	};

	//这里想要什么换算可以自己换
	private JLabel [] lb = {
			new JLabel("公斤"),
		    new JLabel("斤"),
		    new JLabel("米"),
		    new JLabel("厘米"),
		    new JLabel("元"),
		    new JLabel("毛"),
		    new JLabel("小时"),
		    new JLabel("分钟"),
		    new JLabel("分钟"),
		    new JLabel("秒")
	};
	
	private JButton [] b = {
			new JButton("="),
			new JButton("="),
			new JButton("="),
			new JButton("="),
			new JButton("=")
	};
	
	
	public void init(){
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		//可以自行调试出,符合自己要求的大小
		setSize(300,300);
		
		//按照格式把各个符号加入
		for(int i = 0 ; i<5 ; i++){
			c.add(t[2*i]);
			c.add(lb[2*i]);
			c.add(b[i]);
			c.add(t[2*i+1]);
			c.add(lb[2*i+1]);
		}

		for(int i = 0 ; i<5 ; i++){
			b[i].addActionListener(this);
		}	
	}
	
	public void actionPerformed(ActionEvent e){
		JButton n = new JButton();
		n = (JButton) e.getSource();
		if(n == b[0]){
			t[1].setText(Double.parseDouble(t[0].getText())*2 + "");
		}
		else if(n == b[1]){
			t[3].setText(Double.parseDouble(t[2].getText())*10 + "");
		}
		else if(n == b[2]){
			t[5].setText(Double.parseDouble(t[4].getText())*10 + "");
		}
		else if(n == b[3]){
			t[7].setText(Double.parseDouble(t[6].getText())*60 + "");
		}
		else if(n == b[4]){
			t[9].setText(Double.parseDouble(t[8].getText())*60 + "");
		}
		
	}
	
}