?? financialmainframe.java
字號:
JFileChooser filech = new JFileChooser();
int rVal = filech.showSaveDialog(FinancialMainFrame.this);
if(rVal==JFileChooser.APPROVE_OPTION){
String filePathName = filech.getSelectedFile().getAbsolutePath();
writeToFile(filePathName);
}
}
});
}
return jMenuItem5;
}
/**
* This method initializes jInputDialog
*
* @return javax.swing.JDialog
*/
private JDialog getJInputDialog() {
if (jInputDialog == null) {
jInputDialog = new JDialog(this);
jInputDialog.setTitle("單個輸入");
jInputDialog.setSize(new Dimension(213, 171));
jInputDialog.setContentPane(getJContentPane3());
}
return jInputDialog;
}
/**
* This method initializes jContentPane3
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane3() {
if (jContentPane3 == null) {
jLabel9 = new JLabel();
jLabel9.setText("姓名:");
jLabel8 = new JLabel();
jLabel8.setText("金額:");
jLabel7 = new JLabel();
jLabel7.setText("地點:");
jLabel6 = new JLabel();
jLabel6.setText("日期:");
GridLayout gridLayout1 = new GridLayout();
gridLayout1.setRows(5);
gridLayout1.setHgap(0);
gridLayout1.setVgap(0);
gridLayout1.setColumns(2);
jContentPane3 = new JPanel();
jContentPane3.setLayout(gridLayout1);
jContentPane3.add(jLabel6, null);
jContentPane3.add(getJTextField6(), null);
jContentPane3.add(jLabel7, null);
jContentPane3.add(getJTextField7(), null);
jContentPane3.add(jLabel8, null);
jContentPane3.add(getJTextField8(), null);
jContentPane3.add(jLabel9, null);
jContentPane3.add(getJComboBox(), null);
jContentPane3.add(getJButton3(), null);
}
return jContentPane3;
}
/**
* This method initializes jTextField6
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField6() {
if (jTextField6 == null) {
jTextField6 = new JTextField();
}
return jTextField6;
}
/**
* This method initializes jTextField7
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField7() {
if (jTextField7 == null) {
jTextField7 = new JTextField();
}
return jTextField7;
}
/**
* This method initializes jTextField8
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField8() {
if (jTextField8 == null) {
jTextField8 = new JTextField();
}
return jTextField8;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
for(String s:fProc.listNames)
jComboBox.addItem(s);
}
return jComboBox;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setText("確定");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String date = jTextField6.getText().trim();
String place = jTextField7.getText().trim();
double money = Double.valueOf(jTextField8.getText().trim());
String name = (String)(jComboBox.getSelectedItem());
for(BasicItem item:fProc.itemList){
if(date.equals(item.getDate()) && name.equals(item.getName())){
JOptionPane.showMessageDialog(null,"已有相同日期和姓名的記錄存在!",
"輸入錯誤",JOptionPane.WARNING_MESSAGE);
return;
}
}
((BasicTableModel)(jTable.getModel())).removeAllRow();
BasicItem item = new BasicItem(date,name,money,place);
fProc.itemList.add(item);
((BasicTableModel)(jTable.getModel())).appendRow(item);
jTable.updateUI();
jTextArea1.setText(name + " 日期:" + date + " 消費金額:" + money);
jInputDialog.setVisible(false);
}
});
}
return jButton3;
}
/**
* This method initializes jMenuItem6
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem6() {
if (jMenuItem6 == null) {
jMenuItem6 = new JMenuItem();
jMenuItem6.setText("單個輸入");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJInputDialog().setVisible(true);
}
});
}
return jMenuItem6;
}
/**
* This method initializes jScrollPane3
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane3() {
if (jScrollPane3 == null) {
jScrollPane3 = new JScrollPane();
jScrollPane3.setBounds(new Rectangle(12, 93, 271, 149));
jScrollPane3.setViewportView(getJPanel1());
}
return jScrollPane3;
}
/**
* This method initializes jMenuItem7
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem7() {
if (jMenuItem7 == null) {
jMenuItem7 = new JMenuItem();
jMenuItem7.setText("導出文本");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JFileChooser filech = new JFileChooser();
int rVal = filech.showSaveDialog(FinancialMainFrame.this);
if(rVal==JFileChooser.APPROVE_OPTION){
String filePathName = filech.getSelectedFile().getAbsolutePath();
writeToText(filePathName);
}
}
});
}
return jMenuItem7;
}
/**
* This method initializes jMenuItem8
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem8() {
if (jMenuItem8 == null) {
jMenuItem8 = new JMenuItem();
jMenuItem8.setText("導入文本");
jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JFileChooser filech = new JFileChooser();
int rVal = filech.showOpenDialog(FinancialMainFrame.this);
if(rVal==JFileChooser.APPROVE_OPTION){
String filePathName = filech.getSelectedFile().getAbsolutePath();
readFromText(filePathName);
}
}
});
}
return jMenuItem8;
}
/**
* This method initializes jMenuItem9
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem9() {
if (jMenuItem9 == null) {
jMenuItem9 = new JMenuItem();
jMenuItem9.setText("添加成員");
jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String s = (String)JOptionPane.showInputDialog(null,"歡迎加入!請輸入成員姓名:",
"添加成員",JOptionPane.PLAIN_MESSAGE);
s = s.trim();
if ((s != null) && (s.length() > 0)) {
fProc.listNames.add(s);
updateView();
return;
}
}
});
}
return jMenuItem9;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FinancialMainFrame thisClass = new FinancialMainFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setExtendedState(JFrame.MAXIMIZED_BOTH);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public FinancialMainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(680, 290);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("Finacial Manage-Lihong");
jTextArea.setEditable(false);
jTextArea1.setEditable(false);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
jContentPane.add(getJPanel(), null);
jContentPane
.addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsAdapter() {
public void ancestorResized(java.awt.event.HierarchyEvent e) {
jSplitPane.setDividerLocation(0.25);
jSplitPane1.setDividerLocation(0.7);
jSplitPane2.setDividerLocation(0.5);
}
});
}
return jContentPane;
}
void readFromFile(String path){
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
fProc = (FinancialProc)in.readObject();
fProc.setFileFullPath(path);
in.close();
fProc.listNames.clear();
fProc.fillListNames();
updateView();
}
catch(ClassNotFoundException e){
fProc.setFileFullPath("");
javax.swing.JOptionPane.showMessageDialog(null, "類未找到","類錯誤",JOptionPane.WARNING_MESSAGE);
}
catch(IOException e){
fProc.setFileFullPath("");
javax.swing.JOptionPane.showMessageDialog(null, "文件讀取錯誤","讀取錯誤",JOptionPane.WARNING_MESSAGE);
}
}
void writeToFile(String path){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));
out.writeObject(fProc);
out.close();
}
catch(FileNotFoundException e){
javax.swing.JOptionPane.showMessageDialog(null, "文件未找到","保存錯誤",JOptionPane.WARNING_MESSAGE);
}
catch(IOException e){
javax.swing.JOptionPane.showMessageDialog(null, "文件寫入錯誤","寫入錯誤",JOptionPane.WARNING_MESSAGE);
}
}
void readFromText(String path){
try{
BufferedReader in = new BufferedReader(new FileReader(new File(path).getAbsoluteFile()));
String s = "";
while((s=in.readLine())!=null){
String[] sArray = Pattern.compile("###").split(s);
if(sArray.length!=0){
fProc.itemList.add(new BasicItem(sArray[0],sArray[1],Double.valueOf(sArray[2]),sArray[3]));
}
}
in.close();
fProc.fillListNames();
updateView();
}
catch(IOException e){
javax.swing.JOptionPane.showMessageDialog(null, "文件未找到","保存錯誤",JOptionPane.WARNING_MESSAGE);
}
}
void writeToText(String path){
String appendString = "";
for(BasicItem item:fProc.itemList){
appendString = appendString + item.getDate() + "###" +item.getName() +
"###" + item.getMoney() + "###" +item.getPlace() + "\n";
}
try{
FileOutputStream outf = new FileOutputStream(new File(path));
outf.write(appendString.getBytes());
outf.close();
}
catch(FileNotFoundException e){
javax.swing.JOptionPane.showMessageDialog(null, "文件未找到","保存錯誤",JOptionPane.WARNING_MESSAGE);
}
catch(IOException e){
javax.swing.JOptionPane.showMessageDialog(null, "文件寫入錯誤","寫入錯誤",JOptionPane.WARNING_MESSAGE);
}
}
void updateView(){
((BasicTableModel)(jTable.getModel())).removeAllRow();
((DefaultMutableTreeNode)(jTree.getModel().getRoot())).removeAllChildren();
for(BasicItem item:fProc.itemList)
((BasicTableModel)(jTable.getModel())).appendRow(item);
for(String name:fProc.listNames){
((DefaultMutableTreeNode)(jTree.getModel().getRoot())).add(new DefaultMutableTreeNode(name));
}
jTree.updateUI();
jTable.updateUI();
this.setTitle("Finacial Manage-Lihong:" + fProc.getFileFullPath());
if(jComboBox==null)
getJComboBox();
if(jCheckBoxs==null)
jCheckBoxs = getJCheckBoxs();
if(jPanel1==null)
jPanel1 = getJPanel1();
jComboBox.removeAllItems();
for(String s:fProc.listNames)
jComboBox.addItem(s);
if(jCheckBoxs.size()!=0)
jCheckBoxs.clear();
for(String s:fProc.listNames)
jCheckBoxs.add(new JCheckBox(s));
jPanel1.removeAll();
for(JCheckBox cb:jCheckBoxs)
jPanel1.add(cb,null);
}
} // @jve:decl-index=0:visual-constraint="10,10"
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -