JScrollPane现在显示其视口 [英] JScrollPane now showing its viewport
问题描述
我正在用Java Swing制作应用程序,但是我遇到了问题.我制作了一个选项卡式面板,需要容纳一个简单的面板和一个滚动面板.简单的面板工作正常,但是在我的滚动面板中我只能看到滚动条,而看不到视口,我的代码如下:
I am making an application with Java Swing and i have a problem. I have made a Tabbed Panel, which need to hold a simple panel, and a scroll-panel. The simple panel is working fine but in my scroll-panel i can only see the scrollbars, but not the viewport, my code is as follows:
ContentPane
ContentPane
public class ContentPane extends JTabbedPane {
private InfoPanel ip;
ScrollPanel sp;
public InfoPanel getIp() {
return ip;
}
public ContentPane(GraphPanel gp) {
this.sp = new ScrollPanel(gp);
this.sp.setViewportView(gp);
this.addTab("Graph", this.sp);
this.ip = new InfoPanel(gp);
this.addTab("Info", ip);
}
}
ScrollPanel
ScrollPanel
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.repaint();
}
}
GraphPanel
GraphPanel
public GraphPanel(StatusBar sb) {
this.sb = sb;
zoomLevel = 5;
sm = new Simulation();
mh = new MouseHandler(this, sm);
this.addMouseListener(mh);
this.setBackground(new Color(240, 165, 98));
this.repaint();
}
由于我没有任何错误或异常,所以我现在完全迷失了要采取的方法.
Since i don't get any errors or exceptions, i am now completely lost in which aproach to take.
推荐答案
您不应将JScrollPane子类化,这是不必要的.
You should not subclass JScrollPane, it is not necessary.
但是,如果这样做,请不要忘记将组件添加到滚动窗格中.
But if you do so, don't forget to add the component to the scrollpane.
在子类中,您不将GraphPanel添加到滚动窗格.
In your subclass you are not adding the GraphPanel to the scroll pane.:
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
// :::: HERE ::: you are not doing anything with gp
// like this.setViewPort( gp ) or something like that
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.repaint();
}
}
尝试:
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
super( gp );
.... etc ...
并让GraphPanel扩展JComponent或JPanel
And have GraphPanel extend JComponent or JPanel
这篇关于JScrollPane现在显示其视口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!