繁体   English   中英

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

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

我正在尝试制作“石头,剪纸,剪刀”游戏,并且在框架中添加了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!");
        }
    }
}

}

在将所有组件添加到框架AFTER应调用setVisible(true)语句。

当前,您有两个setVisible(...)语句,因此您需要删除第一个。

编辑:

  1. 再看一下代码。 您有多个setVisible(...)语句。 除最后一个以外,全部删除。

  2. 不要为每个按钮创建单独的面板。 而是为所有按钮创建一个面板(称为buttonPanel )。 在您的情况下,您可以使用horizontal BoxLayout 在面板上添加一个按钮,然后添加glue然后添加一个按钮,然后添加glue ,然后添加您的最终按钮。 然后将此buttonPanel添加到框架的北部。 即。 this.add(buttonPanel, BorderLayout.NORTH) 阅读Swing教程中有关如何使用盒式布局的部分,以获得有关布局如何工作以及glue是什么的更多信息。

问题是JFrame具有默认的BorderLayout 当您仅add(component)而不指定BorderLayout.[POSITION]例如add(panel, BorderLayout.SOUTH) ,该组件将被添加到CENTER 问题在于每个POSITION只能有一个组件。 因此,您看到的唯一组件是添加的最后一个组件。

现在,我不知道指定职位后是否会取得理想的结果。 BorderLayout可能不合适。 但是只是为了看到更改,您可以将布局设置为GridLayout(0, 1) ,您将看到组件。

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

如果这不是您想要的结果,则应查看“在容器中布置组件”以了解可用的不同布局。


正如我在评论中指出的那样

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

通过上述操作,您正在尝试将一个对象(最终是一个按钮)与一个字符串进行比较。 相反,您将需要比较actionCommand

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

暂无
暂无

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

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