📄 createfile.java
字号:
//create the data-input-format file
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreateFile extends JFrame{
private ObjectOutputStream output;
private OperatePanel userInterface;
private JButton enter, open;
private int attributeNum;//note the record attribute amount
private String attributeNames[];//note total attribute names of the record
private ItemAttribute attributeArray[];//note total attributes of the record
public final int KEY = 1;//judge wether the attribute is a key
public CreateFile()
{
super( "Creating a Sequential File of Records" );
getContentPane().setLayout( new BorderLayout() );
JOptionPane.showMessageDialog( null, "First choose the Format-File");
OpenFormatFile fget = new OpenFormatFile();
attributeNum = fget.getAttributeNum();
attributeArray = new ItemAttribute[ attributeNum ];
attributeArray = fget.getAttribute();
attributeNames = new String[ attributeNum ];
for( int i = 0; i < attributeNum; i++ )
attributeNames[ i ] = attributeArray[ i ].getName();
userInterface = new OperatePanel( attributeNum, attributeNames );
enter = new JButton();
open = new JButton();
open = userInterface.getDoTask2();
open.setText( "Data-File Save As" );
open.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
openFile( );
}
}
);
getContentPane().add( userInterface, BorderLayout.CENTER );
//getContentPane().add( choose, BorderLayout.NORTH );
enter = userInterface.getDoTask1();
enter.setText( "Enter" );
enter.setEnabled( false ); // disable button to start
enter.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
addRecord();
}
}
);
addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
//if( input != null)
closeFile();
}
}
);
setSize( 400, 200 );
show();
}
private void openFile( )
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showSaveDialog( 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 {
output = new ObjectOutputStream(
new FileOutputStream( fileName ) );
open.setEnabled( false );
enter.setEnabled( true );
}
catch ( IOException e ) {
JOptionPane.showMessageDialog( this,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
}
private void closeFile()
{
try {
output.close();
//System.exit( 0 );
}
catch( IOException ex ) {
JOptionPane.showMessageDialog( this,
"Error closing file",
"Error", JOptionPane.ERROR_MESSAGE );
//System.exit( 1 );
}
}
public void addRecord()
{
GoodsRecord record;
String fieldValues[] = userInterface.getFieldValues();
// If the account field value is not empty
if ( ! fieldValues[ 0 ].equals( "" ) ) {
// output the values to the file
try {
boolean flag = true;
GoodsField fieldArray[] = new GoodsField[ attributeNum ];
for( int i = 0; i < attributeNum; i++ )
{
fieldArray[ i ] = new GoodsField( attributeArray[ i ], fieldValues[ i ] );
if( fieldArray[ i ].getValue() == null && fieldArray[ i ].getConstrain() == KEY )
flag = false;
}
if( flag ){
record = new GoodsRecord ( fieldArray );
output.writeObject( record );
output.flush();
}
else
JOptionPane.showMessageDialog(null,
"The value of KEY attribute wrong!"
+"\nInput the record fail!" );
userInterface.clearFields();
}
catch ( NumberFormatException nfe ) {
JOptionPane.showMessageDialog( this,
"Invalid enter items",
"Invalid enter items",
JOptionPane.ERROR_MESSAGE );
}
catch ( IOException io ) {
closeFile();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -