1

我想将在文本框中输入的数据添加到表中并保持递增的 ID。单击“添加”按钮时添加数据。

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ex1 extends JFrame {
    GridLayout experimentLayout = new GridLayout(3, 3);
    private javax.swing.JButton jButton1,jButton2,jButton3;
    private javax.swing.JLabel jLabel1, jLabel2, jLabel3;

    private javax.swing.JTextField jTextField1,jTextField2,jTextField3;

    String columns[] = {"ID","Name","Age","Gender"};
    Object data[][] = {
            {"0","Tom",new Integer(20),"Male"},
            {"1","Tina", new Integer(18), "Female"},
            {"2","Raj",new Integer(19),"Male"}

    };

    public void addComponentsToPane(final Container pane) {
        final JPanel compsToExperiment = new JPanel();

        compsToExperiment.setLayout(experimentLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(8, 3));

        jLabel1 = new javax.swing.JLabel("Name: ");
        jLabel2 = new javax.swing.JLabel("Age: ");
        jLabel3 = new javax.swing.JLabel("Gender: ");
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton("Add");
        jButton2 = new javax.swing.JButton("Next");
        jButton3 = new javax.swing.JButton("Per");

        compsToExperiment.add(jLabel1);
        compsToExperiment.add(jLabel2);
        compsToExperiment.add(jLabel3);

        compsToExperiment.add(jTextField1);
        compsToExperiment.add(jTextField2);
        compsToExperiment.add(jTextField3);
        compsToExperiment.add(jButton1);

        JTable table = new JTable(data,columns);
        controls.add(table.getTableHeader());
        controls.add(table);
            pane.add(compsToExperiment, BorderLayout.NORTH);
        pane.add(controls, BorderLayout.CENTER);
    }

    /**
     * Create the GUI and show it. For thread safety, this method is invoked
     * from the event dispatch thread.
     */
    private static void createAndShowGUI() {        
        ex1 frame = new ex1();      
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.addComponentsToPane(frame.getContentPane());
        frame.setSize(500,500);
        //frame.pack();     
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        try {           
            UIManager.setLookAndFeel(
                "javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        // Schedule a job for the event dispatch thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我知道设计到处都是我刚开始学习java的地方,请随时指出可以做出的改进。先感谢您 :)

4

1 回答 1

3

要添加在 中输入的数据,JTextFields您需要ActionListener在添加按钮中注册一个,在这种情况下为jButton1

要将条目添加到表模型中,您可以使用可变模型,例如DefaultTableModel

DefaultTableModel model = new DefaultTableModel(data, columns);

在您的actionPerformed方法中,您ActionListener将拥有:

String nextRowId = Integer.toString(model.getRowCount());
model.addRow(new Object[] { 
              nextRowId, 
              jTextField1.getText(), 
              jTextField2.getText(), 
              jTextField3.getText() });

推荐以下:

  • 为变量使用更有意义的名称。
  • JTable为可能包含大量数据的数据分配更多空间。也放在一个JScrollPane.
于 2012-10-13T18:20:41.737 回答