1

所以我有一个需要帮助的学校项目!

所以我正在做一个“工作组日程安排”应用程序作为我今年(南非 11 年级)的主要项目,但我被困住了。我有一个登录系统,可以很好地登录数据库,然后我有一个名为“签到站”的类CheckIn,它有一个按钮,应该打开一个表格,显示当时在给定站工作的所有人员,如果他们签到的话。

到目前为止,该表仅从站内每个人的数据库中提取数据,无论他们是否在这里,但这不是问题。

问题是,如果我开始上课Welcome(名为“欢迎”的电台),它运行良好,显示以下图像:当我单独开始欢迎课程时的表

但是当我尝试welcome从程序中打开课程时(所以我点击课程中的快速链接CheckIn,它会这样做: 当我从另一个班级打开时欢迎班级

Welcome课程的代码运行良好,我是通过从 youtube 复制的教程获得的,但它可以 100% 运行。

有什么我做错了吗???

任何建议将不胜感激。

谢谢乔什

顺便说一下,这是代码:P:CheckIn 类:

package co.za.gecko.inked.crm;

import java.awt.EventQueue;

public class CheckIn extends JFrame {
String u = (String) Login.cbxUsername.getSelectedItem();
static ArrayList<String> firstNames = new ArrayList<>();
JLabel lblHello = new JLabel("Hello, "+u.toString());
JLabel lblClock = new JLabel("");
static JComboBox cbxCIFirstName = new JComboBox();
static JComboBox cbxCILastName = new JComboBox();

// to add checked in people to pCI stands for personCheckedIn
static ArrayList<String> pCI = new ArrayList<>();

// all jLabels for the stations
JLabel lblWelcome = new JLabel("Welcome: 0");
JLabel lblScanning = new JLabel("Scanning: 0");
JLabel lblChecking = new JLabel("Checking: 0");
JLabel lblRunners = new JLabel("Runners: 0");
JLabel lblWrapping = new JLabel("Wrapping: 0");
JLabel lblFirstAid = new JLabel("First Aid: 0");
JLabel lblVolunteers = new JLabel("Volunteers: 0");

// all counters for the stations
int cWelcome = 0;
int cScanning = 0;
int cChecking = 0;
int cRunners = 0;
int cWrapping = 0;
int cFirstAid = 0;
int cVolunteers = 0;

// all stations with volunteer id in station
static ArrayList<String> inWelcomeStation = new ArrayList<>();
ArrayList<String> inScanningStation = new ArrayList<>();
ArrayList<String> inCheckingStation = new ArrayList<>();
ArrayList<String> inRunnersStation = new ArrayList<>();
ArrayList<String> inWrappingStation = new ArrayList<>();
ArrayList<String> inFirstAidStation = new ArrayList<>();
ArrayList<String> inVolunteersStation = new ArrayList<>();

JLabel lblCIStation = new JLabel("Welcome");
JLabel lblCITime = new JLabel("10:00");

/**
 * Launch the application.
 */
public static void main(String[] args) throws Exception{

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CheckIn frame = new CheckIn();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 * @throws Exception 
 */
public CheckIn() throws Exception{

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1024, 768);
    getContentPane().setLayout(null);


    lblHello.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblHello.setBounds(10, 11, 182, 25);
    getContentPane().add(lblHello);


    lblClock.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblClock.setBounds(941, 11, 57, 25);
    getContentPane().add(lblClock);

    JLabel lblDay = new JLabel("Day 1");
    lblDay.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblDay.setBounds(863, 11, 57, 25);
    getContentPane().add(lblDay);
    cbxCIFirstName.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                cbxCILastName.removeAllItems();
                getLastNames();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    cbxCIFirstName.addInputMethodListener(new InputMethodListener() {
        public void caretPositionChanged(InputMethodEvent arg0) {
        }
        public void inputMethodTextChanged(InputMethodEvent arg0) {

        }
    });
    cbxCIFirstName.setEditable(true);


    cbxCIFirstName.setBounds(307, 106, 330, 20);
    getContentPane().add(cbxCIFirstName);
    cbxCILastName.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                updateCheckInData();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });


    cbxCILastName.setBounds(307, 163, 330, 20);
    getContentPane().add(cbxCILastName);

    JLabel lblFirstName = new JLabel("First Name");
    lblFirstName.setBounds(307, 93, 73, 14);
    getContentPane().add(lblFirstName);

    JLabel lblLastName = new JLabel("Last Name");
    lblLastName.setBounds(307, 149, 73, 14);
    getContentPane().add(lblLastName);

    JLabel lblStation = new JLabel("Station");
    lblStation.setBounds(647, 93, 73, 14);
    getContentPane().add(lblStation);

    JLabel lblTime = new JLabel("Time");
    lblTime.setBounds(647, 149, 73, 14);
    getContentPane().add(lblTime);


    lblCIStation.setFont(new Font("Tahoma", Font.PLAIN, 15));
    lblCIStation.setBounds(647, 102, 136, 25);
    getContentPane().add(lblCIStation);

    lblCITime.setFont(new Font("Tahoma", Font.PLAIN, 15));
    lblCITime.setBounds(647, 159, 136, 25);
    getContentPane().add(lblCITime);

    JButton btnCheckIn = new JButton("Check In");
    btnCheckIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                checkedIn();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    btnCheckIn.setBounds(759, 117, 110, 55);
    getContentPane().add(btnCheckIn);

    JSeparator separator = new JSeparator();
    separator.setBounds(52, 220, 900, 2);
    getContentPane().add(separator);


    lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblWelcome.setBounds(85, 254, 161, 25);
    getContentPane().add(lblWelcome);


    lblScanning.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblScanning.setBounds(85, 290, 161, 25);
    getContentPane().add(lblScanning);


    lblChecking.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblChecking.setBounds(85, 326, 161, 25);
    getContentPane().add(lblChecking);


    lblRunners.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblRunners.setBounds(452, 326, 161, 25);
    getContentPane().add(lblRunners);


    lblWrapping.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblWrapping.setBounds(452, 290, 161, 25);
    getContentPane().add(lblWrapping);

    lblFirstAid.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblFirstAid.setBounds(452, 254, 161, 25);
    getContentPane().add(lblFirstAid);

    lblVolunteers.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblVolunteers.setBounds(724, 254, 161, 25);
    getContentPane().add(lblVolunteers);

    JSeparator separator_1 = new JSeparator();
    separator_1.setBounds(52, 383, 900, 2);
    getContentPane().add(separator_1);

    JLabel lblQuickLinks = new JLabel("Quick Links");
    lblQuickLinks.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblQuickLinks.setBounds(85, 413, 161, 25);
    getContentPane().add(lblQuickLinks);

    JButton btnOster = new JButton("Roster");
    btnOster.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnOster.setBounds(85, 451, 116, 55);
    getContentPane().add(btnOster);

    JButton btnVolunteerInfo = new JButton("Volunteer Info");
    btnVolunteerInfo.setBounds(211, 451, 116, 55);
    getContentPane().add(btnVolunteerInfo);

    JButton btnSearch = new JButton("Search");
    btnSearch.setBounds(337, 451, 116, 55);
    getContentPane().add(btnSearch);

    JButton btnWelcome = new JButton("Welcome");
    btnWelcome.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnWelcome.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            checkStation("Welcome");
                            Welcome openWelcome = new Welcome();
                            openWelcome.setVisible(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        }
    });
    btnWelcome.setBounds(85, 526, 116, 55);
    getContentPane().add(btnWelcome);

    JButton btnScanning = new JButton("Scanning");
    btnScanning.setBounds(211, 526, 116, 55);
    getContentPane().add(btnScanning);

    JButton btnChecking = new JButton("Checking");
    btnChecking.setBounds(337, 526, 116, 55);
    getContentPane().add(btnChecking);

    JButton btnFirstAid = new JButton("First Aid");
    btnFirstAid.setBounds(463, 526, 116, 55);
    getContentPane().add(btnFirstAid);

    JButton btnWrapping = new JButton("Wrapping");
    btnWrapping.setBounds(589, 526, 116, 55);
    getContentPane().add(btnWrapping);

    JButton btnRunners = new JButton("Runners");
    btnRunners.setBounds(715, 526, 116, 55);
    getContentPane().add(btnRunners);

    JButton btnVolunteers = new JButton("Volunteers");
    btnVolunteers.setBounds(842, 526, 116, 55);
    getContentPane().add(btnVolunteers);

    JButton btnCheckOut = new JButton("Check Out");
    btnCheckOut.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            try {
                checkOut();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    btnCheckOut.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent arg0) {
            try {
                checkOut();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    btnCheckOut.setBounds(124, 117, 110, 55);
    getContentPane().add(btnCheckOut);

    getFirstNames();

}

public static void getFirstNames() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `first_name` FROM volunteers");
    ResultSet result = statement.executeQuery();
    cbxCIFirstName.setToolTipText("Select a first name");
    cbxCIFirstName.setEditable(true);
    cbxCIFirstName.addItem("Please select a first name");

    while(result.next()){
        if(firstNames.contains(result.getString(1)) != true){
            firstNames.add(result.getString(1));
        }
    }

    for(int i=0; i<firstNames.size(); i++){
        cbxCIFirstName.addItem(firstNames.get(i));
    }
}

public static void getLastNames() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `last_name` FROM volunteers WHERE `first_name` = '" + cbxCIFirstName.getSelectedItem() + "'");
    ResultSet result = statement.executeQuery();
    cbxCILastName.setToolTipText("Select a last name");
    cbxCILastName.setEditable(true);

    int rowcount = 0;
    if (result.last()) {
      rowcount = result.getRow();
      result.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
    }
    if(rowcount > 1){
        cbxCILastName.addItem("Please select a last name");
    }
    while(result.next()){
        cbxCILastName.addItem(result.getString(1));
    }
}

public void checkedIn() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `volunteer_id` FROM volunteers WHERE `first_name` = '" + cbxCIFirstName.getSelectedItem() + "' AND `last_name` = '" + cbxCILastName.getSelectedItem() + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        // checks if volunteer id is already in the array, ie: already checked in
        if(pCI.contains(result.getString(1))){
            JOptionPane.showMessageDialog(null, "Sorry, this volunteer is already logged in!");
        } else {
            // add volunteer id to array list
            pCI.add(result.getString(1));
            System.out.println(pCI);
            addToLabel(result.getString(1));
            getStationInfo(result.getString(1));
        }
    }   
}

public void addToLabel(String user_id) throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `station` FROM volunteers WHERE `volunteer_id` = '" + user_id + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        String stat = result.getString(1);

        switch (stat) {
        case "welcome":
        case "Welcome":
            cWelcome++;
            lblWelcome.setText("Welcome: " + cWelcome);
            break;
        case "scanning":
        case "Scanning":
            cScanning++;
            lblScanning.setText("Scanning: " + cScanning);
            break;
        case "checking":
        case "Checking":
            cChecking++;
            lblChecking.setText("Checking: " + cChecking);
            break;
        case "runners":
        case "Runners":
            cRunners++;
            lblRunners.setText("Runners: " + cRunners);
            break;
        case "wrapping":
        case "Wrapping":
            cWrapping++;
            lblWrapping.setText("Wrapping: " + cWrapping);
            break;
        case "firstaid":
        case "Firstaid":
        case "firstAid":
        case "FirstAid":
            cFirstAid++;
            lblFirstAid.setText("First Aid: " + cFirstAid);
            break;
        case "volunteers":
        case "Volunteers":
            cVolunteers++;
            lblVolunteers.setText("Volunteers: " + cVolunteers);
            break;
        default:
            break;
        }
    }
}

public void updateCheckInData() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `station`, `time` FROM volunteers WHERE `first_name` = '" + cbxCIFirstName.getSelectedItem() + "' AND `last_name` = '" + cbxCILastName.getSelectedItem() + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        lblCIStation.setText(result.getString(1));
        String station = lblCIStation.getText();
        station = station.substring(0, 1).toUpperCase() + station.substring(1);
        lblCIStation.setText(station);
        lblCITime.setText(result.getString(2));
    }
}

public void checkOut() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `volunteer_id` FROM volunteers WHERE `first_name` = '" + cbxCIFirstName.getSelectedItem() + "' AND `last_name` = '" + cbxCILastName.getSelectedItem() + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        pCI.remove(result.getString(1));
        System.out.println(pCI);
        removeFromLabel(result.getString(1));
    }
}

public void removeFromLabel(String user_id) throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `station` FROM volunteers WHERE `volunteer_id` = '" + user_id + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        String stat = result.getString(1);

        switch (stat) {
        case "welcome":
        case "Welcome":
            cWelcome--;
            lblWelcome.setText("Welcome: " + cWelcome);
            break;
        case "scanning":
        case "Scanning":
            cScanning--;
            lblScanning.setText("Scanning: " + cScanning);
            break;
        case "checking":
        case "Checking":
            cChecking--;
            lblChecking.setText("Checking: " + cChecking);
            break;
        case "runners":
        case "Runners":
            cRunners--;
            lblRunners.setText("Runners: " + cRunners);
            break;
        case "wrapping":
        case "Wrapping":
            cWrapping--;
            lblWrapping.setText("Wrapping: " + cWrapping);
            break;
        case "firstaid":
        case "Firstaid":
        case "firstAid":
        case "FirstAid":
            cFirstAid--;
            lblFirstAid.setText("First Aid: " + cFirstAid);
            break;
        case "volunteers":
        case "Volunteers":
            cVolunteers--;
            lblVolunteers.setText("Volunteers: " + cVolunteers);
            break;
        default:
            break;
        }
    }
}

public void checkStation(String station) throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `volunteer_id` FROM volunteers WHERE `station` = '" + station + "'");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        switch (station) {
        case "Welcome":
        case "welcome":

            break;
        case "Scanning":
        case "scanning":

            break;
        case "Checking":
        case "checking":

            break;
        case "Runners":
        case "runners":

            break;
        case "Wrapping":
        case "wrapping":

            break;
        case "FirstAid":
        case "Firstaid":
        case "firstAid":
        case "firstaid":

            break;
        case "Volunteers":
        case "volunteers":

            break;

        default:
            break;
        }
    }
}

public void getStationInfo(String id) throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `first_name`, `last_name`, `time`, `station` FROM volunteers WHERE `volunteer_id` = '" + id + "'");
    ResultSet result = statement.executeQuery();

    String vid = id;
    List vidd = new ArrayList<>(Arrays.asList(vid));

    while(result.next()){
        switch (result.getString(4)) {
        case "Welcome":
        case "welcome":
            inWelcomeStation = (ArrayList<String>) vidd;
            break;
        case "Scanning":
        case "scanning":
            inScanningStation = (ArrayList<String>) vidd;
            break;
        case "Checking":
        case "checking":
            inCheckingStation = (ArrayList<String>) vidd;
            break;
        case "Runners":
        case "runners":
            inRunnersStation = (ArrayList<String>) vidd;
            break;
        case "Wrapping":
        case "wrapping":
            inWrappingStation = (ArrayList<String>) vidd;
            break;
        case "FirstAid":
        case "Firstaid":
        case "firstAid":
        case "firstaid":
            inFirstAidStation = (ArrayList<String>) vidd;
            break;
        case "Volunteers":
        case "volunteers":
            inVolunteersStation = (ArrayList<String>) vidd;
            break;

        default:
            break;
        }
    }
}
}

和欢迎类:

package co.za.gecko.inked.crm;

import java.awt.BorderLayout;

public class Welcome extends JFrame {

static Object[][] databaseInfo;
static Object[] columns = {"first name", "last name", "cellphone", "time", "station"};
static ResultSet rows;
static ResultSetMetaData metaData;
static DefaultTableModel dTableModel = new DefaultTableModel(databaseInfo, columns)    {
//      public Class getColumnClass(int column){
//          Class returnValue;
//          if((column >= 0) && (column < getColumnCount())){
//              returnValue = getValueAt(0, column).getClass();
//          } else {
//              returnValue = Object.class;
//          }
//          return returnValue;
//      }
};

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setBounds(0, 0, 1024, 768);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
        Statement sqlState = conn.createStatement();
        String selectStuff = "SELECT `first_name`, `last_name`, `cellphone`, `time` FROM volunteers";
        rows = sqlState.executeQuery(selectStuff);

        Object[] tempRow;

        while(rows.next()){
            tempRow = new Object[]{rows.getString(1), rows.getString(2), rows.getString(3), rows.getString(4)};
            dTableModel.addRow(tempRow);
        }

        // get column name ?
//          metaData = rows.getMetaData();
//          int numOfCol = metaData.getColumnCount();
//          
//          columns = new String[numOfCol];
//          
//          for(int i=1; i<= numOfCol; i++){
//              columns[i] = metaData.getColumnName(i);
//          }

    } catch (ClassNotFoundException ex) {
        // TODO Auto-generated catch block
        System.out.println(ex.getMessage());
    } catch (SQLException ex) {
        // TODO Auto-generated catch block
        System.out.println(ex.getMessage());
    }

    JTable table = new JTable(dTableModel);

    table.setRowHeight(table.getRowHeight() + 10); // change row height
    table.setFont(new Font("Serif", Font.PLAIN, 20)); // change font
    table.setAutoCreateRowSorter(true); // sort table
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // change column resize
    TableColumn col1 = table.getColumnModel().getColumn(0); // change column width
    col1.setPreferredWidth(100);

    TableColumn tc = table.getColumn("cellphone");
    CenterTableCellRenderer centerRenderer = new CenterTableCellRenderer();
    tc.setCellRenderer(centerRenderer);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800, 500);
    frame.setVisible(true);
}
}
4

1 回答 1

0

(所以我点击 CheckIn 类中的快速链接,

您应该使用 ActionListener(而不是 MouseListener)来监听按钮的点击。

您的代码的基本结构应该是:

JFrame frame = new JFrame();
...
frame.add(scrollPane...);
frame.pack() //or frame.setSize(...)
frame.pack()
frame.setVisible(true);

也就是说,您应该只在将所有组件添加到框架之后才显示框架。现在,您的代码在添加任何组件之前使框架可见。

此外,您不应该使用静态变量。这些变量应该是欢迎类中的实例变量。

上述评论本质上是一般性的,并不是您真正问题的原因。

问题是您的 Welcome 类没有构造函数。

当您通过 JVM 执行 Welcome 类时,将调用 main() 方法,您可以在该方法中创建框架和所有组件。

当您通过其他程序调用 Welcome 类时,会执行 Welcome 类的空构造函数。这段代码什么都不做。由于您的课程扩展了 JFrame,您所看到的只是一个空框架。

因此,要解决您的问题,您基本上需要将与构建框架相关的所有代码移动到 Welcome 类的构造函数中。

于 2013-09-16T15:17:00.330 回答