java - 对 JButton 进行编程以在单击时创建对象

标签 java user-interface jbutton

编辑:我希望此窗口能够创建多个用户配置文件,因此它需要创建比 u1 更多的变量。该程序的想法是模仿社交网络。但是,我不知道该怎么做。谢谢!

我是 Java GUI 新手。到目前为止,我已经创建了一个窗口,其中包含四个文本字段:名字、姓氏、性别和家乡。我现在想创建一个按钮,它接受在这 4 个文本字段中输入的字符串,并使用此构造函数从中创建一个对象:

//object constructor
public UserProfile(String fn, String ln, String g, String h) {
    firstName = fn;
    lastName = ln;
    fullName = fn + ' ' + ln;
    gender = g;
    homeTown = h;
}

这是到目前为止我的 CreateProfile 类。按钮的相关代码位于底部,我很确定它是错误的/效率低下的。

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

/* Window for creating a UserProfile */
public class CreateProfile extends JFrame {

    private JLabel jl1;
    public JTextField tf1;

    private JLabel jl2;
    public JTextField tf2;

    private JLabel jl3;
    public JTextField tf3;

    private JLabel jl4;
    public JTextField tf4;

    public JButton jb;

    public CreateProfile() {   // constructor for the window
        super("Create Your FaceSpace Profile");
        setLayout(new FlowLayout());

        jl1 = new JLabel("First name: ");
        tf1 = new JTextField(null, 10);
        add(jl1);add(tf1);

        jl2 = new JLabel("Last name: ");
        tf2 = new JTextField(null, 10);
        add(jl2);add(tf2);

        jl3 = new JLabel("Gender (M/F): "); // make drop down
        tf3 = new JTextField(null, 10);
        add(jl3);add(tf3);

        jl4 = new JLabel("Hometown: ");
        tf4 = new JTextField(null, 10);
        add(jl4);add(tf4);

        /* Button that creates a UserProfile object given the four text fields */
        jb = new JButton("Create Profile!");
        add(jb);
        jb.addActionListener(this);

    }

    private class buttonEvent implements ActionListener {

        public void action(ActionEvent e) {
            String command = e.getActionCommand();
            if(command.equals("jb")) {
                // Code in question
                String fn = tf1.getText().trim();
                String ln = tf2.getText().trim();
                String g = tf3.getText().trim();
                String ht = tf4.getText().trim();
                UserProfile u1 = new UserProfile(fn,ln,g,ht);
            }
        }

    }

}

最佳答案

首先,您的 CreateProfile 类没有实现 ActionListener,因此您无法使用

jb.addActionListener(this);

您必须将新对象设为实现 ActionListener 的私有(private)类,例如:

jb.addActionListener(new buttonEvent());

此外,buttonEvent 类应该实现 actionPerformed 而不是 action 方法,并且 getActionCommand() 返回按钮的标题而不是“jb”。

你的buttonEvent应该是这样的:

private class buttonEvent implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("Create Profile!")) {
          String fn = tf1.getText().trim();
          String ln = tf2.getText().trim();
          String g = tf3.getText().trim();
          String ht = tf4.getText().trim();
          UserProfile u1 = new UserProfile(fn,ln,g,ht);
        }
    }

}

并且不要忘记在 CreateProfile 构造函数中使用 setVisible(true) 显示窗口。

编辑: 如果您想拥有多个用户配置文件,您可以创建列表并向该列表添加新对象。 例如在 CreateProfile 类中添加:

List<UserProfile> users = new ArrayList<UserProfile>();

并添加新用户

users.add(new UserProfile(fn,ln,g,ht));

而不是 UserProfile u1 = new UserProfile(fn,ln,g,ht);在按钮事件中。

关于java - 对 JButton 进行编程以在单击时创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27571996/

相关文章:

Android GUI 在运行时更新

java - 如何使按钮在不同的点击量上发生变化java

java - 数字聊天登录按钮的边框类型是什么?

java - Eclipse 动态 Web 项目中缺少 WEB-INF 文件

java - 从多模块项目创建 uber-jar

user-interface - 标准 ML 的 GUI?

asp.net - 开发短期的基于 Web 的数据输入 UI

java - 大型浮点文字的舍入模式是否明确规定?

java - Eclipse 中运行时目标的标准 ServletContainer

java - 无背景的 JButton