JAVA实验报告分析(6)

2025-11-14

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{ private JTextField text; private char op;

private float value; private boolean first; public Calculator(){ super(\ this.setSize(280,210); this.setLocation(300,240);

this.display(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); op='+'; value=0.0f; first=true; }

private void display(){

text = new JTextField(\

text.setHorizontalAlignment(JTextField.RIGHT); text.setEditable(false); this.getContentPane().add(text,\

JPanel panel = new JPanel(new GridLayout(5,4,5,5)); this.getContentPane().add(panel); String

str[]={\1\

,\

JButton buttons[] = new JButton[str.length]; for (int i=0; i

buttons[i].addActionListener(this); } }

public void actionPerformed(ActionEvent e){

String current = e.getActionCommand(); if (current==\

text.setText(\ value = 0.0f; first = true; return; }

if (current==\

String

str=text.getText().substring(0,text.getText().length()-1); text.setText(str); if (str==\

text.setText(\ first = true; } return; } if

(current.charAt(0)>='0' &&

current.charAt(0)<='9' ||

current==\ {

if (first){

text.setText(\ first = false; }

text.setText(text.getText()+current); } else {

System.out.print(\ switch (op){

case '+': value += Float.parseFloat(text.getText());break; case '-': value -= Float.parseFloat(text.getText());break; case '*': value *= Float.parseFloat(text.getText());break; case '/': value /= Float.parseFloat(text.getText());break; }

System.out.println(\ text.setText(\

op = current.charAt(0); first = true; } }

public static void main(String arg[]){ new Calculator(); } }

(实验二程序清单续1)

2. 整数进制转换

import java.awt.*; import javax.swing.*;

import javax.swing.event.*;

public class BinaryJFrame extends JFrame implements CaretListener{ private JTextField texts[]; public BinaryJFrame(){

super(\十进制整数转换\);

this.setBounds(300,240,440,140); this.setResizable(false);

this.setBackground(Color.lightGray);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));

String labelstr[]={\十进制\,\二进制\,\八进制\,\十六进制\}; texts = new JTextField[labelstr.length]; for (int i=0; i

this.getContentPane().add(new JLabel(labelstr[i])); texts[i] = new JTextField(32); texts[i].setEditable(false);

this.getContentPane().add(texts[i]); }

texts[0].setEditable(true); texts[0].setText(\);

texts[0].addCaretListener(this); this.caretUpdate(null); this.setVisible(true); }

public void caretUpdate(CaretEvent e){ try{

int i = Integer.parseInt(texts[0].getText()); texts[1].setText(Integer.toBinaryString(i)); texts[2].setText(Integer.toOctalString(i)); texts[3].setText(Integer.toHexString(i)); }

catch(NumberFormatException nfe){

JOptionPane.showMessageDialog(this,\+texts[0].getText()+\不能转换,请重新输入!\); }

finally{} }

public static void main(String arg[]){ new BinaryJFrame(); }

}

(实验二程序清单续2)

3. 模拟裁判评分。

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class ScoreJFrame extends JFrame implements ActionListener{ private JTextField texts[],text_score; private JButton button; private Average av; public ScoreJFrame(int rows, int columns, Average av){ super(\模拟裁判评分\

this.setBounds(300,240,400,150); this.setBackground(Color.lightGray);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel panel1 = new JPanel(new GridLayout(rows, columns)); this.getContentPane().add(panel1); texts=new JTextField[rows*columns]; for (int i=0; i

texts[i].setHorizontalAlignment(JTextField.CENTER); panel1.add(texts[i]); }

JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); this.getContentPane().add(panel2, \ button=new JButton(\平均分\ panel2.add(button);

button.addActionListener(this); text_score = new JTextField(\ text_score.setEditable(false);

text_score.setHorizontalAlignment(JTextField.CENTER); panel2.add(text_score); this.setVisible(true); this.av=av; }

public void actionPerformed(ActionEvent e){ double table[]=new double[texts.length]; for (int i=0; i

table[i] = Double.parseDouble(texts[i].getText()); if(table[i]>10)

throw new Exception(\ }

catch(NumberFormatException nfe){

JOptionPane.showMessageDialog(this, texts[i].getText()+\字符串不能转换符点数,请重新输入。\ }

catch(Exception ex) {

if(ex.getMessage()==\

JOptionPane.showMessageDialog(this,table[i]+\分数范围超出,请重新输入!\ }

text_score.setText(String.format(\ }

public static void main(String arg[]){ new ScoreJFrame(2,5, new AverageExceptMaxMin()); } }

class AverageExceptMaxMin implements Average{ public double average(double table[]){ if (table!=null && table.length>2){

double sum=table[0], max=table[0], min=table[0]; for (int i=1; imax) max = table[i]; if (table[i]

return (sum-max-min)/(table.length-2); }

return 0.0; } }

(实验二程序清单续3)

4. 编译运行下例,然后修改程序,当使用鼠标单击后在另一位置重新绘制月亮。

import java.awt.*;

import java.awt.event.*;

class Mycanvas extends Canvas implements MouseListener{


JAVA实验报告分析(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:08机械原理习题解答改1

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219