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

📄 equationinput.java

📁 用Java开发的实用数学建模程序 简单易懂 初学者可以用来学习java知识
💻 JAVA
字号:
/*
 *@(#)EquationInput.java 2.0 2005/04/28
 *
 *清华大学 精密仪器与机械学系
 *范灿升 fancansheng@163.com
 */

package input;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
//导入系统类

import lib.Library;
import algorithm.LinearEquation;
//导入自定义的类

/**
 *该类所提供的方法是供给类{@link Modeling}调用的,用以产生求解线性方程组的输入界面。
 *@version 2.0, 2005/04/28
 *@author 范灿升
 *@see Modeling
 *@see algorithm.LinearEquation
 */

public class EquationInput implements ActionListener
{
	private int n=0;
	private int i,j;
	private double[][] dCoef;
	private double[] dConst;
	private JTextField[][] coefTextField;
	private JTextField[] constTextField;
	private JButton solveButton;
	private JButton helpButton;
	
	/**
	 *输入面板的父组件。
	 */
	public Component parentComponent;
	
	/**
	 *数据在该面板上输入,包括系数矩阵和常数项。
	 */
	public JPanel inputPanel;
	
	/**
	 *用来放置计算按钮和帮助按钮等。
	 */
	public JPanel computePanel;
	
	/**
	 *用来放置inputPanel的容器。
	 */
	public JPanel mainPanel;

	/**
	 *产生包含方程组相关参数的类。
	 *@param parentComponent	输入面板的上一层组件
	 *@param inputPanel			数据在该面板上输入
	 *@param computePanel		面板上的按钮为计算用按钮
	 *@param mainPanel			mainPanel是用来放置inputPanel的容器
	 */
	public EquationInput(Component parentComponent,JPanel inputPanel,JPanel computePanel,JPanel mainPanel)
	{
		this.parentComponent=parentComponent;
		this.inputPanel=inputPanel;
		this.computePanel=computePanel;
		this.mainPanel=mainPanel;
	}
	
	/**
	 *显示求解线性代数方程组的界面。
	 *<p>用户从该界面输入方程组的系数矩阵和常数项向量。
	 */
	public void showPanel()
	{
		boolean pass=false;//用于标记是否通过合法性检验
		boolean secondTime=false;
		String tmp;
		
		while(!pass)
		{
			if(secondTime==true)
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入一个正整数!\n请重新输入:","输入阶数",JOptionPane.WARNING_MESSAGE);
			else
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入线性方程组中未知数的数目:","输入阶数",JOptionPane.QUESTION_MESSAGE);
			try
			{
				if(tmp==null)
					return;
				n=-1;
				n=Integer.parseInt(tmp);
			}
			catch(NumberFormatException e)
			{
				secondTime=true;
			}
			if(n>=1)
				pass=true;
			else
				secondTime=true;
		}
		dCoef=new double[n][n];
		dConst=new double[n];
		//输入方程的阶数
		
		JPanel coefficients=new JPanel(true);
		GridLayout coefGridLayout=new GridLayout(n,n);
		coefficients.setLayout(coefGridLayout);
		//设置系数矩阵布局
		
		JPanel constants=new JPanel(true);
		GridLayout constGridLayout=new GridLayout(n,1);
		constants.setLayout(constGridLayout);
		//设置常数项向量
		
		coefTextField=new JTextField[n][n];
		constTextField=new JTextField[n];
		coefficients.removeAll();
		constants.removeAll();
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				coefTextField[i][j]=new JTextField(Library.TEXTFIELD_LENGTH);
				coefTextField[i][j].setToolTipText("第"+(i+1)+"行,第"+(j+1)+"列");
				coefficients.add(coefTextField[i][j]);
			}
			constTextField[i]=new JTextField(Library.TEXTFIELD_LENGTH);
			constTextField[i].setToolTipText("第"+(i+1)+"行");
			constants.add(constTextField[i]);
		}
		//向coefficients和constants中添加输入域
		
		JLabel xLabel=new JLabel(" · X = ",JLabel.CENTER);
		xLabel.setFont(new Font("黑体",Font.PLAIN,25));
		
		BoxLayout inputPanelLayout=new BoxLayout(inputPanel,BoxLayout.X_AXIS);
		inputPanel.setLayout(inputPanelLayout);
		inputPanel.removeAll();
		inputPanel.add(coefficients);
		inputPanel.add(xLabel);
		inputPanel.add(constants);
		JScrollPane scrollPane=new JScrollPane(inputPanel);
		
		TitledBorder border=new TitledBorder("输入方程组的系数和常数项");
		border.setTitleFont(Library.font);
		inputPanel.setBorder(border);
		
		BoxLayout computePanelLayout=new BoxLayout(computePanel,BoxLayout.X_AXIS);
		computePanel.setLayout(computePanelLayout);
		
		solveButton=new JButton("解方程",new ImageIcon(Library.polyhedronIcont_Scaled));
		solveButton.setFont(Library.font);
		computePanel.removeAll();
		computePanel.add(solveButton);
		helpButton=new JButton("帮助",new ImageIcon(Library.helpIcon_Scaled));
		helpButton.setFont(Library.font);
		computePanel.add(helpButton);
		
		mainPanel.removeAll();
		mainPanel.add(scrollPane,BorderLayout.CENTER);
		mainPanel.add(computePanel,BorderLayout.SOUTH);
		//布局
		
		solveButton.addActionListener(this);
		helpButton.addActionListener(this);
		
		parentComponent.setSize(parentComponent.getPreferredSize());
		parentComponent.setVisible(true);
	}
	
	/**
	 *由ActionListener所指定的方法,响应用户单击按钮时的动作
	 *@param e		单击按钮所产生的事件
	 */
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==solveButton)
		{
			for(i=0;i<n;i++)
			{
				for(j=0;j<n;j++)
				{
					try
					{
						dCoef[i][j]=Double.parseDouble(coefTextField[i][j].getText());
					}
					catch(NumberFormatException nException)
					{
						JOptionPane.showMessageDialog(parentComponent,
								"系数矩阵中第"+(i+1)+"行,第"+(j+1)+"列的输入不是有效的数字。\n请重新输入","输入错误",JOptionPane.WARNING_MESSAGE);
						return;
					}
				}
				//把文本框中的值传递给系数矩阵
				
				try
				{
					dConst[i]=Double.parseDouble(constTextField[i].getText());
				}
				catch(NumberFormatException nException)
				{
					JOptionPane.showMessageDialog(parentComponent,
							"常数项向量中第"+(i+1)+"行的输入不是有效的数字。\n请重新输入","输入错误",JOptionPane.WARNING_MESSAGE);
					return;
				}
				//把文本框中的值传递给常数项向量
			}
			//完成值的传递
			
			LinearEquation equation=new LinearEquation(dCoef,dConst);
			double[] solution=equation.solve();
			JFrame solutionFrame=new JFrame("求解结果");
			solutionFrame.setResizable(false);
			Container solutionContainer=solutionFrame.getContentPane();
			StringBuffer str=new StringBuffer();
			if(solution==null)
				str.append("方程组的系数矩阵行列式为0或值太小以至进出计算范围,\n方程组可能有无穷多组解或无解");
			else
			{
				for(i=0;i<solution.length;i++)
					str.append(" X"+(i+1)+" = "+(float)solution[i]+"\n");
			}
			
			JTextArea solutionArea=new JTextArea(str.toString());
			JScrollPane solutionScroll=new JScrollPane(solutionArea);
			solutionContainer.add(solutionScroll);
			solutionFrame.pack();
			solutionFrame.setVisible(true);
			solutionFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			//显示求解结果
		}
		//solveButton的动作
		
		if(e.getSource()==helpButton)
		{
			ImageIcon helpMessage=new ImageIcon("resource\\EquationHelp.GIF");
			JOptionPane.showMessageDialog(parentComponent,helpMessage,"输入帮助",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.helpIcon_Scaled));
		}
	}
}

⌨️ 快捷键说明

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