写错了!是JComponent子类, 而不是Component子类
补充一个例子:import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;public class Ex extends JApplet implements ActionListener
{
private JButton proper = new JButton("Proper");
private JButton improper = new JButton("Improper");
private JPanel ButtonPanel = new JPanel();
private JFrame properFrame;
private JFrame improperFrame; public void init()
{ properFrame = new MyFrame("Proper");
improperFrame = new MyFrame("Improper");
proper.addActionListener(this);
improper.addActionListener(this);
getContentPane().add(ButtonPanel); ButtonPanel.add(proper);
ButtonPanel.add(improper);
}
public void actionPerformed(ActionEvent evt)
{
Object Source = evt.getSource();
if (Source == proper)
{
if(!properFrame.isVisible())
properFrame.show();
}
else if (Source == improper)
{
if(!improperFrame.isVisible())
improperFrame.show();
} }}class ProperPanel extends JPanel
{
public ProperPanel()
{
setPreferredSize(new Dimension(800,600));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); Graphics2D g2d = (Graphics2D)g;
g2d.fillRect(100,100,100,100);
}
}class ImproperPanel extends JPanel
{
public ImproperPanel()
{
setPreferredSize(new Dimension(800,600));
} public void paintComponent(Graphics g)
{
super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; //*********************************************************
g2d.setTransform(AffineTransform.getScaleInstance(1f,1f)); //The reason about the problem!!!
//********************************************************* g2d.fillRect(100,100,100,100);
}
}class MyFrame extends JFrame
{
private JPanel MyPanel;
private JScrollPane sp; public MyFrame(String cl)
{ setSize(500,400); if (cl == "Proper")
{
setTitle("Proper frame");
MyPanel = new ProperPanel();
}
else if(cl == "Improper")
{
setTitle("Improper frame");
MyPanel = new ImproperPanel();
}
sp = new JScrollPane(MyPanel);
getContentPane().add(sp,"Center");
}
}

解决方案 »

  1.   

    class ImproperPanel extends JPanel
    {
     boolean firstTime = true; public ImproperPanel()
     {
      setPreferredSize(new Dimension(800,600));
     }
     
     public void paintComponent(Graphics g)
     {
      super.paintComponent(g);
     
      Graphics2D g2d = (Graphics2D)g;
     
      if (firstTime) {
        firstTime = false;
      //*********************************************************
        g2d.setTransform(AffineTransform.getScaleInstance(1f,1f)); //The reason about the problem!!!
      //*********************************************************
      } 
      g2d.fillRect(100,100,100,100);
     }
    }
      

  2.   

    这样不行,setTransform跟本没起作用
      

  3.   

    你难道不是做的变形转换吗?java教程中有例子。
      

  4.   

    我是做transform但java教程中的例子没有将其包含有JScrollPane中
    不过我已有办法解决了,就是用transform()替代setTransform().