为什么去掉pack(); 就可以了,求解释下原理?

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

public class FrameTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new SimpleFrame();
frame.setUndecorated(true);
frame.setResizable(true);
frame.setTitle("AAAAAAAAA");
frame.toFront();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class SimpleFrame extends JFrame
{

public SimpleFrame()
{
add(new Content());
pack();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;

setSize(screenWidth/2,screenHeight/2);
setLocationByPlatform(true);

Image image = Toolkit.getDefaultToolkit ().getImage ("C:\\Users\\Asuna\\Pictures\\00010.JPEG");
setIconImage(image);

}
}

class Content extends JComponent
{
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;

private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

public void paintComponent(Graphics g)
{
g.drawString("BBBBBBBBBBBBBBBBBB",DEFAULT_WIDTH/2,DEFAULT_HEIGHT/2);
}

public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
为什么用上frame.setUndecorated(true); 这个方法就会报错 不就只是把标题栏去掉吗

动漫人物
浏览 66回答 1
1回答

繁星点点滴滴

试一下把这句加上去 this.dispose();应该这样写frame.dispose();frame.setUndecorated(true);
打开App,查看更多内容
随时随地看视频慕课网APP