【发布时间】:2015-03-29 01:08:03
【问题描述】:
我正在尝试向 JApplet 组件添加滚动条。我知道我不应该使用它,而应该使用 JPanel,但为了简洁起见,我将保留它,就像我正在关注的教程中一样。
如您所见,我尝试添加一个 ScrollPane 组件,并将小程序添加到它。然后我将滚动窗格添加到框架中。
结果是我可以看到一个垂直滚动条,但它确实具有滚动的能力。实际上缺少滚动光标。向上和向下箭头也不会滚动。我想向下滚动到我绘制的线中超出可见区域的部分。
我做错了什么?
public class App {
private App() {
final int WINHSIZE = 800;
final int WINVSIZE = 600;
class MyJApplet extends JApplet {
public void init() {
setBackground(Color.black);
setForeground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(0, 0, 2000, 2000);
}
}
}
JFrame f = new JFrame("Title");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JApplet applet = new MyJApplet();
JScrollPane myScrollPane = new JScrollPane(applet, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add("Center", myScrollPane);
applet.init();
f.pack();
f.setSize(new Dimension(WINHSIZE, WINVSIZE));
f.setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
【问题讨论】:
标签: java swing jscrollpane graphics2d japplet