2012-07-25 79 views
2
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的將帶有JTextArea的JScrollPane添加到JDailog中

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

回答

4
  • 使用JTextArea(int rows, int columns)

  • 不設置和刪除chatdailog.setSize(300, 400);

  • 不設置和刪除chatHistory.setPreferredSize(new Dimension(150,100));

  • 不設置和刪除mScrollMessage.add(chatHistory);使用JScrollPane scrollPane = new JScrollPane(textArea);代替

  • 不要s等,並刪除mScrollMessage.setBounds(4, 10, 150, 100);

  • 不設置與前行刪除chatdailog.show();使用chatdailog.setVisible(true);

  • 添加一行代碼chatdailog.pack()chatdailog.setVisible(true);

  • 如果有另一種父母對這個JDialog包裝chatdailog.setVisible(true);invokeLater()

+0

謝謝mK orbel ... – 2012-07-26 09:40:38

0

集的大小如果你有一個佈局,你可以使用new JTextArea(24, 32)pack()得到一個不錯的安排。

1

+0

正確的建議 – mKorbel 2012-07-25 14:01:04