jframe dy not dy d actionListener出现

发布于 2025-01-22 07:48:26 字数 942 浏览 3 评论 0原文

我是初学者,我已经编写了此代码,但是它似乎不起作用。我已经运行了代码,由于某些原因,jframe没有出现(班级名称在匈牙利语中,不介意)。

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ó();
    }
}

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).

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ó();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2025-01-29 07:48:26

void方法főmásolgató不是类构造函数。

在您的情况下,如果要创建一个főmásolgató的实例,则需要调用由Java定义的默认NO-ARG构造函数,而不是void method főmásolgató

这是您的代码调整的一个示例:

public class főMásolgató implements ActionListener {

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

    // this is still a no-arg constructor, but unlike in your code,
    // this is defined by you, and not automatically 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());
    }
}

The void method főMásolgató is not a class constructor.

In your case, if you want to create an instance of főMásolgató, you need to invoke the default no-arg constructor defined by Java, instead of the void method főMásolgató.

Here is an example with your code tweaked:

public class főMásolgató implements ActionListener {

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

    // this is still a no-arg constructor, but unlike in your code,
    // this is defined by you, and not automatically 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());
    }
}
青丝拂面 2025-01-29 07:48:26

您的错误是

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

函数不是构造函数。

如果您这样做,您的代码将工作:

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

因为他将在这里使用默认构造函数。然后他将打电话给您的功能。
或者,您可以通过更改来修复它:

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

那时

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

您的块是构造函数

提示: 您可以按线路运行。这将使您知道您的块是否运行。

Your mistake is that

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

is a function not a constructor.

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文