繁体   English   中英

将带有JTextArea的JScrollPane添加到JDailog中

[英]Adding JScrollPane with JTextArea into JDailog

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。 有人知道我在做什么错吗?

  • 使用JTextArea(int行,int列)

  • 不要设置和删除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.setVisible(true);行之前添加代码行chatdailog.pack() chatdailog.setVisible(true);

  • 如果此JDialog有另一个父级,则包装chatdailog.setVisible(true); 进入invokeLater()

如果您有布局,则可以使用new JTextArea(24, 32)pack()来获得一个不错的布局。

设置JTextArea的大小

chatHistory.setSize(new Dimension(width,height));

暂无
暂无

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

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