我目前正在研究一个需要接受用户输入并将其添加到arrayList内的对象的JFrame。问题是,使用JButton制作JFrame非常简单,但是当我按下其中一个按钮打开JTextField时,它只有一个空白的JFrame,它不接受任何用户输入。
这是我的代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SwingObserver {
Scanner scan = new Scanner(System.in);
JFrame frame;
//ArrayList<Customer>cust = new ArrayList<Customer>();
ArrayList<Magazine>natGeo = new ArrayList<Magazine>();
public static void main(String[]args){
SwingObserver example = new SwingObserver();
example.go();
}
public void go(){
frame = new JFrame("Subscriber Hub");
frame.setVisible(true);
JButton button1 = new JButton("Add Subscriber");
JButton button2 = new JButton("Delete Subscriber");
JButton button3 = new JButton("Send message");
button1.addActionListener(new addListener());
//button2.addActionListener(new removeListener());
//button3.addActionListener(new messageListener());
frame.getContentPane().add(BorderLayout.WEST, button1);
frame.getContentPane().add(BorderLayout.CENTER, button2);
frame.getContentPane().add(BorderLayout.EAST, button3);
frame.setSize(500, 500);
}
class addListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
frame.setVisible(false); //sets main menu frame to invisible
Magazine x = new Magazine();
JFrame frameAdd = new JFrame("Add Subscriber");
JTextField text = new JTextField(15);
frameAdd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameAdd.add(text);
frameAdd.setSize(500,500);
frameAdd.setVisible(true);
String address = text.getText();
x.setAddress(address);
natGeo.add(x);
System.out.println(x.getAddress());
//frame.setVisible(true);
}
}
}
请让我知道我做错了什么,我已经被困了很长一段时间。谢谢