繁体   English   中英

在程序启动后调用Canvas.setSize()和JFrame.pack()后,JFrame变小

[英]JFrame turns small after calling Canvas.setSize() and JFrame.pack() after the program is started

我正在创建一个程序,我希望能够“动态”设置它的大小(当程序运行时)。 我尝试通过在包含CanvasJFrame上调用Canvas.setSize(width, height)然后JFrame.pack()来完成此操作。 问题是在调用这些方法之后,JFrame会调整大小,以便只显示图标以及关闭和最小化按钮。 如何在程序运行时更改大小? 我还需要打电话吗?

提前致谢!

您不应对添加到JFrame任何组件使用setSize()方法。

Swing使用布局管理器。 pack()方法将调用布局管理器,布局管理器通常会使用组件的首选大小来确定组件的大小/位置。

我猜您的Canvas类正在进行自定义绘制,因此默认情况下首选大小为零,因此无需显示任何内容。

解决方案是覆盖Canvas类中的getPreferredSize()方法以返回适当的大小。

另外,不要称你为Canvas类,因为有一个名称的AWT类,所以它会让人感到困惑。 如果您使用的是AWT Canvas类,则应该使用JPanel类来进行自定义绘制。

首先,您应该添加一个侦听器来侦听来自JFrame resize-events。 调整JFrame的大小时,将调用方法componentResized 然后,您可以获取JFrame的当前大小,并设置为Canvas的大小。

JFrame component = new JFrame("My Frame");

component.addComponentListener(new ComponentAdapter() 
{  
    public void componentResized(ComponentEvent evt) {
        Component c = evt.getComponent();
        Dimension dim = c.getSize();

        // process the Dimension and set your Canvas size
    }
});

这是SSCCE(短,自包含,正确,兼容,示例):

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable {
    private static final long serialVersionUID = 1L;

    // The thread
    private Thread thread;

    // The Canvas
    private Display display;

    // Start the game and it's thread
    public synchronized void start() {
        // Create the thread
        thread = new Thread(this, "Game");
        // Start the thread
        thread.start();
    }

    // The run method
    public void run() {
        // SETUP THE JFRAME

        // Create the Display
        display = new Display(700, 500);
        // Set the title of the JFrame
        setTitle("Game");
        // Set the JFrame to not be resizable
        setResizable(false);
        // Add the Display to the frame
        add(display);
        // Pack the JFrame
        pack();
        // Set the location
        setLocationRelativeTo(null);
        // Add a window listener
        addWindowListener(new WindowAdapter() {
            // When the window is being closed
            public void windowClosing(WindowEvent e) {
                // Stop the game
                stop();
            }
        });
        // Show the JFrame
        setVisible(true);

        try {
            // Make the thread wait for two seconds, so that the change is visible
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // Print the error message
            e.printStackTrace();
        }

        // CHANGE THE SIZE OF THE JFRAME
        // Pretend that this is used for changing the size of the game window
        // by the user, later in the game. I'm just testing it now, when I don't
        // have extremely much code

        // Change the size of the Display
        display.setPreferredSize(new Dimension(800, 600));
        // Pack the JFrame
        pack();
    }

    // Stop the game and it's thread
    public synchronized void stop() {
        try {
            // Join the thread
            thread.join();
        } catch (InterruptedException e) {
            // Print the error message
            e.printStackTrace();
        }
        // Exit the program
        System.exit(0);
    }

    // The main method
    public static void main(String[] args) {
        // Create and start a new Game
        new Game().start();
    }
}

感谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM