Swing 设置初始值后,如何将新字符串发布到JPanel文本字段中?

Swing 设置初始值后,如何将新字符串发布到JPanel文本字段中?,swing,textarea,jframe,Swing,Textarea,Jframe,我试图理解JFrame是如何工作的。我正在编写一个程序,它将读取文件的内容,并以二进制字符串的形式显示到文本区域。我已经用严格的命令提示符窗口格式完成了任务。然后,我意识到我需要通过JPane窗口完成任务。因此,我编写了一些代码,允许用户选择一个文件进行输入,然后代码检查一个有效的文件,如果存在,那么代码一次读取一个字节,将其更改为二进制,并将其添加到字符串中。读入整个文件后,字符串在命令提示符中显示为长二进制。但我需要在我创建的实际JFrame中完成这项任务。但是现在,我不知道在读取并创建te

我试图理解JFrame是如何工作的。我正在编写一个程序,它将读取文件的内容,并以二进制字符串的形式显示到文本区域。我已经用严格的命令提示符窗口格式完成了任务。然后,我意识到我需要通过JPane窗口完成任务。因此,我编写了一些代码,允许用户选择一个文件进行输入,然后代码检查一个有效的文件,如果存在,那么代码一次读取一个字节,将其更改为二进制,并将其添加到字符串中。读入整个文件后,字符串在命令提示符中显示为长二进制。但我需要在我创建的实际JFrame中完成这项任务。但是现在,我不知道在读取并创建textarea之后,我应该在哪里以及如何使用二进制信息字符串更新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
import java.awt.*;
导入java.util.*;
导入java.io.*;
导入javax.swing.*;
导入java.lang.*;
公共类二进制编辑器扩展了JFrame{
公共静态字符串myExtension=“”;
公共静态布尔值isThere=false;
公共静态字符串myFile=“”;;
公共静态字符串;
公共静态字符串myDisplay=“”;
//******************************************************************************
公共二进制编辑器(){
//setLayout(新边界布局(5,10));
setLayout(新网格布局(3,2,5,5));
//设置主显示器的面板
JPanel fileInput=新的JPanel();
JPanel binaryPanel=newjpanel();
JPanel saveBinary=新的JPanel();
//将面板添加到框架中
添加(文件输入);
添加(二进制面板);
添加(saveBinary);
//设置面板属性
fileInput.setBackground(颜色:蓝色);
setFont(新字体(“Californian FB”,Font.BOLD,12));
双拼板.立根(颜色.白色);
setFont(新字体(“Californian FB”,Font.BOLD,12));
saveBinary.setBackground(颜色:灰色);
saveBinary.setFont(新字体(“Californian FB”,Font.BOLD,12));
JTextField myDisplay=新的JTextField(16);
//将内容添加到面板
添加(新JLabel(“输入文件”);
添加(新的JTextField(8));
添加(新的JTextArea(“,10,22));
添加(新的JButton(“保存文件”);
添加(myDisplay);
}
//******************************************************************************    
//主要方法
公共静态void main(字符串[]args)引发IOException{
//创建JFrame面板,设置并显示它
BinaryEditor框架=新的BinaryEditor();
框架。设置尺寸(300300);
frame.setTitle(“二进制编辑器”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//要求用户输入文件名
扫描仪输入=新扫描仪(System.in);
做{
System.out.print(“请输入一个文件名以读取:”);
myFile=input.next();
//验证文件名和存在性
checkmyFile(myFile);
如果(!在这里)
System.out.println(“没有这样的文件名”);
}当(!在这里);
//文件已存在,现在打开文件进行读取
readFile();
系统输出打印项次(myDisplay);
重新油漆();
}
//******************************************************************************    
//方法检查文件名
公共静态布尔checkmyFile(字符串文件){
//检查文件是否存在
文件=新文件(文件);
boolean exists=file.exists();
如果(!存在){
isThere=false;
}否则{
isThere=true;
}
回归在这里;
}
//******************************************************************************    
//方法将文件读入数组
公共静态void readFile()引发IOException{
FileInputStream myInput=新的FileInputStream(myFile);
int numberBytes=myInput.available();
byte bytearray[]=新字节[numberBytes];
//将整个文件读入bytearray
myInput.read(bytearray);
对于(int i=0;i
首先,您的JTextField需要声明为类对象,然后您需要从
BynaryEditor
构造函数中删除其声明和实例化

如果在构造函数中声明JTextField,它将作为方法变量而不是类变量可见,并且在代码的其余部分中,您将无法对其调用方法

第二,从文件中读取数据后,需要调用的方法来显示文件中的数据

您可以通过以下方式轻松做到这一点:

public class BinaryEditor extends JFrame{
        /* class variable declaration and instantiation */

        private JTextField myDisplay = new JTextField(16);

        /* other code.. */
并且,在
readFile()
方法中,读取数据后,可以添加以下行:

myDisplay.setText(by);
这样,您的
by
字符串将显示在jtext字段中