如何使用Timer动态调整帧大小?

时间:2014-01-07 17:16:41

标签: java swing user-interface timer jframe

我正在尝试使用Timer对象动态调整窗口大小,但没有成功...我在构造函数中设置了面板的首选大小,它很好地设置了窗口的大小,但只有一次。初始化程序后,首选大小会更改,但窗口大小保持不变。为什么?因为构造函数只初始化一次,因此不受大小变化的影响?如果是这样,我怎么能绕过这个来实时调整窗口大小?

我知道这不会解决开头评论中给出的练习中的问题,所以请忽略它: - )

/*
 * Exercise 18.15
 * 
 * "(Enlarge and shrink an image) Write an applet that will display a sequence of
 * image files in different sizes. Initially, the viewing area for this image has
 * a width of 300 and a height of 300. Your program should continuously shrink the
 * viewing area by 1 in width and 1 in height until it reaches a width of 50 and
 * a height of 50. At that point, the viewing area should continuously enlarge by 
 * 1 in width and 1 in height until it reaches a width of 300 and a height of 300. 
 * The viewing area should shrink and enlarge (alternately) to create animation
 * for the single image."
 * 
 * Created: 2014.01.07
 */



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Ex_18_15 extends JApplet {


    // Main method
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Ex_18_15 applet = new Ex_18_15();
        applet.isStandalone = true;
        frame.add(applet);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }







    // Data fields

    private boolean isStandalone = false;

    private Image image = new ImageIcon("greenguy.png").getImage();

    private int xCoordinate = 360;
    private int yCoordinate = 300;

    private Timer timer = new Timer(20, new TimerListener());

    private DrawPanel panel = new DrawPanel();


    // Constructor
    public Ex_18_15() {
        panel.setPreferredSize(new Dimension(xCoordinate, yCoordinate));
        add(panel);

        timer.start();
    }



class DrawPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(image, 0, 0, this);
    }
}



class TimerListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(yCoordinate <= 50) {
            yCoordinate++;
            xCoordinate++;
        }

        else if(yCoordinate >= 300) {
            yCoordinate--;
            xCoordinate--;
        }

        panel.setPreferredSize(new Dimension(xCoordinate, yCoordinate));
        repaint();
    }
}

}

1 个答案:

答案 0 :(得分:5)

您需要重新打包JFrame以调整其大小。例如,在ActionListener的末尾:

Window win = SwingUtilities.getWindowAncestor(panel);
win.pack();

但问题是:为什么天堂的名字是你的类扩展JApplet而不是JPanel?或者如果它需要是一个小程序,为什么要将它填充到JFrame中?


修改
关于你的评论:

  

通常不会扩展JFrame而不是JPanel吗?我将它填充到JFrame中,以允许它作为应用程序和applet运行。这就是“Java编程简介”告诉我如何做到这一点:p在actionPerformed方法的末尾添加代码对我没有任何作用; o

您的大多数GUI代码都应该适合创建JPanel,而不是JFrame或JApplets。然后,您可以轻松地将JPanel放置在需要和期望的位置。你的书有严重的问题,如果它告诉你这个问题就不应该被信任。


编辑2
适合我:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class ShrinkingGui extends JPanel {
   private static final int INIT_W = 400;
   private static final int INIT_H = INIT_W;
   private static final int TIMER_DELAY = 20;
   private int prefW = INIT_W;
   private int prefH = INIT_H;

   public ShrinkingGui() {
      new Timer(TIMER_DELAY, new TimerListener()).start();;
   }

   public Dimension getPreferredSize() {
      return new Dimension(prefW, prefH);
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         if (prefW > 0 && prefH > 0) {
            prefW--;
            prefH--;
            Window win = SwingUtilities.getWindowAncestor(ShrinkingGui.this);
            win.pack();
         } else {
            ((Timer)e.getSource()).stop();
         }
      }
   }

   private static void createAndShowGUI() {
      ShrinkingGui paintEg = new ShrinkingGui();

      JFrame frame = new JFrame("Shrinking Gui");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}