【问题标题】:JTextArea text invisible and no scrollJTextArea 文本不可见且无滚动
【发布时间】:2015-05-14 11:57:58
【问题描述】:

第一次用 Java 编写 GUI

到目前为止,当我单击“坐标异常”时,JTextArea 中会显示一个字符串数组列表,但查看文本的唯一方法是突出显示它。

我还尝试向 JTextArea 添加滚动条,但没有成功。

JTextArea textArea = new JTextArea();
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);

JButton btnNewButton_1 = new JButton("Coordinate Anomalies");

btnNewButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> anomalies = vessels.coordinateAnomaly();
        JScrollPane jp = new JScrollPane();
        jp.setViewportView(textArea);

        for (String a : anomalies) {
            textArea.append(a + "\n");
        }

        textArea.setBounds(10, 79, 172, 339);
        frame.getContentPane().add(textArea);
    }
});

btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);

这是我的代码(很抱歉出现问题),我对 GUI 不熟悉,因此我的压力再怎么强调也不为过,任何建议都非常感谢。

【问题讨论】:

  • Layout managers 是你的朋友
  • Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。

标签: java eclipse swing


【解决方案1】:

一个组件只能有一个父级。在应用程序启动时添加滚动窗格,而不是 JTextArea

frame.add(new JScrollPane(textArea));

【讨论】:

  • 我试过了,JtextArea 仍然存在,但字符串不再打印,也没有滚动。 oi57.tinypic.com/np2cg9.jpg
  • 不要在ActionListener 中添加新的滚动窗格,只需更新textArea 即可
  • oi57.tinypic.com/2ai0r47.jpg 好的,所以没有滚动,在我单击按钮之前 JtextArea 不可见,但是当我单击按钮时,文本不再可见。只需要确定如何在单击按钮并滚动之前显示空的 Jtextarea。
  • 您仍在侦听器中添加文本区域。至于第二部分,请查看文档中的setText...
  • 如果它不在动作监听器中,那么当点击按钮时我的信息/文本区域都不会显示,我尝试在动作监听器之前和之后添加它。也许 ELI5 ? ...
【解决方案2】:

简单的解决方案:

JTextArea 添加到JFrame 时,只需像这样在其中添加滚动窗格

JTextArea textarea = new JTextArea();
add(new JScrollPane(textarea));

【讨论】:

    【解决方案3】:

    在 GUI 已经显示的情况下添加组件时,您需要调用 re/in/validate 来布置所有内容,并调用 repaint() 来显示它

    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ArrayList<String> anomalies = vessels.coordinateAnomaly();
            JScrollPane jp = new JScrollPane();
            jp.setViewportView(textArea);
    
            for (String a : anomalies) {
                textArea.append(a + "\n");
            }
    
            textArea.setBounds(10, 79, 172, 339);
            frame.getContentPane().add(jp); // changed
            frame.getContentPane().invalidate(); // added
            frame.getContentPane().validate(); // added
            frame.getContentPane().repaint(); // added
        }
    });
    

    【讨论】:

    【解决方案4】:

    感谢@Reimeus 的帮助,这是我最终实施的解决方案,它奏效了。

    JTextArea textArea = new JTextArea();
    JScrollPane jp = new JScrollPane(textArea);
    jp.setBounds(10, 79, 172, 339);
    frame.getContentPane().add(jp);
    
    JButton btnNewButton_1 = new JButton("Coordinate Anomalies");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            textArea.setText("");
            ArrayList<String> anomalies = vessels.coordinateAnomaly(); 
    
            for(String a : anomalies){
                   textArea.append(a + "\n");
                }
        }
    });
    btnNewButton_1.setBounds(10, 45, 172, 23);
    frame.getContentPane().add(btnNewButton_1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多