这是我的程序,能实现图片的伸缩,但显示不了上下左右scrollbar.package nxworks.jpcom.net.util;import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;public class Kakudai1
extends JFrame
{
Image image1 = Toolkit.getDefaultToolkit().getImage(
"D:\\airport\\images\\enlarge1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\plane1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\airB.gif"); JLabel lbl_peirihiteyi = new JLabel();
JLabel lbl_peri = new JLabel();
JLabel lbl_periseteyi = new JLabel(); JSlider sld_peri = new JSlider();
JTextField txt_peri = new JTextField();
JToggleButton btn_cancel = new JToggleButton(); JScrollPane jScrollPane1 = new JScrollPane();
DrawPicturePanel custom_pane = new DrawPicturePanel(); public Kakudai1()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
} sld_peri.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{ double peri = sld_peri.getValue()/10.0;
txt_peri.setText("" + peri);
//peri = peri/10.0;
System.out.println("changed-----------");
custom_pane = new DrawPicturePanel( (int)(320 * peri), (int)(40 * peri));
jScrollPane1.getViewport().add(custom_pane, null);
}
}
);
} private void jbInit() throws Exception
{ lbl_peirihiteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peirihiteyi.setVerifyInputWhenFocusTarget(true);
lbl_peirihiteyi.setText("倍率指定");
lbl_peirihiteyi.setBounds(new Rectangle(48, 368, 75, 39));
sld_peri.setMajorTickSpacing(1);
sld_peri.setValue(10);
sld_peri.setMaximum(12);
sld_peri.setMinimum(8);
sld_peri.setMinorTickSpacing(0);
sld_peri.setPaintLabels(false);
sld_peri.setPaintTicks(true);
sld_peri.setBorder(null);
sld_peri.setMinimumSize(new Dimension(36, 40));
sld_peri.setOpaque(true);
sld_peri.setBounds(new Rectangle(126, 376, 113, 28));
lbl_periseteyi.setEnabled(true);
lbl_periseteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_periseteyi.setText("倍率设定");
lbl_periseteyi.setBounds(new Rectangle(246, 382, 67, 15));
txt_peri.setText("1.0");
txt_peri.setBounds(new Rectangle(319, 380, 55, 22)); btn_cancel.setText("消去");
btn_cancel.setBounds(new Rectangle(327, 414, 72, 40));
lbl_peri.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peri.setText("倍");
lbl_peri.setVerticalAlignment(SwingConstants.CENTER);
lbl_peri.setBounds(new Rectangle(388, 379, 41, 26));
this.setResizable(false); jScrollPane1.setBorder(null);
//jScrollPane1.getViewport().setBackground(Color.lightGray);
//jScrollPane1.setAlignmentX( (float) 0.5);
jScrollPane1.setBounds(new Rectangle(40, 40, 320, 320));
jScrollPane1.getViewport().add(custom_pane, null);
//jScrollPane1.setVerticalScrollBarPolicy(1); this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(lbl_peirihiteyi, null);
this.getContentPane().add(sld_peri, null);
this.getContentPane().add(btn_cancel, null);
this.getContentPane().add(lbl_periseteyi, null);
this.getContentPane().add(txt_peri, null);
this.getContentPane().add(lbl_peri, null); this.setTitle("");
this.getContentPane().setLayout(null);
}
class DrawPicturePanel
extends JScrollPane
{ int width1;
int width2;
public DrawPicturePanel()
{
width1 = 320;
width2 = 40;
} public DrawPicturePanel(int width1, int width2)
{
this.width1 = width1;
this.width2 = width2;
repaint();
} public void paint(Graphics g)
{
System.out.println("width1==" + width1);
// System.out.println("width2==" + width2);
g.drawImage(image1, 0, 0, width1, width1, this);
// g.drawImage(image2, width1/2, width1/2, width2, width2, this);
} } public static void main(String args[])
{
Kakudai1 kakudai1 = new Kakudai1();
kakudai1.setSize(450, 450);
kakudai1.show(); }
}
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;public class Kakudai1
extends JFrame
{
Image image1 = Toolkit.getDefaultToolkit().getImage(
"D:\\airport\\images\\enlarge1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\plane1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\airB.gif"); JLabel lbl_peirihiteyi = new JLabel();
JLabel lbl_peri = new JLabel();
JLabel lbl_periseteyi = new JLabel(); JSlider sld_peri = new JSlider();
JTextField txt_peri = new JTextField();
JToggleButton btn_cancel = new JToggleButton(); JScrollPane jScrollPane1 = new JScrollPane();
DrawPicturePanel custom_pane = new DrawPicturePanel(); public Kakudai1()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
} sld_peri.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{ double peri = sld_peri.getValue()/10.0;
txt_peri.setText("" + peri);
//peri = peri/10.0;
System.out.println("changed-----------");
custom_pane = new DrawPicturePanel( (int)(320 * peri), (int)(40 * peri));
jScrollPane1.getViewport().add(custom_pane, null);
}
}
);
} private void jbInit() throws Exception
{ lbl_peirihiteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peirihiteyi.setVerifyInputWhenFocusTarget(true);
lbl_peirihiteyi.setText("倍率指定");
lbl_peirihiteyi.setBounds(new Rectangle(48, 368, 75, 39));
sld_peri.setMajorTickSpacing(1);
sld_peri.setValue(10);
sld_peri.setMaximum(12);
sld_peri.setMinimum(8);
sld_peri.setMinorTickSpacing(0);
sld_peri.setPaintLabels(false);
sld_peri.setPaintTicks(true);
sld_peri.setBorder(null);
sld_peri.setMinimumSize(new Dimension(36, 40));
sld_peri.setOpaque(true);
sld_peri.setBounds(new Rectangle(126, 376, 113, 28));
lbl_periseteyi.setEnabled(true);
lbl_periseteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_periseteyi.setText("倍率设定");
lbl_periseteyi.setBounds(new Rectangle(246, 382, 67, 15));
txt_peri.setText("1.0");
txt_peri.setBounds(new Rectangle(319, 380, 55, 22)); btn_cancel.setText("消去");
btn_cancel.setBounds(new Rectangle(327, 414, 72, 40));
lbl_peri.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peri.setText("倍");
lbl_peri.setVerticalAlignment(SwingConstants.CENTER);
lbl_peri.setBounds(new Rectangle(388, 379, 41, 26));
this.setResizable(false); jScrollPane1.setBorder(null);
//jScrollPane1.getViewport().setBackground(Color.lightGray);
//jScrollPane1.setAlignmentX( (float) 0.5);
jScrollPane1.setBounds(new Rectangle(40, 40, 320, 320));
jScrollPane1.getViewport().add(custom_pane, null);
//jScrollPane1.setVerticalScrollBarPolicy(1); this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(lbl_peirihiteyi, null);
this.getContentPane().add(sld_peri, null);
this.getContentPane().add(btn_cancel, null);
this.getContentPane().add(lbl_periseteyi, null);
this.getContentPane().add(txt_peri, null);
this.getContentPane().add(lbl_peri, null); this.setTitle("");
this.getContentPane().setLayout(null);
}
class DrawPicturePanel
extends JScrollPane
{ int width1;
int width2;
public DrawPicturePanel()
{
width1 = 320;
width2 = 40;
} public DrawPicturePanel(int width1, int width2)
{
this.width1 = width1;
this.width2 = width2;
repaint();
} public void paint(Graphics g)
{
System.out.println("width1==" + width1);
// System.out.println("width2==" + width2);
g.drawImage(image1, 0, 0, width1, width1, this);
// g.drawImage(image2, width1/2, width1/2, width2, width2, this);
} } public static void main(String args[])
{
Kakudai1 kakudai1 = new Kakudai1();
kakudai1.setSize(450, 450);
kakudai1.show(); }
}
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;public class TestImage
extends JFrame {
Image image1 = Toolkit.getDefaultToolkit().getImage(
"E:\\相片\\618017.jpeg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\plane1.jpg");
// Image image2 = Toolkit.getDefaultToolkit().getImage(
// "D:\\airport\\images\\airB.gif");
JLabel lbl_peirihiteyi = new JLabel();
JLabel lbl_peri = new JLabel();
JLabel lbl_periseteyi = new JLabel();
JSlider sld_peri = new JSlider();
JTextField txt_peri = new JTextField();
JToggleButton btn_cancel = new JToggleButton();
JScrollPane jScrollPane1 = new JScrollPane();
DrawPicturePanel custom_pane = new DrawPicturePanel();
public TestImage() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
sld_peri.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
double peri = sld_peri.getValue()/10.0;
txt_peri.setText("" + peri);
//peri = peri/10.0;
System.out.println("changed-----------");
custom_pane = new DrawPicturePanel( (int)(320 * peri), (int)(40 * peri));
jScrollPane1.setViewportView(custom_pane);
}
}
);
}
private void jbInit() throws Exception {
lbl_peirihiteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peirihiteyi.setVerifyInputWhenFocusTarget(true);
lbl_peirihiteyi.setText("倍率指定");
lbl_peirihiteyi.setBounds(new Rectangle(48, 368, 75, 39));
sld_peri.setMajorTickSpacing(1);
sld_peri.setValue(10);
sld_peri.setMaximum(12);
sld_peri.setMinimum(8);
sld_peri.setMinorTickSpacing(0);
sld_peri.setPaintLabels(false);
sld_peri.setPaintTicks(true);
sld_peri.setBorder(null);
sld_peri.setMinimumSize(new Dimension(36, 40));
sld_peri.setOpaque(true);
sld_peri.setBounds(new Rectangle(126, 376, 113, 28));
lbl_periseteyi.setEnabled(true);
lbl_periseteyi.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_periseteyi.setText("倍率设定");
lbl_periseteyi.setBounds(new Rectangle(246, 382, 67, 15));
txt_peri.setText("1.0");
txt_peri.setBounds(new Rectangle(319, 380, 55, 22));
btn_cancel.setText("消去");
btn_cancel.setBounds(new Rectangle(327, 414, 72, 40));
lbl_peri.setFont(new java.awt.Font("Dialog", 0, 16));
lbl_peri.setText("倍");
lbl_peri.setVerticalAlignment(SwingConstants.CENTER);
lbl_peri.setBounds(new Rectangle(388, 379, 41, 26));
this.setResizable(false);
jScrollPane1.setBorder(null);
//jScrollPane1.getViewport().setBackground(Color.lightGray);
//jScrollPane1.setAlignmentX( (float) 0.5);
jScrollPane1.setBounds(new Rectangle(40, 40, 320, 320));
jScrollPane1.setViewportView(custom_pane);
//jScrollPane1.setVerticalScrollBarPolicy(1);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(lbl_peirihiteyi, null);
this.getContentPane().add(sld_peri, null);
this.getContentPane().add(btn_cancel, null);
this.getContentPane().add(lbl_periseteyi, null);
this.getContentPane().add(txt_peri, null);
this.getContentPane().add(lbl_peri, null);
this.setTitle("");
this.getContentPane().setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class DrawPicturePanel
extends JScrollPane {
int width1;
int width2;
public DrawPicturePanel() {
width1 = 320;
width2 = 40;
}
public DrawPicturePanel(int width1, int width2) {
this.width1 = width1;
this.width2 = width2;
repaint();
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("width1==" + width1);
// System.out.println("width2==" + width2);
g.drawImage(image1, 0, 0, width1, width1, this);
// g.drawImage(image2, width1/2, width1/2, width2, width2, this);
}
//重载此方法后就可以了
public Dimension getPreferredSize()
{
return new Dimension(width1, width1);
}
}
public static void main(String args[]) {
TestImage kakudai1 = new TestImage();
kakudai1.setSize(450, 450);
kakudai1.show();
}
}