簡體   English   中英

使用JFileChooser設置值JTextField

[英]Using JFileChooser to set values JTextField

我正在學習Java,並且嘗試加載.txt文件中的一些數字。 這些數字需要顯示在GUI的“文本字段”中。 我在Google上做了很多研究,但無法正常工作。 有什么提示嗎?

//TEXT FIELDS
    this.Vx = new JTextField();
    this.Vx.setBounds(120, 20, 100, 30);
    this.add(Vx);
    JLabel etiVx = new JLabel("Vx");
    etiVx.setBounds(90, 20, 90, 40);
    this.add(etiVx);

    this.Vy = new JTextField();
    this.Vy.setBounds(120, 60, 100, 30);
    this.add(Vy);
    JLabel etiVy = new JLabel("Vy");
    etiVy.setBounds(90,60,100,30);
    this.add(etiVy);

//BOTTON TO OPEN .TXT
@Override
public void actionPerformed(ActionEvent e) {
    JFileChooser abrir = new JFileChooser();
    if(e.getSource()==this.abrir){
        int returnVal = abrir.showOpenDialog(PanelControl.this);
        if(returnVal == JFileChooser.APPROVE_OPTION);
        File file = abrir.getSelectedFile();

        JOptionPane.showMessageDialog(null,"Abierto");
        try{
            BufferedReader eleccion = new BufferedReader(new FileReader(file));
            String linea = eleccion.readLine();
            while((linea=eleccion.readLine())!=null){
                Vx.setText(file);
                linea = eleccion.readLine();
            }
        }
        catch(IOException f){
            System.out.println(f);
        }
    }

這是什么,

           while(eleccion!=null){
                Vx.setText(file);//how can you set a file handler to a TextField
                linea = eleccion.readLine();
            }

采用,

               while((linea=eleccion.readLine())!=null){
                    Vx.setText(linea);

                }

如果您需要顯示文件中的所有Text,最好使用JTextArea並將從文件中讀取的所有行附加到其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM