这样做可以 PaintPanel pp = new PaintPanel(); pp.setPreferredSize(new Dimension(2030, 2030)); JScrollPane sp = new JScrollPane(); sp.getViewport().add(pp); getContentPane().setLayout(new BorderLayout()); getContentPane().add(sp, BorderLayout.CENTER);
这样做可以 PaintPanel pp = new PaintPanel(); pp.setPreferredSize(new Dimension(2030, 2030)); JScrollPane sp = new JScrollPane(); sp.getViewport().add(pp); getContentPane().setLayout(new BorderLayout()); getContentPane().add(sp, BorderLayout.CENTER);
import java.awt.*; import javax.swing.*; import java.awt.geom.*;public class Test extends JFrame{ public Test(){ JPanel panel = new JPanel(){ { this.setPreferredSize(new Dimension(2005,2005)); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; Rectangle2D rec = new Rectangle2D.Double(0,0,2000,2000); g2.setPaint(Color.red); g2.draw(rec); } }; Container con = this.getContentPane(); int v = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS; int h = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane p = new JScrollPane(panel); con.add(p,BorderLayout.CENTER); } public void show(){ this.setTitle("A big picture"); this.setSize(400,400); super.show(); } public static void main(String[] args){ new Test().show(); } }上面是我写的代码,已经测试过了。可以达到你要的效果。 你自己运行一下看看。
this.contentPane().add(js)就行了
2. 把你乘放2000*2000的图的组件放到JScrollPane中 js.add(canvas);
3. 把JScrollPane 加入到窗体中 frame.contentPane().add(js);JScrollPane 有两个构造方法:JScrollPane(), JScrollPane(component c)
import java.awt.geom.*;
import javax.swing.*;public class VisualGraph extends JFrame{
PaintPanel paintPanel; public VisualGraph(){
super("Visual Graph"); getContentPane().add(new JScrollPane(new PaintPanel())); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(690,530);
setVisible(true);
}
public static void main(String[] args)
{
VisualGraph vg = new VisualGraph();
}}class PaintPanel extends JPanel{
public void paint(Graphics g){
super.paint(g); Graphics2D g2D = ( Graphics2D ) g; g2D.draw(new Rectangle.Double(100,100,10,10));
g2D.draw(new Rectangle.Double(2000,2000,10,10));
}
}
pp.setPreferredSize(new Dimension(2030, 2030));
JScrollPane sp = new JScrollPane();
sp.getViewport().add(pp);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(sp, BorderLayout.CENTER);
pp.setPreferredSize(new Dimension(2030, 2030));
JScrollPane sp = new JScrollPane();
sp.getViewport().add(pp);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(sp, BorderLayout.CENTER);
import javax.swing.*;
import java.awt.geom.*;public class Test extends JFrame{
public Test(){
JPanel panel = new JPanel(){
{
this.setPreferredSize(new Dimension(2005,2005));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle2D rec = new Rectangle2D.Double(0,0,2000,2000);
g2.setPaint(Color.red);
g2.draw(rec);
}
}; Container con = this.getContentPane();
int v = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
int h = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane p = new JScrollPane(panel);
con.add(p,BorderLayout.CENTER);
} public void show(){
this.setTitle("A big picture");
this.setSize(400,400);
super.show();
} public static void main(String[] args){
new Test().show();
}
}上面是我写的代码,已经测试过了。可以达到你要的效果。
你自己运行一下看看。