【问题标题】:JTextArea won't allow for text to be typed inJTextArea 不允许输入文本
【发布时间】:2014-06-18 05:41:00
【问题描述】:

我已阅读有关 JTextArea 的其他问题并将 setVisible(true) 移至底部,但我的 JTextArea 仍然不允许我显示文本。我的代码在下面,它是一个扩展 JPanel 的类的构造函数,当我输入 JTextArea 时它不会显示任何内容。 JTextArea 出现在中间,称为“newText”。感谢您的帮助!

EditScreen(TaskMaster taskMaster, Task toEdit){
        this.taskMaster = taskMaster;
        editingTask = toEdit;
        textAreaShading = new Color(10, 20, 20, 20);
        initBackground();
        this.setLayout(new BorderLayout(500,500));
        this.setBackground(background); //this might not be initializedset

        topToolbar = new JPanel();
        topToolbar.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 
        topToolbar.setOpaque(false);
        topToolbar.setLayout(new BoxLayout(topToolbar, BoxLayout.X_AXIS));
        category = new JLabel("Choose type:");
        categories = new JComboBox(catS);
        date = new JTextField(10);
        topToolbar.add(category);
        topToolbar.add(categories);
        topToolbar.add(new JLabel("Due Date:"));
        topToolbar.add(date);


        textPanel = new JPanel();
        //textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));  
        textPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));   
        textPanel.setLayout(new BorderLayout());  
        //textPanel.setOpaque(false);
        textPanel.setBackground(Color.CYAN);
        newText = new JTextArea();
        newText.setOpaque(true);
        newText.setBackground(textAreaShading);
        newText.setLineWrap(true);
        newText.setWrapStyleWord(true); 
        textPanel.add(BorderLayout.CENTER, newText);


        bottomPanel = new JPanel();
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(5,5,2,0)); 
        bottomPanel.setOpaque(false);
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
        priority = new JComboBox(priorityS); 
        cancel = new JButton("Cancel");
        save = new JButton("Save");
        bottomPanel.add(new JLabel("Choose Priority:"));
        bottomPanel.add(priority);
        bottomPanel.add(cancel);
        bottomPanel.add(save);


        //set layout and size of frame
        this.add(topToolbar,BorderLayout.NORTH);
        this.add(textPanel, BorderLayout.CENTER); 
        //screenPanel.add(newText, BorderLayout.CENTER); 
        this.add(bottomPanel, BorderLayout.SOUTH);

        initListeners();
        initEditingTask();
        this.setVisible(true);
        textPanel.setVisible(true);
        newText.setVisible(true);
    }

【问题讨论】:

  • 尝试删除 newText.setOpaque(true);
  • (1) 为了尽快获得更好的帮助,请发布Minimal Complete Example 来说明问题。我们无法仅使用代码 sn-p 重现您的问题。 (2) 一般情况下,你根本不需要打电话给newText.setVisible(...)
  • 谢谢,我想避免因为项目很大,我不确定如何提供 MCE。如果我找不到解决方案,我会尝试这样做。我希望这是一个小而快速的解决方案,而我却没有注意到!
  • 根据textAreaShading 声明,调用newText.setBackground(textAreaShading) 会使背景变得很暗,所以如果你的文字也很暗(黑色?),你确定你能够看到 i> 深色背景的深色文本?

标签: java swing jtextarea


【解决方案1】:

好的,有人回答了我的问题,然后删除了该回复,所以我将其发布在这里,这不是我做的,而是程序的问题,所以就在这里!

setLayout(new BorderLayout(500,500));在覆盖整个区域上留有很大的差距。

解决方案:缩小差距

setLayout(new BorderLayout(5,5));

密码之谜:已完成。谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2018-10-19
    • 2015-12-17
    • 1970-01-01
    相关资源
    最近更新 更多