【问题标题】:How to add buttons and JPanels to a ZOrderComponent?如何将按钮和 JPanel 添加到 ZOrderComponent?
【发布时间】:2014-06-26 15:59:15
【问题描述】:

我正在尝试创建一个带有背景图像的 JFrame,一个位于背景图像上的 JLabel 居中并朝向底部,左右两侧有两个按钮,分别表示“留下”和“离开”。这已经创建好了。问题出现在每个项目的顺序上。我无法在背景图像上显示带有文本和按钮的 JLabel,它们都显示。这是我的代码;任何意见,将不胜感激。先感谢您。

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 jframe jpanel z-order


    【解决方案1】:

    首先遵循 Java 命名约定。变量名不应以大写字符开头。我从未见过像您使用的那样使用变量名的教程、教科书或论坛示例。不要制定自己的约定!

    JFrame 的默认布局管理器是 BorderLayout。当您使用 frame.add(...) 方法时,组件默认添加到 BorderLayout 的CENTER。只有一个组件可以添加到 CENTER,因此只显示最后一个组件。

    如果您希望组件出现在图像顶部,则需要将组件添加到图像中。基本代码是:

    JLabel label = new JLabel( new ImageIcon(...) );
    frame.add(label);
    
    label.setLayout(....);
    label.add(leaveButton);
    label.add(labelWithText);
    label.add(stayButton);
    

    我会让你制定出你想要的确切布局。

    【讨论】:

    • 1.很抱歉,到目前为止,完成和理解一直是我的首要任务。我刚刚开始学习和实现正确的命名convention.2。谢谢你的例子,我会赞成这个,但我还没有获得必要的声誉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多