我正在尝试创建一个学生注册程序,其中用户在JTextField中的JFrame中输入数据,并将该数据存储到另一个类中的变量中。 包裹注册;
import java.util.*;
/**
*
* @author Frank
*/
public class AcgRegistration {
public static void main(String[] args) {
memberDialogBox memberDialogBox = new memberDialogBox();
}
}
package acgregistration;
/**
*
* @author Frank
*/
class acgMember {
private String name;
private int num;
private String email;
public acgMember(String name, int number, String email) {
this.name = name;
this.num = number;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package acgregistration;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author Frank
*/
public class memberDialogBox {
String options[] = {"Student","Faculty/Staff"};
JComboBox choices = new JComboBox(options);
JButton b = new JButton("Confirm");
JLabel l = new JLabel("Select your ACG Status");
public memberDialogBox(){
frame();
}
public void frame(){
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(210,150);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel p = new JPanel();
p.add(choices);
p.add(b);
p.add(l);
f.add(p);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s = choices.getSelectedItem().toString();
if ("Student".equals(choices.getSelectedItem())){
studentDialogBox student = new studentDialogBox();
//This code gives me an error code saying I should call
//acgMemberModel
}
else{
facultyDialogBox faculty= new facultyDialogBox();
}
f.dispose();
}
});
}
}
package acgregistration;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class studentDialogBox {
private JTextField nameField = new JTextField("", 20);
private JTextField emailField = new JTextField("", 20);
private JTextField numberField = new JTextField("", 20);
private JButton confirmButton = new JButton("Confirm");
private acgMemberModel model;
public studentDialogBox(acgMemberModel model) {
this.model = model;
frame();
}
public void frame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(nameField);
panel.add(emailField);
panel.add(numberField);
panel.add(confirmButton);
frame.add(panel);
confirmButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String number = numberField.getText();
String email = emailField.getText();
acgMember member = new acgMember(name,
Integer.valueOf(number), email);
model.addNew(member);
}
});
}
}
class acgMemberModel {
private List<acgMember> members = new ArrayList<>();
public void addNew(acgMember member) {
members.add(member);
}
public List<acgMember> getMembers() {
return Collections.unmodifiableList(members);
}
}
我基本上是尝试对所有文本字段执行此操作,然后将其保存到ArrayList或Hashmap(基本上是最终结果)。我唯一的问题是,如何将文本字段输入从一个类存储到另一个类? 任何帮助将非常感谢!谢谢!
答案 0 :(得分:0)
每次填充字段时都只需创建成员的新实例,并且在调用此操作侦听器的结果时按下按钮,您将从文本字段中获取所有数据并将其传递给新的实例构造函数。每次创建新成员时都会将其传递给MemberModel单独的类。
P.S。你需要阅读一些关于java语言命名约定的内容,特别是有关变量的内容,你错误地将动作监听器设置为TextField而不是Button,因为你的变量名称完全没有意义。我重构代码以将所有变量名称更改为人类可读的形式并修复我的解决方案中的错误,因为所有这些约定都没有被使用。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
MemberModel model = new MemberModel();
StudentsToOutputListener outputListener
= new StudentsToOutputListener(model, new FileOutput(new File("path to your default file")));
Window studentDialog = new StudentDialogBox(model);
Window facilityDialog = new FacultyDialogBox();
Window memberDialog = new MemberDialogBox(studentDialog, facilityDialog);
memberDialog.show();
}
}
class Member {
private String name;
private int number;
private String email;
public Member(String name, int number, String email) {
this.name = name;
this.number = number;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Member{" +
"name='" + name + '\'' +
", number=" + number +
", email='" + email + '\'' +
'}';
}
}
interface Window {
void show();
}
interface Output {
void output(List<Member> members);
}
interface Listener {
void update();
}
class MemberDialogBox implements Window {
private JFrame frame = new JFrame();
private JComboBox<Window> choiceComboBox = new JComboBox<>();
private JButton confirmButton = new JButton("Confirm");
private JLabel selectLabel = new JLabel("Select your ACG Status");
public MemberDialogBox(Window... windows) {
for (Window window : windows) {
choiceComboBox.addItem(window);
}
frame();
}
public void show() {
frame.setVisible(true);
}
public void frame() {
frame = new JFrame();
frame.setSize(210, 150);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(choiceComboBox);
panel.add(confirmButton);
panel.add(selectLabel);
frame.add(panel);
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window window = (Window) choiceComboBox.getSelectedItem();
window.show();
frame.dispose();
}
});
}
}
class StudentDialogBox implements Window, Listener, Output {
private JTextField nameField = new JTextField("", 20);
private JTextField emailField = new JTextField("", 20);
private JTextField numberField = new JTextField("", 20);
private JButton confirmButton = new JButton("Confirm");
private JButton saveButton = new JButton("Save students to file");
private JFrame frame;
private JList<Member> list = new JList<>();
private MemberModel model;
public StudentDialogBox(MemberModel model) {
this.model = model;
model.addListener(this);
frame();
}
public void show() {
frame.setVisible(true);
}
public void output(List<Member> members) {
list.setListData(members.toArray(new Member[]{}));
}
public void update() {
model.outputStudentsTo(this);
}
public void frame() {
frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(nameField);
panel.add(emailField);
panel.add(numberField);
panel.add(confirmButton);
panel.add(list);
panel.add(saveButton);
frame.add(panel);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showSaveDialog(frame);
File selectedFile = fileChooser.getSelectedFile();
Output output = new FileOutput(selectedFile);
model.outputStudentsTo(output);
}
});
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String number = numberField.getText();
String email = emailField.getText();
Member member = new Member(name, Integer.valueOf(number), email);
model.addNewStudent(member);
}
});
}
public String toString() {
return "Student";
}
}
class FacultyDialogBox implements Window {
public void show() {
System.out.println("you need to implement FacultyDialogBox " +
"in similar way than StudentDialog box");
}
public String toString() {
return "Faculty/Stuff";
}
}
class MemberModel {
private List<Member> students = new ArrayList<>();
private List<Member> stuff = new ArrayList<>();
private List<Listener> listeners = new ArrayList<>();
public void addListener(Listener listener) {
listeners.add(listener);
}
private void notifyListeners() {
for (Listener listener : listeners) {
listener.update();
}
}
public void addNewStudent(Member member) {
students.add(member);
notifyListeners();
}
public void addNewStuff(Member member) {
stuff.add(member);
notifyListeners();
}
public void outputStudentsTo(Output output) {
output.output(Collections.unmodifiableList(students));
}
public void outputStuffTo(Output output) {
output.output(Collections.unmodifiableList(stuff));
}
}
class FileOutput implements Output {
private final File destination;
public FileOutput(File destination) {
this.destination = destination;
}
public void output(List<Member> members) {
try (BufferedWriter file = new BufferedWriter(new FileWriter(destination))) {
for (Member member : members) {
file.write(member.toString());
file.write("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
class StudentsToOutputListener implements Listener {
private final MemberModel model;
private final Output output;
public StudentsToOutputListener(MemberModel model, Output output) {
this.model = model;
this.output = output;
model.addListener(this);
}
public void update() {
model.outputStudentsTo(output);
}
}
我更新了你的问题澄清答案,我明白你想要实现什么并修复你的程序,我也重构这个代码并使其更具有oop风格和更多可读性,但你需要自己实现第二个对话框Facility以类似于学生对话框的方式。您还需要将此源中的每个类放在不同的* .java文件中。
答案 1 :(得分:0)
你在谈论数据绑定。我最近发布了一个演示程序,演示了如何解决此问题所需的内容:Switching JPanels moves content diagonally
基本上,GUI通过事件(通常是Property Change Events)将信息传递给模型,反之亦然,但是您可以选择很多选项。这个过程看起来像这样:
类似的方法可以允许模型将属性更改请求发送到GUI以更新字段的值。
使用这些事件的好处在于它使GUI保持简单,将模型与表示分开,并且更容易用另一种技术轻松替换/复制GUI。
答案 2 :(得分:-2)
您是否尝试过要在类public static
之间共享的变量?