gpt4 book ai didi

java - 在java中使用JPanel时,如何将一个类中的用户输入数据显示到另一个类中

转载 作者:行者123 更新时间:2023-12-02 03:10:35 24 4
gpt4 key购买 nike

我在这方面遇到了一些困难。基本上,我希望用户在一个 Java 类中输入名称,并在另一个类中显示该数据。我尝试使用 gets 但我总是得到“null”。另外,如果我想从我的类(class)(示例)中获取一个 int 以在我的第二个类(class)中显示,您会如何执行此操作。基本上是一样的吗?

这是一个例子来说明我的意思

一级

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

public class Exampl extends JFrame implements ActionListener {

JLabel intro = new JLabel("Welcome What is your name?");
JTextField answer = new JTextField(10);
JButton button = new JButton("Enter");
final int WIDTH = 330;
final int HEIGHT = 330;
JLabel greeting = new JLabel("");
JButton next =new JButton("Next");
String name = answer.getText();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Exampl() {
super("Welcome");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
add(intro);
add(answer);
add(button);
add(greeting);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == button) {
String greet = "Hello there" + name;
greeting.setText(greet);
add(next);
next.addActionListener(this);

if(source==next)
dispose();
new Hello();
}

}


public static void main(String[] args) {
Exampl frame= new Exampl();
frame.setVisible(true);

}
}

二等

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hello extends JFrame implements ActionListener {
String name;
JLabel hi = new JLabel("Nice to see you "+name);
JButton button = new JButton("Enter");
final int WIDTH = 330;
final int HEIGHT = 330;
JLabel greeting = new JLabel("");
JButton next =new JButton("Next");

public Hello() {
super("Welcome");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
add(hi);
add(button);
add(greeting);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == button) {
String greet = "example " + name;
greeting.setText(greet);
add(next);
}
}

public static void main(String[] args) {
Exampl frame= new Exampl();
frame.setVisible(true);
}
}

最佳答案

我给你的建议:

  1. 不要扩展 JFrame,而是扩展 JPanel。请参阅:Extends Frame vs creating it inside the programWhy shouldn't you extend JFrame and other components
  2. 不要使用多个JFrame。请参阅The use of multiple JFrames, good / bad practice? (BAD)相反,您可能想尝试使用 Card LayoutJDialog s

What do you mean by extend? do you mean "public class Hello extends Exampl"? I'm new to java so I don't know much.

  • 根据对另一个答案的评论,您可能希望首先在控制台应用程序中学习基础知识,然后再进入 GUI 应用程序,这会增加您的程序的复杂性,从而增加您的学习的复杂性。
  • I'm planning on doing a project where I add the inputted name and a random int into a file so I can store it. Or would it make more sense to add that code into the same class?

  • 根据该注释,您可以创建一个公共(public)方法,该方法以您需要的格式返回 JTextField 中的值,然后从其他类调用该方法,例如:

    public String getUserName() {
    return userNameField.getText();
    }

    public int getDigit() {
    try {
    return Integer.parseInt(digitField.getText());
    } catch (NumberFormatException nfe) {
    nfe.printStackTrace();
    }
    return -1; //In case it's not a number, or return 0, or whatever you need
    }

    //------In another class------//

    int digit = object1.getDigit(); //Where object1 is an instance of the other class where those methods are defined
  • 关于java - 在java中使用JPanel时,如何将一个类中的用户输入数据显示到另一个类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107678/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com