请教一个关于JScrollPane的问题

freebirdlib2 2004-08-08 08:27:27
有谁能告诉我下面这段代码为什么不能显示滚动条啊,要显示滚动条应该怎么做,最好有代码,谢谢!


public class Edge extends JFrame{
public JPanel pane=new JPanel();

public Edge(){

super("Drawing");
setSize(500,400);
JScrollPane scrollpane=new JScrollPane();
JScrollBar bar1=new JScrollBar();
JScrollBar bar2=new JScrollBar();
scrollpane.setVerticalScrollBar(bar1);
scrollpane.setVerticalScrollBar(bar2);
pane.add(scrollpane);
setContentPane(pane);
ExitWindow exit=new ExitWindow();
addWindowListener(exit);


}
public static void main(String[] args) {

Edge frame=new Edge();
frame.show();


}
class ExitWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ntzls 2004-08-08
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Edge extends JFrame{
public JPanel pane=new JPanel();
public Edge(){
super("Drawing");
setSize(500,400);
JScrollBar bar1=new JScrollBar();
JScrollBar bar2=new JScrollBar(JScrollBar.HORIZONTAL);
pane.setLayout(new BorderLayout());
pane.add(bar1,BorderLayout.EAST);
pane.add(bar2,BorderLayout.SOUTH);
setContentPane(pane);
ExitWindow exit=new ExitWindow();
addWindowListener(exit);
}
public static void main(String[] args) {
Edge frame=new Edge();
frame.setVisible(true);
}
class ExitWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
kkcncry 2004-08-08
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JScrollBar1 implements AdjustmentListener
{
JScrollBar scrollBar1;
JScrollBar scrollBar2;
JPanel panel1;
JLabel label2 = new JLabel("刻度:",JLabel.CENTER);

public JScrollBar1()
{
JFrame f = new JFrame("JScrollBarDemo");
Container contentPane = f.getContentPane();

JLabel label1 = new JLabel(new ImageIcon(".\\icons\\flower.jpg"));
panel1 = new JPanel();
panel1.add(label1);
scrollBar1 = new JScrollBar(JScrollBar.VERTICAL,10,10,0,100);
scrollBar1.setUnitIncrement(1);
scrollBar1.setBlockIncrement(10);
scrollBar1.addAdjustmentListener(this);

scrollBar2 = new JScrollBar();
scrollBar2.setOrientation(JScrollBar.HORIZONTAL);
scrollBar2.setValue(0);
scrollBar2.setVisibleAmount(20);
scrollBar2.setMinimum(10);
scrollBar2.setMaximum(60);
scrollBar2.setBlockIncrement(5);
scrollBar2.addAdjustmentListener(this);

contentPane.add(panel1,BorderLayout.CENTER);
contentPane.add(scrollBar1,BorderLayout.EAST);
contentPane.add(scrollBar2,BorderLayout.SOUTH);
contentPane.add(label2,BorderLayout.NORTH);

f.setSize(new Dimension(200,200));
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void adjustmentValueChanged(AdjustmentEvent e)
{
if ((JScrollBar)e.getSource() == scrollBar1)
label2.setText("垂直刻度:"+e.getValue());
if ((JScrollBar)e.getSource() == scrollBar2)
label2.setText("水平刻度:"+e.getValue());
}
public static void main(String[] arg)
{
new JScrollBar1();
}
}


yangFrame 2004-08-08
  • 打赏
  • 举报
回复
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS)
这样的话,不管是否需要滚动条,就会出现滚动条了。

以前不出现滚动条,是因为你没有添加viewport,所以没必要显示滚动条。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧