【问题标题】:Subtle differences between java swing objectsjava swing对象之间的细微差别
【发布时间】:2013-09-19 15:32:04
【问题描述】:

我很难弄清楚为什么我在标题屏幕和设置屏幕上看到的结果存在差异。我在调整每个代码之前复制/粘贴了大部分代码......它显然与我的外框架中的某些东西有关,但我不知道是什么。我看到的问题是,虽然标题屏幕以正确的 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);
}

我将上述内容添加到标题和设置类中,并删除了硬编码的调整大小。问题仍然存在 - 窗口大小适合标题但不适合设置。任何帮助将不胜感激..

【问题讨论】:

  • 1) 到部署时,这些资源可能会变成embedded-resource。在这种情况下,资源必须由URL 而不是File 访问。请参阅标签的info page,了解形成URL 的方法。 2) 源代码中的一个空白行总是就足够了。 { 之后或 } 之前的空行通常也是多余的。

标签: java swing size jpanel preferredsize


【解决方案1】:

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

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

title.setPreferredSize(screenSize);

这是一个禁忌(有太多的原因在这里详细介绍)。组件应返回其首选大小,然后 pack() 语句将正常工作。

【讨论】:

  • 所以与其设置首选尺寸,我应该重写返回尺寸的命令,告诉它只返回我想要的尺寸而不是试图找出它?
  • 我想我是在重复你刚才所说的话,但我想确保我理解这里发生了什么。
  • telling it to just return the size I want rather than trying to figure it out? - 确切地说,您的自定义绘画代码知道尺寸应该是多少。所以这是应该返回的值。这是所有 Swing 组件的工作方式。
【解决方案2】:

发现问题出在 removeAll() 语句上。我添加了 getContentPane。到它,它工作得很好。

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2011-04-26
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多