gpt4 book ai didi

java - JScrollPane 现在显示其视口(viewport)

转载 作者:行者123 更新时间:2023-11-30 05:17:22 25 4
gpt4 key购买 nike

我正在使用 Java Swing 制作应用程序,但遇到了问题。我制作了一个选项卡式面板,它需要容纳一个简单的面板和一个滚动面板。简单的面板工作正常,但在我的滚动面板中我只能看到滚动条,但看不到视口(viewport),我的代码如下:

内容 Pane

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);
}
}

滚动面板

public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.repaint();
}
}

图形面板

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();
}

由于我没有收到任何错误或异常,我现在完全迷失了该采取哪种方法。

最佳答案

您不应该子类化 JScrollPane,这是没有必要的。

但是如果您这样做,请不要忘记将该组件添加到滚动 Pane 中。

在您的子类中,您没有将 GraphPanel 添加到滚动 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

关于java - JScrollPane 现在显示其视口(viewport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/448832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com