简体   繁体   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();
}
}

In the above code, I can't able to see the JTextArea in JScrollPane. 在上面的代码中,我无法在JScrollPane中看到JTextArea。 Does anybody know what I am doing wrong? 有人知道我在做什么错吗?

  • use JTextArea(int rows, int columns) 使用JTextArea(int行,int列)

  • don't set and remove chatdailog.setSize(300, 400); 不要设置和删除chatdailog.setSize(300, 400);

  • don't set and remove chatHistory.setPreferredSize(new Dimension(150,100)); 不要设置和删除chatHistory.setPreferredSize(new Dimension(150,100));

  • don't set and remove mScrollMessage.add(chatHistory); 不要设置和删除mScrollMessage.add(chatHistory); use JScrollPane scrollPane = new JScrollPane(textArea); 使用JScrollPane scrollPane = new JScrollPane(textArea); instead 代替

  • don't set and remove mScrollMessage.setBounds(4, 10, 150, 100); 不要设置和删除mScrollMessage.setBounds(4, 10, 150, 100);

  • don't set and remove chatdailog.show(); 不要设置和删除chatdailog.show(); use chatdailog.setVisible(true); 使用chatdailog.setVisible(true);

  • add code line chatdailog.pack() before line chatdailog.setVisible(true); chatdailog.setVisible(true);行之前添加代码行chatdailog.pack() chatdailog.setVisible(true);

  • if is there another parent for this JDialog wrap chatdailog.setVisible(true); 如果此JDialog有另一个父级,则包装chatdailog.setVisible(true); into invokeLater() 进入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