能否帮助我找出如何在mergefiles方法中将合并数组显示为BinaryFile中的一个,并将其显示在箭头指向的project9类中?谢谢。
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.JOptionPane;
public class Project09 extends Frame implements ActionListener
{
int ww;
int wh;
String command = "";
TextFile textFile;
BinaryFile binaryFile;
double product;
int[] searchKeys;
public static void main(String[] args)
{
Frame frame = new Project09();
frame.setResizable(true);
frame.setSize(1000,800);
frame.setVisible(true);
}
public Project09()
{
setTitle("Data Structures");
// Create Menu
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu fileMenu = new Menu("File");
mb.add(fileMenu);
MenuItem miAbout = new MenuItem("About");
miAbout.addActionListener(this);
fileMenu.add(miAbout);
MenuItem miExit = new MenuItem("Exit");
miExit.addActionListener(this);
fileMenu.add(miExit);
Menu actionMenu = new Menu("One Dimensional Structures");
mb.add(actionMenu);
// Text File menus
Menu textFileMenu = new Menu("Text File");
actionMenu.add(textFileMenu);
MenuItem miMergeTextFile = new MenuItem("Merge Text File");
miMergeTextFile.addActionListener(this);
textFileMenu.add(miMergeTextFile);
Menu BinaryFileMenu = new Menu("BinaryFile");
actionMenu.add(BinaryFileMenu);
MenuItem miMergeBinaryFile = new MenuItem("Merge Binary File");
miMergeBinaryFile.addActionListener(this);
BinaryFileMenu.add( miMergeBinaryFile);
// End program when window is closed
WindowListener l = new WindowAdapter()
{
public void windowClosing(WindowEvent ev)
{
System.exit(0);
}
public void windowActivated(WindowEvent ev)
{
repaint();
}
public void windowStateChanged(WindowEvent ev)
{
repaint();
}
};
ComponentListener k = new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
repaint();
}
};
// register listeners
this.addWindowListener(l);
this.addComponentListener(k);
}
//******************************************************************************
// called by windows manager whenever the application window performs an action
// (select a menu item, close, resize, ....
//******************************************************************************
public void actionPerformed (ActionEvent ev)
{
// figure out which command was issued
command = ev.getActionCommand();
// take action accordingly
if("About".equals(command))
{
repaint();
}
else
if("Exit".equals(command))
{
System.exit(0);
}
else
if("Merge Binary File".equals(command))
{
binaryFile = new BinaryFile();
BinaryFile b = new BinaryFile();
try {
b.mergeFiles();
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
else
if("Merge Text File".equals(command))
{
textFile = new TextFile();
repaint();
}
}
//********************************************************
// called by repaint() to redraw the screen
//********************************************************
public void paint(Graphics g)
{
// Check Command issued, take action accordingly
ww = this.getWidth();
Font f1 = new Font("SansSerif",Font.BOLD,16);
Font f = new Font("SansSerif",Font.BOLD,12);
g.setFont(f);;
--> if("Merge Binary File".equals(command))
{
int x=100;
int y = 140;
int[] temp =null;
try {
temp = binaryFile.mergeFiles();
} catch (IOException e) {
e.printStackTrace();
}
dp(g,temp,x,y);
}
else
if("Merge Text File".equals(command))
{
int x=100;
int y = 140;
int[] temp =null;
try {
temp = textFile.mergeFiles();
} catch (IOException e) {
e.printStackTrace();
}
dp(g,temp,x,y);
}
}
public int dp(Graphics g, int[]a, int xs, int ys)
{
ww = this.getWidth();
Font f1 = new Font("SansSerif",Font.BOLD,16);
Font f = new Font("SansSerif",Font.BOLD,12);
g.setColor(Color.RED);
g.setFont(f1);
g.drawString("Original File", ww/2-40, 110);
g.setColor(Color.black);
g.setFont(f);
int x=xs;
int y =ys;
for (int i=0; i<a.length; i++)
{
g.drawString(Integer.toString(a[i]), x, y);
x = x + 35;
if (x>(ww-100))
{
x = 100;
y = y + 15;
}
}
return y;
}
}
import javax.swing.*;
import java.io.IOException;
import java.util.Scanner;
import java.io.DataOutputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class BinaryFile extends OneDStructure
{
JFileChooser chooser;
public Object s;
public BinaryFile()
{
dataFilePath = null;
dataFileName = null;
}
public BinaryFile(String fileName, int fileSize)
{
dataFileName = fileName;
size = fileSize;
array = new int[size];
}
public void loadFile()
{
dataFilePath = null;
dataFileName = null;
chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG );
chooser.setDialogTitle("Open Data File");
int returnVal = chooser.showOpenDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION)
{
dataFilePath = chooser.getSelectedFile().getPath();
dataFileName = chooser.getSelectedFile().getName();
}
try
{
readFileIntoArray();
}
catch (IOException ioe)
{
System.exit(0);
}
}
public void readFileIntoArray() throws IOException
{
if (dataFilePath != null)
{
DataInputStream binaryfile = new DataInputStream(new BufferedInputStream(
new FileInputStream(dataFilePath)));
size=binaryfile.readInt();
array =new int [size];
for (int i=0; i<size; i++)
{
array[i] = binaryfile.readInt();
}
low = this.getMinimum();
high = this.getMaximum();
// end of file detected
binaryfile.close();
}
else
{
size = 0;
}
}
public void storeFile()
{
try
{
createOutputFile();
}
catch (IOException e)
{
System.exit(2);
}
}
public void createOutputFile() throws IOException
{
String outFileName = this.dataFilePath+".data";
DataOutputStream out = new DataOutputStream (new BufferedOutputStream(
new FileOutputStream(outFileName)));
out.close();
}
public int[] mergeFiles() throws IOException
{
BinaryFile t = new BinaryFile();
t.loadFile();
BinaryFile s =new BinaryFile();
s.loadFile();
int[] a= new int [t.getArray().length +s.getArray().length];
for (int i =0;i<a.length;i++)
{
if (i<t.getArray().length )
{
a[i]=t.getArray()[i];
}
else{
a[i]=s.getArray()[i-t.getArray().length ];
}
}
FileOutputStream f =new FileOutputStream(new File("Merge.txt" ) );
for (int q=0;q< a.length;q++)
{
f.write(a[q]);
}
return a;
}
}
能否帮助我找出如何在mergefiles方法中将合并数组显示为BinaryFile中的一个,并将其显示在箭头指向的project9类中?谢谢。