添加自定义背景时,JButton和JLabel会消失

时间:2013-02-21 13:08:37

标签: java swing jframe jbutton jlabel

添加自定义背景时,

JButtonJLabel消失。我在程序中没有看到任何问题,但也许你们找到了解决方案!我认为这只是我忘记的一件小事,但我无法弄清楚。

以下是代码:

GameWindow.java

setContentPane(new StartImagePanel(RollrackLogo));
out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");

背景看起来很完美,但不是JButtonJLabel

代码StartImagePanel.java

public class StartImagePanel extends JComponent{
    private Image image;
    public StartImagePanel(Image image) {
        this.image = image;
    }
    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }
}

2 个答案:

答案 0 :(得分:2)

您的按钮和标签会添加到GameWindow框架中,而应将其添加到其contentPane setContentPane(new StartImagePanel(RollrackLogo));中。这就是为什么他们没有展示,他们被添加到框架中。

创建StartImagePanel的变量并添加按钮和标签,它们应该显示出来。

StartImagePanel contentPanel = new StartImagePanel(RollrackLogo);
setContentPane(contentPanel);

...

out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
contentPanel.add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
contentPanel.add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");

回答纠纷

第一段中的说法是完全错误的。这是证明它的来源。

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

public class AddToCustomContentPane {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new FlowLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.setBackground(Color.RED);

                JFrame f = new JFrame("Demo");
                f.setContentPane(gui);

                // Acid test.  Can we add buttons direct to the frame?
                f.add(new JButton("Button 1"));
                f.add(new JButton("Button 2"));

                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

在给出自定义面板代码后进行编辑

这是一个片段,用于在黑色图像背景上显示按钮和标签,我删除了不需要的内容(听众)。

public static void main(String[] v) {

class StartImagePanel extends JPanel {
  private Image image;
  public StartImagePanel(Image image) {
      this.image = image;
  }
  @Override
  protected void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, null);
  }
}

class GameWindow extends JFrame{
  public GameWindow() {
    BufferedImage RollrackLogo;
    RollrackLogo = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);
    final JButton JBRandom = new JButton();
    final JLabel JLWelcome = new JLabel();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    StartImagePanel panel = new StartImagePanel(RollrackLogo);
    setContentPane(panel);
    setExtendedState(MAXIMIZED_BOTH);
    setVisible(true);
    JLWelcome.setText("Welcome to Rollrack");
    panel.add(JLWelcome);
    JLWelcome.setVisible(true);
    JBRandom.setText("Random");
    panel.add(JBRandom);
    JBRandom.setVisible(true);
  }
}

GameWindow window = new GameWindow();
window.pack();
window.setVisible(true);
}

答案 1 :(得分:1)

我宁愿使用JFrame的实例,而不是扩展它,正如@Andrew Thompson在另一个问题中提出的那样。
但是,如果您要扩展它,那么在构造函数中调用super()可能是个好习惯。

此外,我们可能需要知道StartImagePanel中发生了什么 对我来说,似乎是问题。

  1. 确保GameWindowStartImagePanel正确扩展其超类(致电super();)。
  2. 确保StartImagePanel具有正确的Layout
  3. 在设置框架可见之前添加组件。这也意味着您不需要JLWelcome.setVisible(true);
  4. 确保您的代码在EDT中执行(事件 - 调度线程)。
  5. 示例:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class GameWindow extends JFrame{
        BufferedImage rollrackLogo;
        JButton jbRandom;
        JLabel jlWelcome;
    
        public GameWindow() {
            super();
            jbRandom = new JButton("Random");
            jlWelcome = new JLabel("Welcome to Rollrack, " +
                    namewindow.name);
    
            rollrackLogo = new BufferedImage(400, 200,
                    BufferedImage.TYPE_INT_RGB);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setContentPane(new StartImagePanel(rollrackLogo));
    
            // Add your components.
            add(jlWelcome);
            add(jbRandom);
    
            addKeyListener(new KeyListener() {
                @SuppressWarnings("static-access")
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == e.VK_ESCAPE){
                        System.exit(7);
                    }
                }
    
                @Override
                public void keyReleased(KeyEvent arg0) {}
    
                @Override
                public void keyTyped(KeyEvent arg0) {}
            });
    
            // Pack, or otherwise set fullscreen.
            pack();
    
            // Now, set frame visible.
            setVisible(true);
        }
    }
    

    修改:现在您已发布了StartImagePanel的代码,我发现您正在展开JComponent。按照我之前的建议(致电super),设置Layout,然后延长JPanel