【发布时间】: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> 深色背景的深色文本?