我需要一些帮助来完成我已经设置的项目。我正在用 java 创建一个小型地址簿程序。我已经对用户界面进行了排序,现在我正在努力从 inputDialog 将信息添加到 JTable 中。
这是我的通讯录用户界面。
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;
public class AddressBookGUI {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 100;
private static JTable table;
public static void main(String[] args) {
JFrame frame = new JFrame();
final JFrame addPersonFrame = new JFrame();
frame.setSize(768, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
JButton addButton = new JButton("Add");
addButton.setBounds(10, 228, 89, 23);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showInputDialog("First Name: ");
JOptionPane.showInputDialog("Last Name: ");
JOptionPane.showInputDialog("Address: ");
JOptionPane.showInputDialog("Town: ");
JOptionPane.showInputDialog("County: ");
JOptionPane.showInputDialog("Post Code: ");
JOptionPane.showInputDialog("Phone Number: ");
}
});
frame.getContentPane().setLayout(null);
frame.getContentPane().add(addButton);
JButton editButton = new JButton("Edit");
editButton.setBounds(109, 228, 89, 23);
frame.getContentPane().add(editButton);
JButton delButton = new JButton("Delete");
delButton.setBounds(208, 228, 89, 23);
frame.getContentPane().add(delButton);
// Declare new menuBar and menuItems
JMenuBar mainMenuBar = new JMenuBar();
JMenu fileMenuBar = new javax.swing.JMenu();
JMenuItem newFileMenuBar = new JMenuItem();
JMenuItem openFileMenuBar = new JMenuItem();
JMenuItem saveFileMenuBar = new JMenuItem();
JMenuItem printFileMenuBar = new JMenuItem();
JMenuItem quitFileMenuBar = new JMenuItem();
JMenuItem editMenuBar = new JMenu();
JMenuItem editPersonsEditMenuBar = new JMenuItem();
JMenuItem sortNameEditMenuBar = new JMenuItem();
JMenuItem sortPostcodeEditMenuBar = new JMenuItem();
JMenuItem helpMenuBar = new JMenu();
JMenuItem aboutHelpMenuBar = new JMenuItem();
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 742, 21);
frame.getContentPane().add(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
// Declares and adds items to the menu
JMenuItem menuItemNew = new JMenuItem("New");
mnFile.add(menuItemNew);
JMenuItem menuItemOpen = new JMenuItem("Open");
mnFile.add(menuItemOpen);
JMenuItem menuItemSave = new JMenuItem("Save");
mnFile.add(menuItemSave);
JMenuItem menuItemSaveAs = new JMenuItem("Save As..");
mnFile.add(menuItemSaveAs);
JMenuItem menuItemPrint = new JMenuItem("Print");
mnFile.add(menuItemPrint);
JMenuItem menuItemQuit = new JMenuItem("Quit");
menuItemQuit.addActionListener(new exitApp());
mnFile.add(menuItemQuit);
JMenu menuEdit = new JMenu("Edit");
menuBar.add(menuEdit);
JMenuItem menuItemEditSelectedPerson = new JMenuItem(
"Edit Selected Person");
menuEdit.add(menuItemEditSelectedPerson);
JMenuItem menuItemSortByName = new JMenuItem("Sort By Name");
menuEdit.add(menuItemSortByName);
JMenuItem menuItemSortPostCode = new JMenuItem("Sort By Post Code");
menuEdit.add(menuItemSortPostCode);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 32, 732, 151);
frame.getContentPane().add(scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(new Object[][] {
{ "", null, "", "", "", null, null },
{ null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null }, }, new String[] {
"First Name", "Last Name", "Addesss", "Town", "County",
"Post Code", "Phone Number" }));
scrollPane.setViewportView(table);
frame.setVisible(true);
}
static class exitApp implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
// closes the application
System.exit(0);
}
}
}
如果有人可以帮助我,那就太好了。
谢谢