繁体   English   中英

文字区域似乎无效

[英]Text area doesn't seem to be working

我尝试创建一个文本区域,以显示分数列表。 但是由于某种原因,文本区域仅在有人输入时才扩展,甚至不应该允许用户输入。 我以为我在代码中正确编写了它,但是由于某种原因,它似乎不起作用。 我添加的“ hello”短语甚至没有显示在文本区域中。 任何人都可以提供一些建议:

    public HighScores() throws FileNotFoundException, IOException{
    frame.setVisible(true);
    frame.setSize(400,200);
    frame.add(main);
    GridBagConstraints g = new GridBagConstraints();
    g.insets = new Insets(10,10,10,10);
    g.gridx = 0;
    g.gridy = 0;
    main.add(highscorespanel, g);
    highscorespanel.add(highscores);
    g.gridx = 0;
    g.gridy = 1;
    main.add(textareapanel, g);
    Color c = textareapanel.getBackground();
    textareapanel.setBackground(c);
    textareapanel.add(ta);
    ta = new JTextArea ();
    ta.setVisible(true);
    ta.setEnabled(true);
    ta.setEditable(false);

    ta.append("hello");
    JScrollPane sp = new JScrollPane(ta);
    BufferedReader br = new BufferedReader(new FileReader("src/BattleShip/scores.txt"));
    String namescore = br.readLine();
    while(namescore!=null){
        ta.append("\t"+namescore);
    }

我几乎可以肯定,您不再需要答案了,但是您需要移动“ textareapanel.add(ta);”行。 在初始化ta后的某个时刻。

ta = new JTextArea();
textareapanel.add(ta);
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);

编辑

再看一遍,您希望为JTextArea使用JScrollPane,因此您的代码应如下所示:

JTextArea ta = new JTextArea();
ta.setVisible(true);
ta.setEnabled(true);
ta.setEditable(false);

JScrollPane sp = new JScrollPane(ta);
textareapanel.add(sp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM