简体   繁体   English

JFrame 没有出现在 ActionListener 中

[英]JFrame doesn't appear with ActionListener

I'm a beginner and I've written this code but it does not seem to work.我是初学者,我已经编写了这段代码,但它似乎不起作用。 I've run the code and for some reasons the JFrame does not appear (the class name is in Hungarian, do not mind it).我运行了代码,由于某些原因, JFrame没有出现(class 名称是匈牙利语,请不要介意)。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class főMásolgató implements ActionListener {

    JButton b;
    JLabel label;
    JTextField tfield;
    JFrame frame;

    public void főMásolgató(){
        frame = new JFrame();
        b = new JButton("Másolás");
        label = new JLabel("");
        tfield = new JTextField();
        frame.add(b);
        frame.add(label);
        frame.add(tfield);
        b.addActionListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        label.setText(tfield.getText());
    }
}

public class másolgatóHasználata {
    public static void main(String args[]){
        new főMásolgató();
    }
}

The void method főMásolgató is not the class constructor. void 方法 főMásolgató 不是 class 构造函数。

When you instance a new főMásolgató you're simply invoking the default no-args constructor instead of the void method főMásolgató where you show your JFrame .当您实例化一个新的 főMásolgató 时,您只是调用默认的无参数构造函数,而不是调用显示 JFrame 的无效方法JFrame

You should re-write it like this:你应该像这样重写它:

public class főMásolgató implements ActionListener {

    JButton b;
    JLabel label;
    JTextField tfield;
    JFrame frame;

    //This is now YOUR no-args constructor not the default one provided by Java
    public főMásolgató(){
        frame = new JFrame();
        b = new JButton("Másolás");
        label = new JLabel("");
        tfield = new JTextField();
        frame.add(b);
        frame.add(label);
        frame.add(tfield);
        b.addActionListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        label.setText(tfield.getText());
    }
}

Your mistake is that你的错误是

public void főMásolgató(){ ... }

is a function not a constructor.function不是构造函数。

Your code will work if you do:如果您这样做,您的代码将起作用:

public class másolgatóHasználata {
  public static void main(String args[]){
    new főMásolgató().főMásolgató();
  }}

because here he will use the default constructor.因为这里他会使用默认的构造函数。 then he will call your function. Or you can fix it by changing:然后他会打电话给你的 function。或者你可以通过更改来修复它:

public void főMásolgató(){ ... }

to

public főMásolgató(){ ... }

Then your block is the constructor .那么你的块就是构造函数

Hint: you can debug this by running line by line.提示:您可以通过逐行运行来调试它。 This will let you know if your block was run or not.这会让您知道您的区块是否运行。

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

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