当前位置: 代码迷 >> Java相关 >> [求助]请教关于滚动条的问题!!
  详细解决方案

[求助]请教关于滚动条的问题!!

热度:250   发布时间:2007-11-04 21:48:30.0
[求助]请教关于滚动条的问题!!

请问在第42行中,怎样编写关于JTextArea的滚动条??
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class lianxi extends JFrame
{
lianxi(String Title)
{
super(Title);
LianJie();
WenBen();
}
JMenuBar jmenubar=new JMenuBar();
JMenu[] jmenu=new JMenu[]{new JMenu("文件"),new JMenu("编辑")};
JMenuItem[][] jmenuitem=new JMenuItem[][]{{new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")},
{new JMenuItem("剪切"),new JMenuItem("复制"),new JMenuItem("粘贴"),new JMenuItem("删除")}};
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

}
};
public void LianJie()
{
int i,j;
for(i=0;i<jmenu.length;i++)
{
jmenubar.add(jmenu[i]);
for(j=0;j<jmenuitem[i].length;j++)
{
jmenu[i].add(jmenuitem[i][j]);
jmenuitem[i][j].addActionListener(action);
}
}
this.setJMenuBar(jmenubar);
}
public void WenBen()
{
JTextArea word=new JTextArea();
this.getContentPane().add(word);
// 第42行
}
public static void main(String[] args)
{
lianxi L=new lianxi("菜单窗口");
L.setSize(600,400);
L.setLocation(180,180);
L.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
L.setVisible(true);
}
}

搜索更多相关的解决方案: 滚动  

----------------解决方案--------------------------------------------------------
C#里有个关于文本域溢出的属性:autoflow,不知道Java中有没有,窗体这块不是很熟
----------------解决方案--------------------------------------------------------

把:
this.getContentPane().add(word);
改成:
this.getContentPane().add(new JScrollPane(word));
就OK了

[此贴子已经被作者于2007-11-4 23:36:47编辑过]


----------------解决方案--------------------------------------------------------
或者改成这样都可以了

JTextArea word=new JTextArea();
JScrollPane jsp = new JScrollPane(word);
jsp.setVerticalScrollBar(new JScrollBar());
jsp.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));

this.getContentPane().add(jsp);
再有不明白的话可以再上来问.
----------------解决方案--------------------------------------------------------
回复:(Eastsun)把:this.getContentPane().add(word...

如果改成你建议的方法,编译的时候,出现报错报告:lianxi.java:41: 找不到符号
符号: 构造函数 JScrollBar(javax.swing.JTextArea)
位置: 类 javax.swing.JScrollBar
this.getContentPane().add(new JScrollBar(word));

^
1 错误
请再次赐教!!
Eastsun:刚才写错了,应该是JScrollPane

[此贴子已经被Eastsun于2007-11-4 23:38:04编辑过]


----------------解决方案--------------------------------------------------------
回复:(netstriker) 或者改成这样都可以了 ...
其实这两个代码就能产生滚动条: JTextArea word=new JTextArea();
JScrollPane jsp = new JScrollPane(word);
为什么还要加上后面的代码呢?请赐教!!
----------------解决方案--------------------------------------------------------
回复:(Eastsun)把:this.getContentPane().add(word...
好的,谢谢你的指导!!
----------------解决方案--------------------------------------------------------
  其实这是非必要的,可以不加,这只是为了代码有可读性.
----------------解决方案--------------------------------------------------------
回复:(netstriker) 其实这是非必要的,可以不加,这...

哦,原来这样啊,我知道了!谢谢你的指导!


----------------解决方案--------------------------------------------------------
  相关解决方案