-3

我需要一些帮助来完成我已经设置的项目。我正在用 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);

        }

    }

}

如果有人可以帮助我,那就太好了。

谢谢

4

2 回答 2

0

您应该创建一些变量来保存您输入的信息。

然后使用

table.getModel().table.getModel().setValueAt("ddd", 2, 2);

将其传递到 Jtable 中。

于 2013-01-17T09:50:18.223 回答
0

如果您需要将输入的数据从 传递JOptionPane到 ,JTable那么首先获取数据和 UI 并将其添加到 JTable 中。

如果您将每一行存储为一个对象,则使用该方法

model.addRow(..);

如果您要单独存储每个值,请使用该方法

// value here is your input.
setValueAt(value, rowIndex, columnIndex);
于 2013-01-17T11:18:57.670 回答