繁体   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