我试图了解JFrame的工作原理。我正在编写一个程序,它将读取文件的内容,并以二进制字符串的形式显示在文本区域中。我已经用严格的命令提示符窗口格式完成了任务。然后,我意识到我需要通过JPane窗口完成任务。所以,我有我编写的代码,允许用户选择一个文件进行输入,代码然后检查一个有效的文件,如果它在那里,那么代码一次读取一个字节,将其改为二进制,并将其添加到字符串中。读入整个文件后,该字符串将在命令提示符中显示为长二进制文件。但我需要在我设法创建的实际JFrame中完成此任务。但现在,我无法弄清楚在读入和创建后,我应该用二进制信息字符串更新textarea的位置和方式。这是代码:
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
public class BinaryEditor extends JFrame{
public static String myExtension ="";
public static boolean isThere=false;
public static String myFile="";;
public static String by;
public static String myDisplay="";
//******************************************************************************
public BinaryEditor(){
//setLayout(new BorderLayout(5,10));
setLayout(new GridLayout(3,2,5,5));
//Set up panels for main display
JPanel fileInput = new JPanel();
JPanel binaryPanel = new JPanel();
JPanel saveBinary = new JPanel();
//Add the panels to the frame
add(fileInput);
add(binaryPanel);
add(saveBinary);
//Set up panel properties
fileInput.setBackground(Color.BLUE);
fileInput.setFont(new Font("Californian FB", Font.BOLD, 12));
binaryPanel.setBackground(Color.WHITE);
binaryPanel.setFont(new Font("Californian FB", Font.BOLD, 12));
saveBinary.setBackground(Color.GRAY);
saveBinary.setFont(new Font("Californian FB", Font.BOLD, 12));
JTextField myDisplay = new JTextField(16);
//Add contents to the panels
fileInput.add(new JLabel("Enter a File"));
fileInput.add(new JTextField(8));
binaryPanel.add(new JTextArea("",10,22));
saveBinary.add(new JButton("Save file"));
binaryPanel.add(myDisplay);
}
//******************************************************************************
//Main Method
public static void main(String[] args) throws IOException {
//Create a JFrame panel, set it up and display it
BinaryEditor frame = new BinaryEditor();
frame.setSize(300,300);
frame.setTitle("Binary Editor");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Ask user for filename
Scanner input = new Scanner(System.in);
do{
System.out.print("Please input a filename to read : ");
myFile = input.next();
//Validate filename and existance
checkmyFile(myFile);
if(!isThere)
System.out.println("No such filename.");
}while(!isThere);
//File exists, now open file for reading
readFile();
System.out.println(myDisplay);
repaint();
}
//******************************************************************************
//Method to check filename
public static boolean checkmyFile(String theFile){
//Check for file existance
File file=new File(theFile);
boolean exists = file.exists();
if (!exists) {
isThere=false;
}else{
isThere=true;
}
return isThere;
}
//******************************************************************************
//Method to read file into array
public static void readFile()throws IOException{
FileInputStream myInput = new FileInputStream(myFile);
int numberBytes = myInput.available();
byte bytearray[] = new byte[numberBytes];
//Read entire file into bytearray
myInput.read(bytearray);
for(int i = 0; i < numberBytes; i++){
//System.out.println (bytearray[i]);
by = Integer.toBinaryString(bytearray[i]);
//System.out.print(by+"*");
myDisplay +=by;
}
myInput.close();
//myDisplay.
}
}//End of MAIN Method
答案 0 :(得分:0)
首先,您的JTextField需要声明为类对象,然后您需要从BynaryEditor
构造函数中删除它的声明和实例化。
如果在构造函数中声明JTextField,它将作为方法变量而不是类变量显示,并且在其余代码中,您将无法在其上调用方法。
其次,一旦从文件中读取数据,就需要调用setText()的JTextField方法,以显示文件中的数据。
你可以轻松地做到:
public class BinaryEditor extends JFrame{
/* class variable declaration and instantiation */
private JTextField myDisplay = new JTextField(16);
/* other code.. */
并且,在您的readFile()
方法中,在数据读取后,您可以添加以下行:
myDisplay.setText(by);
这样,您的by
字符串将显示在您的JTextField中。