?? equationinput.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 + -