我很难弄清楚为什么我在标题屏幕和设置屏幕上看到的结果之间存在差异。我在调整每个代码之前复制/粘贴了大部分代码......它显然与我的外框架中的某些东西有关,但我不知道是什么。我看到的问题是,虽然标题屏幕以正确的 1024x768 大小出现,并且背景显示正确,但设置屏幕却以一个非常小的窗口出现,就好像我没有设置它的大小一样。即使调整了框的大小,背景图像也仅显示在该空间中。
我已经删除了标题屏幕内的所有元素,但它仍然保持其大小。有人可以帮忙吗?谢谢
外框
public class OuterFrame extends JFrame {
public OuterFrame(String windowHeading) {
int WIDTH = 1024;
int HEIGHT = 768;
final Dimension screenSize = new Dimension(WIDTH,HEIGHT);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel title = new TitleScreen();
title.setLayout(new BoxLayout(title, BoxLayout.PAGE_AXIS));
JButton matchButton = new JButton("New Match");
//Add action listener to button
matchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
removeAll();
JPanel setupScreen = new SetupScreen();
add(setupScreen);
pack();
}
});
JButton exitButton = new JButton("Exit to Windows");
//Add action listener to button
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
System.exit(0);
}
});
matchButton.setAlignmentX(title.CENTER_ALIGNMENT);
exitButton.setAlignmentX(title.CENTER_ALIGNMENT);
title.setPreferredSize(screenSize);
title.add(matchButton);
title.add(Box.createRigidArea(new Dimension(0,25)));
title.add(exitButton);
add(title);
pack();
}
}
标题画面
public class TitleScreen extends JPanel {
public BufferedImage background;
public TitleScreen() {
try {
InputStream is = new BufferedInputStream(new FileInputStream("images/datascreen.png"));
Image image = ImageIO.read(is);
background = (BufferedImage)image;
} catch (Exception a) {
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(background,0,0,1024,768,null);
}
}
设置屏幕
public class SetupScreen extends JPanel {
public BufferedImage background;
public SetupScreen() {
try {
InputStream is = new BufferedInputStream(new FileInputStream("images/datascreen.png"));
Image image = ImageIO.read(is);
background = (BufferedImage)image;
} catch (Exception a) {
}
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(background,0,0,1024,768,null);
}
}
对格式感到抱歉.. 我一辈子都不能让它保持我在代码中使用的缩进。
编辑:
@Override
public Dimension getPreferredSize() {
return new Dimension(1024, 768);
}
我将上述内容添加到标题和设置类中,并删除了硬编码的调整大小。问题仍然存在 - 窗口大小适合标题但不适合设置。任何帮助,将不胜感激..