首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖TextArea

覆盖TextArea
EN

Stack Overflow用户
提问于 2016-06-02 02:21:26
回答 1查看 209关注 0票数 0

我想重写TextArea中的字符串,我很熟悉setText( String ),但是它似乎不起作用。我想让textArea根据用户输入的内容来读取txt文件。我的代码如下:

代码语言:javascript
复制
first class: 

    public String getUsuario(){

        return txtUsuario.getText();
    }
}

second class: 

public class PanelResultado extends JPanel
{

    private JLabel lblMostrar;
    private JScrollPane scrollPane;
    private JTextArea textArea;
    private PanelUsuario panelUsuario;
    private InterfazMilkyWay principal;
    private PanelResultado resultado;

    /**
     * Constructor for objects of class PanelInfo
     */
    public PanelResultado()
    {
        this.setLayout(null);

        textArea = new JTextArea("");
        textArea.setEditable(false);
        textArea.setBorder(BorderFactory.createLineBorder(Color.gray));

        panelUsuario = new PanelUsuario();    

        //JScrollPane scrollPane = new JScrollPane(textArea);  // le pone un scrollPane al txtArea 

        lblMostrar = new JLabel("Resultado;");
        lblMostrar.setBounds(0,0,385,30);
        textArea.setBounds(0,30,440,110);  
        this.setBackground(Color.WHITE);

        add(lblMostrar);
        add(textArea);
        //add(scrollPane, BorderLayout.CENTER);




    }
    public void mostrar(){
        String tema = panelUsuario.getUsuario();
        String texto = "";
          switch (PlanetaActual.planetaActual){


            case 0:
             textArea.setText("holaaaa");
                if (tema.equals("temperatura") || tema.equals("Temperatura")){

                    System.out.println("temperatura");

                    texto = "";
                    try {
                        Scanner scanner = new Scanner(new File("temperatura mercurio.txt"));

                        while (scanner.hasNext()) {
                            // mientras el scanner tenga otra linea
                            texto += scanner.hasNext();


                        }
                        textArea.setText(texto);
                    } catch (FileNotFoundException e) {

                        texto = "El archivo no se encuentra";
                    }
                    textArea.setText(texto);
                }
                else if ( tema.equals("posicion") || tema.equals("Posicion")|| tema.equals("Posición")){
                            texto = "";
                    try {
                        Scanner scanner = new Scanner(new File("posicion mercurio.txt"));

                        while (scanner.hasNext()) {
                            // mientras el scanner tenga otra linea
                            texto += scanner.hasNext();

                        }
                    } catch (FileNotFoundException e) {
                        texto = "El archivo no se encuentra";
                    }
                    textArea.setText(texto);

               }else if (tema.equals("gravedad") || tema.equals("Gravedad")){
                            texto = "";
                    try {
                        Scanner scanner = new Scanner(new File("gravedad mercurio.txt"));

                        while (scanner.hasNext()) {
                            // mientras el scanner tenga otra linea
                            texto += scanner.hasNext();

                        }
                    } catch (FileNotFoundException e) {
                       texto ="El archivo no se encuentra";
                    }
                    textArea.setText(texto);

             }       else {
               texto ="no hay información de este tema en este planeta";
               textArea.setText(texto);
             }
             break;
EN

回答 1

Stack Overflow用户

发布于 2016-06-02 02:39:41

这可能是因为您正在尝试更改public函数中的private变量。你必须改变

JTextArea textArea;

JTextArea textArea;

我还没有测试它,所以我不确定这是否是问题所在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37576095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档