public class DailogDemo
{
private JDialog chatdailog;
private JTextArea chatHistory;
private JScrollPane mScrollMessage;
DailogDemo()
{
chatdailog=new JDialog();
chatdailog.setSize(300, 400);
chatHistory=new JTextArea();
chatHistory.setPreferredSize(new Dimension(150,100));
mScrollMessage=new JScrollPane();
mScrollMessage.add(chatHistory);
mScrollMessage.setBounds(4, 10, 150, 100);
mScrollMessage.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
chatdailog.add(mScrollMessage);
chatdailog.show();
}
public static void main(String args[])
{
new DailogDemo();
}
}
在上面的代码中,我看不到 JScrollPane 中的 JTextArea。有人知道我做错了什么吗?
最佳答案
不要设置和删除
chatdailog.setSize(300, 400);
不要设置和删除
chatHistory.setPreferredSize(new Dimension(150,100));
不要设置和删除
mScrollMessage.add(chatHistory);
使用JScrollPane scrollPane = new JScrollPane(textArea);
相反不要设置和删除
mScrollMessage.setBounds(4, 10, 150, 100);
不要设置和删除
chatdailog.show();
使用chatdailog.setVisible(true);
添加代码行
chatdailog.pack()
行前chatdailog.setVisible(true);
是否还有另一个父项
JDialog
包裹chatdailog.setVisible(true);
进入invokeLater()
关于java - 将 JScrollPane 和 JTextArea 添加到 JDailog 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11649564/