📄 clientreadfile.java
字号:
import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ClientReadFile extends JFrame { private ObjectInputStream input; private ServerOperatePanel userInterface; private JButton nextRecord, open; public final String attributeNames[] = { "Attribute-Name", "Type","Length", "ConstrainTag","Constrain"}; // Constructor -- initialize the Frame public ClientReadFile() { super( "Display the defined attributes" ); getContentPane().setLayout( new BorderLayout() ); userInterface = new ServerOperatePanel( 5, attributeNames ); nextRecord = userInterface.getDoTask1(); nextRecord.setText( "Show Attribute" ); nextRecord.setEnabled( false ); nextRecord.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { readRecord(); nextRecord.setText( "Next Attribute" ); } } ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { if( input != null) closeFile(); } } ); open = userInterface.getDoTask2(); open.setText( "Open Format-File" ); open.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { openFile(); } } ); getContentPane().add( userInterface, BorderLayout.CENTER ); pack(); setSize( 500, 250 ); show(); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // user clicked Cancel button on dialog if ( result == JFileChooser.CANCEL_OPTION ) return; File fileName = fileChooser.getSelectedFile(); if ( fileName == null || fileName.getName().equals( "" ) ) JOptionPane.showMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); else { // Open the file try { input = new ObjectInputStream( new FileInputStream( fileName ) ); open.setEnabled( false ); nextRecord.setEnabled( true ); } catch ( IOException e ) { JOptionPane.showMessageDialog( this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE ); } } } public void readRecord() { ItemAttribute record; // input the values from the file try { record = ( ItemAttribute ) input.readObject(); String values[] = { record.getName(), ""+TypeIndex( record.getType() ), ""+record.getLength(), ""+ConstrainTagIndex( record.getConstrainTag() ), ""+ConstrainIndex( record.getConstrain() )}; userInterface.setFieldValues( values ); } catch ( EOFException eofex ) { nextRecord.setEnabled( false ); JOptionPane.showMessageDialog( this, "No more Attributes in file", "End of File", JOptionPane.ERROR_MESSAGE ); } catch ( ClassNotFoundException cnfex ) { JOptionPane.showMessageDialog( this, "Unable to create object", "Class Not Found", JOptionPane.ERROR_MESSAGE ); } catch ( IOException ioex ) { JOptionPane.showMessageDialog( this, "Error during read from file", "Read Error", JOptionPane.ERROR_MESSAGE ); } } private void closeFile() { try { input.close(); //System.exit( 0 ); } catch ( IOException e ) { JOptionPane.showMessageDialog( this, "Error closing file", "Error", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } //change the ItemAttribute data to the valid String format for displaying //ItemAttribute(String,int,int,boolean,int) private String TypeIndex( int type) { if( type == 1 ) return "Integer"; else if( type == 2 ) return "Float"; else if( type == 3 ) return "String"; else return null; } private String ConstrainTagIndex( boolean tag ) { if( tag == true ) return "true"; else if( tag == false ) return "false"; else return "true"; } private String ConstrainIndex( int c ) { if( c == 1 ) return "Key"; else return "NotKey"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -