java swing对象之间的细微差别

时间:2013-09-19 15:32:04

标签: java swing size jpanel preferredsize

我很难弄清楚为什么我在标题屏幕和设置屏幕上看到的结果之间存在差异。我在调整每个代码之前复制/粘贴了大部分代码......这显然与我的外框中的某些东西有关,但我不知道是什么。我看到的问题是,虽然标题屏幕的正确尺寸为1024x768,背景正确显示,但设置屏幕显示为一个非常小的窗口,好像我没有设置它的大小。即使调整了框的大小,背景图像也只显示在那个空格中。

我删除了标题屏幕内的所有元素,但它仍然保持其大小。有人可以帮忙吗?感谢

OuterFrame

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);
}
}

SetupScreen

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);
}

我将以上内容添加到title和setup类中,以及删除硬编码的resize。问题仍然存在 - 窗口的大小正确,但不是设置。任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:3)

阅读Custom Painting上的Swing教程了解基础知识。在这种情况下,问题是您不会覆盖自定义组件的getPreferredSize()方法,因此布局管理器基本上使用0。

您显示第一个屏幕的原因是您硬编码:

title.setPreferredSize(screenSize);

这是禁忌(由于太多原因需要详细说明)。组件应返回上面提到的首选大小,然后pack()语句将正常工作。

答案 1 :(得分:0)

想出问题是removeAll()语句。我添加了getContentPane。对它而言,它运作良好。