各位大侠,我在创建JScrollPane滚动面板是遇见了这样两个问题:
第一个问题:
JTextArea ta=new JTextArea(20,15);
JScrollPane sp=new JScrollPane(ta); //这样写没有错误
JScrollPane sp=new JScrollPane(ta,1,0); //但这样写就抛出异常,能通过编译但就是不能运行。
或者将上句改为JScrollPane sp=new JScrollPane(ta,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
也不行,和上一句一样。我看类库里面明明可以使用啊,为什么这里会出错吗?难道是我的JDK的问题?我用的是最新的JDK-1.6.0_23。
这是我的代码:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DemoScrollPane extends JFrame {
DemoScrollPane() {
super("ScrollPane");
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
JTextArea ta=new JTextArea(20,15);
JScrollPane sp=new JScrollPane(ta,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
cp.add(sp);
setVisible(true);
pack();
}
public static void main(String sd[]){
DemoScrollPane frm=new DemoScrollPane();
}
}第二个问题:
我想为一个Panel或者JTextArea等Component创建一个JScrollPane,想让它在水平方向上不用滚动条,超出水平边界自动换行,垂直方向上使用滚动条,应该怎么实现呢?我按第一个问题那样试了一下但出问题了,行不通,跪求高手指点,谢谢!
第一个问题:
JTextArea ta=new JTextArea(20,15);
JScrollPane sp=new JScrollPane(ta); //这样写没有错误
JScrollPane sp=new JScrollPane(ta,1,0); //但这样写就抛出异常,能通过编译但就是不能运行。
或者将上句改为JScrollPane sp=new JScrollPane(ta,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
也不行,和上一句一样。我看类库里面明明可以使用啊,为什么这里会出错吗?难道是我的JDK的问题?我用的是最新的JDK-1.6.0_23。
这是我的代码:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DemoScrollPane extends JFrame {
DemoScrollPane() {
super("ScrollPane");
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
JTextArea ta=new JTextArea(20,15);
JScrollPane sp=new JScrollPane(ta,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
cp.add(sp);
setVisible(true);
pack();
}
public static void main(String sd[]){
DemoScrollPane frm=new DemoScrollPane();
}
}第二个问题:
我想为一个Panel或者JTextArea等Component创建一个JScrollPane,想让它在水平方向上不用滚动条,超出水平边界自动换行,垂直方向上使用滚动条,应该怎么实现呢?我按第一个问题那样试了一下但出问题了,行不通,跪求高手指点,谢谢!
JScrollPane sp=new JScrollPane(ta,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);2 。设置自动换行
JTextArea textcomponent = new JTextArea();
textcomponent.setLineWrap(true);
textcomponent.setWrapStyleWord(true);
JScrollPane scrollpane = new JScrollPane(textcomponent,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
什么意图