簡體   English   中英

Java-為什么CardLayout不起作用? 有錯誤

[英]Java - Why isnt CardLayout Working? Having bugs

更新代碼以顯示所有類別

Person類具有設置者和獲取者,因為我最終將有用戶輸入。(目前為硬編碼)這​​是一些創建通訊錄的代碼。 到目前為止,我有4個面板,我想根據所按的按鈕進行切換。 我最初將GridBagLayoutBorderLayout一起使用,但是確定CardLayout是正確的選擇嗎? 我嘗試實現此CardLayout但出現錯誤。

Exception in thread "main": java.lang.NullPointerException
    at JTableSortingPerson.<init>(JTableSortingPerson.java:30)    
    at AddressBook.main(AddressBook.java:100)

第30行只是我將CardLayout設置為MainPanel

第100行是我在主目錄中創建一個類對象以設置可見的布局等。

我也無法調整每個面板的大小,以使其適合JFrame或自動調整其大小。 我是計算機科學的初學者/學生。 任何幫助,將不勝感激。 另外,對不起,這是我第一次使用此網站。

類地址AddressBook

import java.util.*;
import javax.swing.JFrame;

public class AddressBook{

    public static void main(String[] args) {
        JTableSortingPerson p = new JTableSortingPerson();
        p.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        p.setSize(800, 450);
        p.setVisible(true);
    }
}

班級Person

public class Person {
    String FirstName;
    String MiddleName;
    String LastName;
    String StreetAddress;
    int AptNumber;
    String City;
    String State;
    int ZipCode;
    String PhoneNumber; //change back to long after trialList
    String Email;
    String Group;
    String SpecialDate;
    String Comments;

    public Person(String fn, String mn, String ln, String st, int apt, String City, String State, int zip, String phone, String email, String group, String date, String comments){
        FirstName = fn;
        MiddleName = mn;
        LastName = ln;
        StreetAddress = st;
        AptNumber = apt;
        this.City = City;
        this.State = State;
        ZipCode = zip;
        PhoneNumber = phone;
        this.Email = email;
        this.Group = group;
        SpecialDate = date;
        this.Comments = comments;
    }

    public String toString(){
        return "First Name: " + FirstName + 
            " \nMiddle Name: " + MiddleName + 
            " \nLast Name: " + LastName + 
            " \nStreet Address: " + StreetAddress + 
            " \nApt: " + AptNumber + 
            " \nCity: " + City + 
            " \nState: " + State + 
            " \nZip: " + ZipCode + 
            " \nPhone: " + PhoneNumber +
            " \nEmail: " + Email + 
            " \nGroup: " + Group + 
            " \nDates: " + SpecialDate + 
            " \nComments " + "***" + Comments + "***\n\n";
    }
    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        this.FirstName = firstName;
    }
    public String getMiddleName() {
        return MiddleName;
    }
    public void setMiddleName(String middleName) {
        this.MiddleName = middleName;
     }
    public String getLastName() {
        return LastName;
    }
    public void setLastName(String lastName) {
        this.LastName = lastName;
    }
    public String getStreetAddress() {
        return StreetAddress;
    }
    public void setStreetAddress(String streetAddress) {
        this.StreetAddress = streetAddress;
    }
    public int getAptNumber() {
        return AptNumber;
    }
    public void setAptNumber(int aptNumber) {
        this.AptNumber = aptNumber;
    }
    public String getCity() {
        return City;
    }
    public void setCity(String city) {
        this.City = city;
    }
    public String getState() {
        return State;
    }
    public void setState(String state) {
        this.State = state;
    }
    public int getZipCode() {
        return ZipCode;
    }
    public void setZipCode(int zipCode) {
        this.ZipCode = zipCode;
    }
    public String getPhoneNumber() {
        return PhoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.PhoneNumber = phoneNumber;
    }
    public String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        this.Email = email;
    }
    public String getGroup() {
        return Group;
    }
    public void setGroup(String group) {
        this.Group = group;
    }
    public String getSpecialDate() {
        return SpecialDate;
    }
    public void setSpecialDate(String specialDate) {
        this.SpecialDate = specialDate;
    }
    public String getComments() {
        return Comments;
    }
    public void setComments(String comments) {
        this.Comments = comments;
    }

}

PersonTable

import javax.swing.table.AbstractTableModel;
import java.util.*;

public class PersonTable extends AbstractTableModel {
    private static final int COLUMN_FIRST_NAME = 0;
    private static final int COLUMN_LAST_NAME = 1;
    private static final int COLUMN_PHONE_NUMBER = 2;
    private static final int COLUMN_CITY = 3;
    private String[] columnNames = {"First Name", "Last Name", "Phone  Number", "City"};
    private List<Person> listPerson;

    public PersonTable(List<Person> listPerson){
        this.listPerson = listPerson;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return listPerson.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Person person = listPerson.get(rowIndex);
        Object retVal = null;
        switch(columnIndex){
        case COLUMN_FIRST_NAME: retVal = person.getFirstName(); break;
        case COLUMN_LAST_NAME: retVal = person.getLastName(); break;
        case COLUMN_PHONE_NUMBER: retVal = person.getPhoneNumber(); break;
        case COLUMN_CITY: retVal = person.getCity(); break;
        default: throw new IllegalArgumentException("Invalid column index.");
        }
        return retVal;
    }

    @Override 
    public String getColumnName(int columnIndex){
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex){
        if(listPerson.isEmpty()) return Object.class;
        else return getValueAt(0, columnIndex).getClass();
    }
}

PersonTable2

import javax.swing.table.AbstractTableModel;
import java.util.*;

public class PersonTable2 extends AbstractTableModel{
    private static final int COLUMN_FIRST_NAME = 0;
    private static final int COLUMN_MIDDLE_NAME = 1;
    private static final int COLUMN_LAST_NAME = 2;
    private static final int COLUMN_PHONE_NUMBER = 3;
    private static final int COLUMN_CITY = 4;
    private static final int COLUMN_STATE = 5;
    private static final int COLUMN_EMAIL = 6;
    private static final int COLUMN_GROUP = 7;
    private String[] columnNames = {"First Name", "Middle Name", "Last Name", "Phone Number", "City", "State", "Email", "Group"};
    private List<Person> listPerson;

    public PersonTable2(List<Person> listPerson){
        this.listPerson = listPerson;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return listPerson.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Person person = listPerson.get(rowIndex);
        Object retVal = null;
        switch(columnIndex){
        case COLUMN_FIRST_NAME: retVal = person.getFirstName(); break;
        case COLUMN_MIDDLE_NAME: retVal = person.getMiddleName(); break;
        case COLUMN_LAST_NAME: retVal = person.getLastName(); break;
        case COLUMN_PHONE_NUMBER: retVal = person.getPhoneNumber(); break;
        case COLUMN_CITY: retVal = person.getCity(); break;
        case COLUMN_STATE: retVal = person.getState(); break;
        case COLUMN_EMAIL: retVal = person.getEmail(); break;
        case COLUMN_GROUP: retVal = person.getGroup(); break;
        default: throw new IllegalArgumentException("Invalid column index.");
        }
        return retVal;
    }

    @Override 
    public String getColumnName(int columnIndex){
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex){
        if(listPerson.isEmpty()) return Object.class;
        else return getValueAt(0, columnIndex).getClass();
    }
}

JTableSortingPerson

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List; //important
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

@SuppressWarnings("serial")
public class JTableSortingPerson extends JFrame{
    private JPanel mainPanel, panel, panel2, introPanel, newUserPanel, editPanel;
    private JTextArea textArea;
    private JTextArea textArea2;
    private JTextField searchInput, userName, passWord, newUser, newPass, confirmPassword;
    private JTextField searchInput2;
    private JLabel statusBar, user, pass, newUserName, newPassword, newConfirmPassword;
    private JLabel statusBar2;
    private JTable table, table2;
    private JButton DetailButton, SearchButton, AllButton, LogInButton, NewButton, saveButton, cancelButton, addButton, removeButton, editButton;
    private JButton DetailButton2, SearchButton2, goBack, LogInButton2, NewButton2, saveButton2, cancelButton2, addButton2, removeButton2, editButton2;
    private CardLayout cardLayout = new CardLayout();
    List<Person> listPerson = createListPerson();

    public JTableSortingPerson() {
        super();

        mainPanel.setLayout(cardLayout);

        //***********************************************
        //LOG IN PANEL 
        //***********************************************

        introPanel = new JPanel();
        GridBagLayout GridLayout = new GridBagLayout();
        introPanel.setLayout(GridLayout);
        GridBagConstraints c = new GridBagConstraints();
        //add(introPanel, BorderLayout.CENTER);
        mainPanel.add(introPanel, "Log In");

        NewButton = new JButton("New User");
        NewButton.addActionListener(new ButtonListener());
        LogInButton = new JButton("Log In");
        LogInButton.addActionListener(new ButtonListener());
        userName = new JTextField();
        passWord = new JTextField();
        user = new JLabel("Username:");
        pass = new JLabel("Password:");

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        introPanel.add(user, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        introPanel.add(pass, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1000;
        introPanel.add(userName, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1000;
        introPanel.add(passWord, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 1000;
        introPanel.add(LogInButton, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1000;
        introPanel.add(NewButton, c);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        //***********************************************
        //MAIN TABLE PANEL
        //***********************************************

        panel = new JPanel();
        GridBagLayout GridLayout2 = new GridBagLayout();
        panel.setLayout(GridLayout2);
        GridBagConstraints c2 = new GridBagConstraints();

        PersonTable tableModel = new PersonTable(listPerson);
        table = new JTable(tableModel);
        table.setAutoCreateRowSorter(true);

        DetailButton = new JButton("Details");
        DetailButton.addActionListener(new ButtonListener());
        SearchButton = new JButton("Search");
        SearchButton.addActionListener(new ButtonListener());
        AllButton = new JButton("Display All");
        AllButton.addActionListener(new ButtonListener());
        addButton = new JButton("Add Person");
        addButton.addActionListener(new ButtonListener());
        removeButton = new JButton("Remove Person");
        removeButton.addActionListener(new ButtonListener());
        searchInput = new JTextField();
        textArea = new JTextArea();
        statusBar = new JLabel();

        //add(panel, BorderLayout.CENTER);
        mainPanel.add(panel, "Edit Person Table");
        add(statusBar, BorderLayout.SOUTH);

        panel.add(new JScrollPane(table), c2); 
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 1;
        c2.gridwidth = 1000;
        panel.add(searchInput, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 2;
        c2.gridwidth = 1000;
        panel.add(SearchButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 3;
        c2.gridwidth = 1000;
        panel.add(DetailButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 4;
        c2.gridwidth = 1000;
        panel.add(AllButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 5;
        c2.gridwidth = 1000;
        panel.add(addButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 6;
        c2.gridwidth = 1000;
        panel.add(removeButton, c2);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        panel.setVisible(false);

        //***********************************************
        //CREATE NEW USER PANEL
        //***********************************************

        newUserPanel = new JPanel();
        GridBagLayout GridBagLayout3 = new GridBagLayout();
        newUserPanel.setLayout(GridBagLayout3);
        GridBagConstraints c3 = new GridBagConstraints();
        //add(newUserPanel, BorderLayout.CENTER);
        mainPanel.add(newUserPanel, "New User");

        newUser = new JTextField();
        newPass = new JTextField();
        confirmPassword = new JTextField();
        newUserName = new JLabel("New Username:");
        newPassword = new JLabel("New Password:");
        newConfirmPassword = new JLabel("Confirm Password");
        saveButton = new JButton("Save");
        saveButton.addActionListener(new ButtonListener());
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ButtonListener());

        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 1;
        c3.gridwidth = 1000;
        newUserPanel.add(newUserName, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 2;
        c3.gridwidth = 1000;
        newUserPanel.add(newUser, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 3;
        c3.gridwidth = 1000;
        newUserPanel.add(newPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 4;
        c3.gridwidth = 1000;
        newUserPanel.add(newPass, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 5;
        c3.gridwidth = 1000;
        newUserPanel.add(newConfirmPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 6;
        c3.gridwidth = 1000;
        newUserPanel.add(confirmPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 7;
        c3.gridwidth = 1000;
        newUserPanel.add(saveButton, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 8;
        c3.gridwidth = 1000;
        newUserPanel.add(cancelButton, c3);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        newUserPanel.setVisible(false);

        //***********************************************
        //MAIN TABLE PANEL2
        //***********************************************

        panel2 = new JPanel();
        GridBagLayout GridLayout4 = new GridBagLayout();
        panel2.setLayout(GridLayout4);
        GridBagConstraints c4 = new GridBagConstraints();

        PersonTable2 tableModel2 = new PersonTable2(listPerson);
        table2 = new JTable(tableModel2);
        table2.setAutoCreateRowSorter(true);

        DetailButton2 = new JButton("Details");
        DetailButton2.addActionListener(new ButtonListener());
        SearchButton2 = new JButton("Search");
        SearchButton2.addActionListener(new ButtonListener());
        goBack = new JButton("Back");
        goBack.addActionListener(new ButtonListener());
        searchInput2 = new JTextField();
        textArea2 = new JTextArea();
        statusBar2 = new JLabel();

        //add(panel2, BorderLayout.CENTER);
        mainPanel.add(panel2, "All Person Table");
        add(statusBar2, BorderLayout.SOUTH);

        panel2.add(new JScrollPane(table2), c4); 
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 1;
        c4.gridwidth = 1000;
        panel2.add(searchInput2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 2;
        c4.gridwidth = 1000;
        panel2.add(SearchButton2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 3;
        c4.gridwidth = 1000;
        panel2.add(DetailButton2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 4;
        c4.gridwidth = 1000;
        panel2.add(goBack, c4);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        panel2.setVisible(false);

        //*********CARD LAYOUT*********

        //mainPanel.add(introPanel, "Log In");
        //mainPanel.add(newUserPanel, "New User");
        //mainPanel.add(panel, "Edit Person Table");
        //mainPanel.add(panel2, "All Person Table");

        this.setContentPane(mainPanel);

        cardLayout.show(mainPanel, "introPanel");

    }

    public List<Person> createListPerson(){
        ArrayList<Person> retVal = new ArrayList<Person>();
        retVal.add(new Person("Nicolas", "Lawrence", "Soto", "10513 Demilo Pl", 110, "Orlando", "FL", 32836, "3052159446", "nsoto0216@hotmail.com", "Self", "02/16/1991", "I am a student."));
        retVal.add(new Person("Sebastian", "Matias", "Soto", "10513 Demilo Pl", 110, "Orlando", "FL", 32836, "3213559306", "ssoto1005@hotmail.com", "Family", "10/05/1992", "My little brother."));
        retVal.add(new Person("Montserrat", "Soto", "Casco", "12401 nw 185 st", 0, "Miami", "FL", 33161, "3052023390", "msoto0331@gmail.com", "Family", "03/31/1987", "My sister."));
        retVal.add(new Person("Marcelo", "Suarez", "Soto", "4420 nw 16 st", 203, "Miami", "FL", 33055, "3213559355", "chefchileno3@hotmail.com", "Family", "10/28/1955", "My Father."));
        retVal.add(new Person("Olga", "Beatriz", "Munizaga", "4420 nw 16 st", 203, "Miami", "FL", 33055, "3213559356", "omunizaga0326@hotmail.com", "Family", "03/26/1959", "My Mother."));
        retVal.add(new Person("Linda", "Cecilia", "Chavez", "3256 nw 12 st", 0, "Miami", "FL", 33056, "3056234456", "lindizee09@hotmail.com", "Friend", "10/10/1991", "ex girlfriend."));
        retVal.add(new Person("Laura", "Perez", "Carrasco", "500 Pinnacle Cove", 203, "Orlando", "FL", 32078, "4072180248", "lcarrasco0325@hotmail.com", "Friend", "03/25/1991", "ex girlfriend."));
        retVal.add(new Person("Jenn", "", "Ortiz", "2034 Lanstar Blvd", 0, "Orlando", "FL", 32724, "8084735466", "jenn456@gmail.com", "Friend", "07/17/1993", "crazy girl"));
        retVal.add(new Person("Cristian", "", "Camacho", "4556 Americana blvd", 308, "Orlando", "FL", 32856, "4076557890", "chrisc176@hotmail.com", "Friend", "12/02/1989", ""));
        retVal.add(new Person("Gabriela", "", "Valdez", "3004 Watercrest ln", 109, "Orlando", "FL", 32747, "4073066789", "gabbyvaldez@gmail.com", "Friend", "07/15/1992", "Moved to California"));
        retVal.add(new Person("Devin", "", "Delaney", "", 0, "Orlando", "FL", 32836, "4074259556", "ddevin64@hotmail.com", "Coworker", "02/23/1993", "my favorite coworker"));
        return retVal;
    }

    public class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == DetailButton) {
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                int row = table.getSelectedRow();
                if (row == -1) {
                    statusBar.setText("Make a selection");
                    JOptionPane.showMessageDialog(null, "No Selection was made");
                } else {
                    statusBar.setText("Showing details...");
                    row = table.convertRowIndexToModel(table.getSelectedRow()); 
                    JOptionPane.showMessageDialog(null, listPerson.get(row));
                }
            } else if (e.getSource() == SearchButton) {
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                JTextFieldActionPerformed(e);
            } else if (e.getSource() == AllButton) {
                statusBar.setText("Showing all people...");
                /*JTextArea textArea = new JTextArea(listPerson.toString());
                JScrollPane scroll = new JScrollPane(textArea);
                JDialog displayAll = new JDialog();
                displayAll.setVisible(true);
                displayAll.setSize(350, 750);
                displayAll.add(scroll);*/
                statusBar.setText("Done");
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel.invalidate();
                panel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
            } else if (e.getSource() == addButton){

            } else if (e.getSource() == removeButton){
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(null, "No Selection was made");
                } else {
                    row = table.convertRowIndexToModel(table.getSelectedRow()); 
                    listPerson.remove(row);
                    table.addNotify();
                    //JOptionPane.showMessageDialog(null, listPerson.get(row));
                }
            } else if (e.getSource() == DetailButton2) {
                panel.invalidate();
                panel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
                    int row = table2.getSelectedRow();
                    if (row == -1) {
                        statusBar2.setText("Make a selection");
                        JOptionPane.showMessageDialog(null, "No Selection was made");
                    } else {
                        statusBar2.setText("Showing details...");
                        row = table2.convertRowIndexToModel(table2.getSelectedRow()); 
                        JOptionPane.showMessageDialog(null, listPerson.get(row));
                    }
            } else if (e.getSource() == SearchButton2) {
                panel.invalidate();
                panel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
                JTextFieldActionPerformed2(e);
            } else if (e.getSource() == goBack) {
                /*JTextArea textArea = new JTextArea(listPerson.toString());
                JScrollPane scroll = new JScrollPane(textArea);
                JDialog displayAll = new JDialog();
                displayAll.setVisible(true);
                displayAll.setSize(350, 750);
                displayAll.add(scroll);*/
                statusBar2.setText("Done");
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
            } else if (e.getSource() == LogInButton){
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                //pack();
                //JOptionPane.showMessageDialog(null, "You attempted to Log In");
            } else if (e.getSource() == NewButton){
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel.invalidate();
                panel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                newUserPanel.validate();
                newUserPanel.setVisible(true);
                //pack();
                //JOptionPane.showMessageDialog(null, "You attempted to create an account");
            } else if (e.getSource() == saveButton){

            } else if (e.getSource() == cancelButton){
                panel.invalidate();
                panel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.validate();
                introPanel.setVisible(true);
            }
        }
    }

    public void JTextFieldActionPerformed(java.awt.event.ActionEvent evt)
    {                                            
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(((PersonTable) table.getModel())); 
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchInput.getText())); //"(?i)" for case sensitivity
        table.setRowSorter(sorter);
    } 

    public void JTextFieldActionPerformed2(java.awt.event.ActionEvent evt)
    {                                            
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(((PersonTable2) table2.getModel())); 
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchInput2.getText())); //"(?i)" for case sensitivity
        table2.setRowSorter(sorter);
    } 

    public void newUser(){
        /*try {

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    }

    public void LogIn(String user, String pass){

    }

}

我想我不會是唯一不想停止幫助您的人,對此:P

您的代碼太瘋狂了,您顯然需要提高Java技能。 我很確定,經過良好的代碼清潔,您的錯誤將神奇地消失了!

因此,為了給您一些幫助(對此感到抱歉,但不是您問題的直接解決方案),我建議您一些構想來重構代碼:

避免使用具有超過大約10個屬性的類; 使用更多的類來表示您的數據!

其次,嘗試使用Enums (這很簡單,您會看到)來代替這種事情:

private static final int COLUMN_FIRST_NAME = 0;
private static final int COLUMN_LAST_NAME = 1;
private static final int COLUMN_PHONE_NUMBER = 2;
private static final int COLUMN_CITY = 3;

最后,我建議您將JTableSortingPerson分解為許多JTableSortingPerson件,而不是將所有基本塊都放在同一位置。 這就像直接用沙子建造塔樓,而不是使用由沙子制成的磚頭。

我可以說很多事情來改進您的代碼,但是我會首先煩擾您,並使您為我將給您的所有技術問題瘋狂!

重構好運,這是我認為的關鍵!

我忘記了“ mainPanel = new JPanel();”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM