如何将按钮和JPanels添加到ZOrderComponent? [英] How to add buttons and JPanels to a ZOrderComponent?

查看:154
本文介绍了如何将按钮和JPanels添加到ZOrderComponent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有背景图像的JFrame,在背景图像的中心居中并朝底部放置一个JLabel,左右两侧分别有两个按钮,分别表示"Stay"和"Leave".这已经创建.问题出现在每个项目的顺序上.我无法在背景图像上看到带有文本和按钮的JLabel,但它们都显示了出来.这是我的代码;任何意见,将不胜感激.先感谢您.

I'm trying to create a JFrame with a background image, a JLabel over the background image centered and toward the bottom, with two buttons on the right and left that say "Stay" and "Leave". This is already created. The issue arises with the order of each of the items. I cannot get the JLabel with text and buttons over the background image, with both of them showing. Here is my code; any advice would be appreciated. Thank you in advance.

public class SceneOne {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        JFrame SceneOne = new JFrame();

        ImageIcon image = new ImageIcon(
                "C:/Users/alan/Downloads/scary_forest_2.jpg");
        JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(imageLabel, BorderLayout.CENTER);
        SceneOne.add(panel);
        SceneOne.setResizable(true);
        imageLabel.setVisible(true);
        SceneOne.pack();

        JButton Leave=new JButton("Leave");
        JButton Stay= new JButton ("Stay");
        JPanel Leave1= new JPanel(new FlowLayout());
        JPanel Stay1=new JPanel(new FlowLayout());
        FlowLayout two = new FlowLayout(FlowLayout.LEFT);
        FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
        Leave1.setLayout(two);
        Stay1.setLayout(three); 
        Stay1.add(Leave);
        Leave1.add(Stay);
        Leave1.setOpaque(false);
        Stay1.setOpaque(false);
        SceneOne.add(Leave1);
        SceneOne.add(Stay1);


        JLabel label1 = new JLabel("Test");
        SceneOne.add(label1);

        label1.setText("<html><font color='red'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
                + "except the thick and powerful moonlight."
                + "<br>"
                + "You are alone leaving a costume party at a friend's place."
                + "It was rather boring and you decided to leave early."
                + "A stutter is heard and your"
                + "<br>"
                + "car begins to shake"
                + "Your headlights and car lights crack. The engine is left dead silent."
                + "You are left in a total silence"
                + "and baked in merely the moonlight."
                + "<br>"
                + "There is a mere second of silence till a harsh chill ripes through the"
                + "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
        label1.setHorizontalAlignment(JLabel.CENTER);
        label1.setVerticalAlignment(JLabel.BOTTOM);
        label1.setVisible(true);
        label1.setOpaque(false);



      SceneOne.setComponentZOrder(panel, 0);
        SceneOne.setComponentZOrder(label1, 0);
     //  SceneOne.setComponentZOrder(Leave1,0);
      // SceneOne.setComponentZOrder(Stay1,0);



        SceneOne.setSize(400,320);
        SceneOne.setTitle("The Car");
        SceneOne.setVisible(true);
        SceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SceneOne.setLocation(500, 300);
    }

}

推荐答案

首先遵循Java命名约定.变量名称不应以大写字母开头.我从未见过像使用变量名一样的教程,教科书或论坛示例.不要编造自己的约定!

First of all follow Java naming conventions. Variable names should NOT start with an upper case character. I have never seen a tutorial, text book or forum example that uses variable names like you are using. Don't make up your own conventions!

JFrame的默认布局管理器是BorderLayout.当您使用frame.add(...)方法时,默认情况下会将组件添加到BorderLayout的CENTER中.只能将一个组件添加到CENTER,因此仅显示最后一个组件.

The default layout manager for a JFrame is a BorderLayout. When you use the frame.add(...) method the component is added to the CENTER of the BorderLayout by default. Only on component can be added to the CENTER so only the last component is displayed.

如果要将组件显示在图像的顶部,则需要将组件添加到图像.基本代码为:

If you want components to appear on top of the image, then you need to add the components to the image. The basic code would be:

JLabel label = new JLabel( new ImageIcon(...) );
frame.add(label);

label.setLayout(....);
label.add(leaveButton);
label.add(labelWithText);
label.add(stayButton);

我会让您确定所需的确切布局.

I'll let you work out the exact layout that you want.

这篇关于如何将按钮和JPanels添加到ZOrderComponent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆