JFrame setContentPane隐藏其他组件

时间:2015-11-24 19:03:43

标签: java swing user-interface jframe jlabel

所以我在Java中使用GUI,我正在尝试为计时器创建一个简单的主菜单。一切顺利,直到我试图为GUI添加背景。添加后台工作,但现在所有其他组件都消失了(按钮)。我怎么能解决这个问题?

编辑:这是我的新代码。

公共类MainMenu {

// JFrame = the actual menu / frame.
private JFrame frame;
// JLabel = provides text instructions or information on a GUI —
// display a single line of read-only text, an image or both text and an image.
private JLabel background;
// JButton = button.
private JButton alarmClockButton;

// Constructor to create menu
public MainMenu() {
    frame = new JFrame("Alarm Clock");
    alarmClockButton = new JButton("Timer");
    // Add an event to clicking the button.
    alarmClockButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // TODO: CHANGE TO SOMETHING NICER
            JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
                    JOptionPane.ERROR_MESSAGE);
        }
    });
    // Creating the background
    try {
        background = new JLabel(new ImageIcon(ImageIO.read(getClass()
                .getResourceAsStream("/me/devy/alarm/clock/resources/Background.jpg"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    frame.setLayout(new FlowLayout());
    frame.setContentPane(background);
    frame.add(alarmClockButton);
    frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    frame.setVisible(true);
    frame.setSize(450, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    alarmClockButton.setForeground(Color.RED);
}

}

谢谢!

2 个答案:

答案 0 :(得分:3)

frame.setContentPane(background);

您可以将标签用作内容窗格。问题是标签默认情况下不使用布局管理器。

您需要添加:

background.setLayout( new BorderLayout() ); // or whatever layout you want
frame.setContentPane(background);

现在您可以将按钮直接添加到框架中。你不需要面板。

或者,如果你想获得幻想,你可以使用Background Panel,它可以选择缩放或平铺背景图像。

答案 1 :(得分:0)

您可以将JLabel包装在JPanel中,然后将此JPanel添加为ContentPane,而不是将ContentPane设置为JLabel:

    public class MainMenu {

    public static void main(String[] args) {
        new MainMenu();
    }

    // JFrame = the actual menu / frame.
    private JFrame frame;
    private JPanel panel;
    private JPanel bkgPanel;
    // JLabel = provides text instructions or information on a GUI —
    // display a single line of read-only text, an image or both text and an
    // image.
    private JLabel background;
    // JButton = button.
    private JButton alarmClockButton;

    // Constructor to create menu
    public MainMenu() {
        frame = new JFrame("Alarm Clock");
        panel = new JPanel();
        bkgPanel = new JPanel();
        alarmClockButton = new JButton("Timer");
        // Add an event to clicking the button.
        alarmClockButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO: CHANGE TO SOMETHING NICER
                JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
        // Creating the background
        try {
            background = new JLabel(new ImageIcon(
                    ImageIO.read(getClass().getResourceAsStream("/me/devy/alarm/clock/resources/Background.jpg"))));
            bkgPanel.add(background);
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame.setContentPane(bkgPanel);
        frame.add(panel);
        panel.add(alarmClockButton);
        frame.setVisible(true);
        frame.setSize(450, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        alarmClockButton.setForeground(Color.RED);
    }
}