武汉理工大学Java程序语言设计课内实验二
Posted 我不想秃头呀z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了武汉理工大学Java程序语言设计课内实验二相关的知识,希望对你有一定的参考价值。
一、前言
本文内容:武汉理工大学Java语言程序设计第二次课内实验的内容,分为有按钮和无按钮。
开发工具:IntelliJ IDEA2021.2.3
二、实验描述
1.实验描述
根据教材关于Java GUI的介绍,编写一个具有如下布局的应用程序,要求挡在上面的文本区输入若干数据后(输入的多个数据之间以逗号分隔),并点击按钮,就可以在下面的文本框中分别显示除输入数据的和与平均数。
2.基本要求
在完成上述要求的情况下,考虑改进该应用程序,从界面上去掉按钮,并且随着输入数据的变化,其和与平均数能够立可显示在下面的相应文本框中。
要求考虑异常处理,使得程序能够正常运行。
三、具体实现
1.有按钮
frame = new JFrame("和与平均数");
frame.getContentPane().setFont(font);
frame.setBounds(400,200,400,400);
mainPanel = new JPanel();
initComponent();
initEvent();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//文本框设置
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(300, 100));
textArea.setToolTipText("将输入数据用逗号分开!");
textArea.setFont(font);
JPanel textAreaPanel = new JPanel();
textAreaPanel.setPreferredSize(new Dimension(350, 150));
textAreaPanel.add(textArea);
mainPanel.add(textAreaPanel, BorderLayout.NORTH);
//和值显示框
sumField = new JTextField();
sumField.setFont(font);
sumField.setEditable(false);
sumField.setPreferredSize(new Dimension(300, 30));
JLabel sumLabel = new JLabel("和值");
JPanel sumFieldPanel = new JPanel();
sumFieldPanel.add(sumLabel);
sumFieldPanel.add(sumField);
//平均值显示框
averageField = new JTextField();
averageField.setFont(font);
averageField.setEditable(false);
averageField.setPreferredSize(new Dimension(300, 30));
JLabel averageLabel = new JLabel("平均值");
JPanel averageFieldPanel = new JPanel();
averageFieldPanel.add(averageLabel);
averageFieldPanel.add(averageField);
//计算按钮
calculateButton = new JButton("计算");
calculateButton.setPreferredSize(new Dimension(60, 40));
JPanel calculatePanel = new JPanel();
calculatePanel.add(calculateButton);
JPanel centerPanel = new JPanel();
centerPanel.add(sumFieldPanel);
centerPanel.add(averageFieldPanel);
centerPanel.add(calculatePanel);
frame.add(mainPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
public void inEvent()
calculateButton.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
if (e.getButton() == MouseEvent.BUTTON1)
String numberText = textArea.getText();
if (numberText == null || numberText.trim().length() == 0)
JOptionPane.showMessageDialog(null, "输入为空!");
sumField.setText(" ");
averageField.setText(" ");
else
String[] numString = numberText.split(";");
double[] numDouble = stringToTypeDouble(numString);
double sumNumber = sumCalculate(numDouble);
double averNumber = averCalculate(numDouble);
sumField.setText(String.valueOf(sumNumber));
averageField.setText(String.valueOf(averNumber));
);
private double[] stringToTypeDouble(String[] string )
if(string == null || string.length==0)
return null;
else
double[]arrDouble = new double[string.length];
try
for(int i = 0; i<string.length;i++)
String str = string[i].trim();
arrDouble[i] = Double.parseDouble(str);
catch (NumberFormatException e)
e.printStackTrace();
arrDouble = null;
JOptionPane.showMessageDialog(null,"输入的数据类型错误!");
catch (InputMismatchException e)
e.printStackTrace();
arrDouble = null;
JOptionPane.showMessageDialog(null,"输入的数据格式错误!");
return arrDouble;
/**
* 计算和值
*/
private double sumCalculate(double[] arrDouble)
double sumNum = 0;
if(arrDouble == null || arrDouble.length == 0)
return sumNum;
else
for (double v : arrDouble)
sumNum = sumNum + v;
return Double.parseDouble(new DecimalFormat("#.000").format(sumNum));
/**
* 计算平均值
*/
private double averCalculate(double[]arrDouble)
double averNum = 0;
if(arrDouble == null || arrDouble.length == 0)
return averNum;
else
averNum = sumCalculate(arrDouble)/arrDouble.length;
return Double.parseDouble(new DecimalFormat("#.000").format(averNum));
2.无按钮
无按钮的界面设计只需将“计算”按钮删除即可。
/**
* 键盘输入处理
*/
private void initEvent()
textArea.getDocument().addDocumentListener(new DocumentListener()
@Override
public void insertUpdate(DocumentEvent e)
updateTextArea();
@Override
public void removeUpdate(DocumentEvent e)
updateTextArea();
@Override
public void changedUpdate(DocumentEvent e)
updateTextArea();
);
private void updateTextArea()
String numberText = textArea.getText();
if( numberText.trim().length() == 0)
JOptionPane.showMessageDialog(null,"输入为空!!!");
sumField.setText(" ");
averageField.setText(" ");
else
String[] numString = numberText.split(";");
double[] numDouble = stringToTypeDouble(numString);
double sumNumber = sumCalculate(numDouble);
double averNumber = averCalculate(numDouble);
sumField.setText(String.valueOf(sumNumber));
averageField.setText(String.valueOf(averNumber));
private double[] stringToTypeDouble(String[] string)
if(string == null || string.length==0)
return null;
else
double[] arrDouble = new double[string.length];
try
for (int i = 0; i < string.length; i++)
String str = string[i].trim();
arrDouble[i] = Double.parseDouble(str);
catch (NumberFormatException e)
e.printStackTrace();
arrDouble = null;
JOptionPane.showMessageDialog(null, "输入的数据类型错误!");
catch (InputMismatchException e)
e.printStackTrace();
arrDouble = null;
JOptionPane.showMessageDialog(null, "输入的数据格式错误!");
return arrDouble;
private double sumCalculate(double[] arrDouble)
double sumNum = 0;
if(arrDouble == null || arrDouble.length == 0)
return sumNum;
else
for (double v : arrDouble)
sumNum = sumNum + v;
return Double.parseDouble(new DecimalFormat("#.000").format(sumNum));
private double averCalculate(double[] arrDouble)
double averNum = 0;
if(arrDouble == null || arrDouble.length == 0)
return averNum;
else
averNum = sumCalculate(arrDouble)/arrDouble.length;
return Double.parseDouble(new DecimalFormat("#.000").format(averNum));
以上是关于武汉理工大学Java程序语言设计课内实验二的主要内容,如果未能解决你的问题,请参考以下文章