简体   繁体   English

为什么在运行此JFrame时我的按钮不出现?

[英]Why won't my buttons appear when I run this JFrame?

I am trying to make a Rock, Paper, Scissors game and I have added 3 buttons to the frame, however when I launch the program two of the buttons don't appear until you hover over them, anyone have any idea why? 我正在尝试制作“石头,剪纸,剪刀”游戏,并且在框架中添加了3个按钮,但是当我启动程序时,只有当您将鼠标悬停在其中时,其中两个按钮才会出现,所以有人知道为什么吗?

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.FlowLayout;
import javax.swing.JOptionPane;
public class RPSFrame extends JFrame {
public static void main(String [] args){
    new RPSFrame();
}
public RPSFrame(){
    JFrame Frame1 = new JFrame();
    this.setSize(500,500);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Rock, Paper or Scissors game");
    this.setLocationRelativeTo(null);
    ClickListener cl1 = new ClickListener();
    ClickListener cl2 = new ClickListener();
    ClickListener cl3 = new ClickListener();

    JPanel panel1 = new JPanel();
    JLabel label1 = new JLabel("Result:");
    panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 25));
    this.add(panel1);
    this.setVisible(false);

    JPanel panel2 = new JPanel();
    JButton Rock = new JButton("Rock");
    Rock.addActionListener(cl1);
    panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
    panel2.add(Rock);
    this.add(panel2);
    this.setVisible(true);
    JPanel panel3 = new JPanel();
    JButton Paper = new JButton("Paper");
    Paper.addActionListener(cl2);
    panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel3.add(Paper);
    this.add(panel3);
    this.setVisible(true);
    JPanel panel4 = new JPanel();
    JButton Scissors = new JButton("Scissors");
    Scissors.addActionListener(cl3);
    panel4.setLayout(new FlowLayout(FlowLayout.RIGHT));
    panel4.add(Scissors);
    this.add(panel4);
    this.setVisible(true);
}
private class ClickListener implements ActionListener{

    public void actionPerformed(ActionEvent e){
        if(e.getSource() == "Rock"){
            int AI = new Random().nextInt(3) + 1;
            JOptionPane.showMessageDialog(null, "I have been clicked!");
        }
    }
}

} }

The setVisible(true) statement should be invoked AFTER all the components have been added to the frame. 在将所有组件添加到框架AFTER应调用setVisible(true)语句。

You currently have two setVisible(...) statements, so you need to get rid of the first one. 当前,您有两个setVisible(...)语句,因此您需要删除第一个。

Edit: 编辑:

  1. Took a second look at the code. 再看一下代码。 You have multiple setVisible(...) statements. 您有多个setVisible(...)语句。 Get rid of them all except for the last one. 除最后一个以外,全部删除。

  2. Don't create separate panels for each button. 不要为每个按钮创建单独的面板。 Instead you create one panel (called buttonPanel ) for all the buttons. 而是为所有按钮创建一个面板(称为buttonPanel )。 In your case you might use a horizontal BoxLayout . 在您的情况下,您可以使用horizontal BoxLayout Add a button to the panel, then add glue then add a button, then add glue and then add your final button. 在面板上添加一个按钮,然后添加glue然后添加一个按钮,然后添加glue ,然后添加您的最终按钮。 Then add this buttonPanel to the NORTH of the frame. 然后将此buttonPanel添加到框架的北部。 ie. 即。 this.add(buttonPanel, BorderLayout.NORTH) . this.add(buttonPanel, BorderLayout.NORTH) Read the section from the Swing tutorial on How to Use Box Layout for more information on how the layout works and on what glue is. 阅读Swing教程中有关如何使用盒式布局的部分,以获得有关布局如何工作以及glue是什么的更多信息。

The problem is that JFrame has a default BorderLayout . 问题是JFrame具有默认的BorderLayout When you just add(component) without specifying a BorderLayout.[POSITION] eg add(panel, BorderLayout.SOUTH) , then the component will get added to the CENTER . 当您仅add(component)而不指定BorderLayout.[POSITION]例如add(panel, BorderLayout.SOUTH) ,该组件将被添加到CENTER The problem with that is each POSITION can only have one component. 问题在于每个POSITION只能有一个组件。 So the only component you see id the last one you add. 因此,您看到的唯一组件是添加的最后一个组件。

Now I don't know after specifying the positions, if you will get your desired result. 现在,我不知道指定职位后是否会取得理想的结果。 A BorderLayout may not be the right fit. BorderLayout可能不合适。 But just to see a change, you can set the layout to GridLayout(0, 1) and you will see the component. 但是只是为了看到更改,您可以将布局设置为GridLayout(0, 1) ,您将看到组件。

this.setLayout(new GridLayout(0, 1));

If this is not the result you want, then you should look over Laying out Components within a Container to learn the different layouts available to you. 如果这不是您想要的结果,则应查看“在容器中布置组件”以了解可用的不同布局。


Also as I pointed out in my comment 正如我在评论中指出的那样

if(e.getSource() == "Rock"){

with the above, you are trying to compare an object (ultimately a button) with a String. 通过上述操作,您正在尝试将一个对象(最终是一个按钮)与一个字符串进行比较。 Instead you will want to compare the actionCommand 相反,您将需要比较actionCommand

String command = e.getActionCommand();
if("Rock".equals(command)){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM