简体   繁体   English

我希望在按下JButton之后出现JLabel

[英]I want a JLabel to appear after pressing a JButton

I need help with this code, I'm trying to make a simple cookie clicker type game, I have most the code done, but for some reason, when I try to add the JLabel to the frame, it creates an error, I was hoping one of you guys could help me out, I'm fairly new to Java, thanks for the help! 我需要有关此代码的帮助,我正在尝试制作一个简单的Cookie Clicker类型的游戏,大部分代码已完成,但是由于某些原因,当我尝试将JLabel添加到框架时,它会产生错误,希望你们中的一个可以帮助我,我对Java还是很陌生,谢谢您的帮助!

//Variables
static int clicked = 0;
private FlowLayout layout;
private Container container;    

public static void main(String [] args) {

    //Declaring the buttons, panels, etc...
    JButton button = new JButton("Click");
    JPanel panel = new JPanel();
    panel.add(button);

    final JFrame frame = new JFrame("Button Pressed");  
    frame.setSize(400, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(panel);

    //Action Listener Code
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
            clicked++;
            System.out.println("Button pressed " + clicked + " times!");  
        }
    }       
}

The basic principle is relatively easy. 基本原理比较容易。 In order to add something to something else, you first need to have access (or a reference to) the thing you want to add to. 为了向其他内容添加内容,您首先需要访问(或引用)您要添加的内容。

While there are a number of ways you might achieve this, the simplest might be to use an instance/class field. 虽然有多种方法可以实现此目的,但最简单的方法可能是使用实例/类字段。 This field would then be accessible from anywhere within the class, for example 然后可以从班级中的任何地方访问此字段,例如

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClickTest {

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

    private JPanel panel;

    public ClickTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                //Declaring the buttons, panels, etc...
                JButton button = new JButton("Click");

                panel = new JPanel();
                panel.add(button);

                final JFrame frame = new JFrame("Button Pressed");
                frame.setSize(400, 200);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.add(panel);

                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        panel.add(new JLabel("You clicked me"));
                        panel.revalidate();
                    }
                });
            }
        });
    }
}

Take a look at Creating a GUI With JFC/Swing and Understanding Class Members for more details 看一看使用JFC / Swing创建GUI了解类成员了解更多详细信息

You can add an JLabel then update its text when button is clicked. 您可以添加一个JLabel然后在单击按钮时更新其文本。

Note: call JFrame.setVisible(true) in the end when all the component is added. 注意:添加所有组件后,最后调用JFrame.setVisible(true)

sample code: 样例代码:

// Declaring the buttons, panels, etc...
JButton button = new JButton("Click");
final JLabel label = new JLabel();

button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        clicked++;
        label.setText("Button pressed " + clicked + " times!");
    }
});

JPanel panel = new JPanel();
panel.add(button);
panel.add(label);

final JFrame frame = new JFrame("Button Pressed");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);

Find more examples here and here 在这里这里找到更多示例

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

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