这是我制作的图形用户界面......当连接到服务器时它不稳定但是当它连接时它不会从数据库中读取信息并显示在图形用户界面内的 JTable 上。在第一块代码下面是我制作的服务器,第一块代码是 GUI 代码。你能指出我哪里出错了,因为我无法弄清楚......
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import javax.swing.*;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import Client_and_Server_Package.*;
public class Staff_GUI extends JFrame {
Container con;
JLabel lblWelcome = new JLabel("Welcome Staff");
JButton btnView = new JButton("View");
JButton btnUpdate = new JButton("Update");
JButton btnLogout = new JButton("Logout");
JTextField txtfilter = new JTextField(10);
JTextField txtstatus = new JTextField(30);
JTable table;
Object[] possibilities = {"None", "Student Grades", "Students on the course", "Students on the Unit"};
/*
String[] columnName = {};
private Connection conn;
private Statement stmt;
private ResultSet rs;*/
Object[] [] data= {{}};
protected ObjectOutputStream output;
protected ObjectInputStream input;
protected JTable message;
protected String chatServer;
protected Socket client;
private RowFilter rf;
private TableRowSorter sorter;
TableModel model;
public Staff_GUI(){
super ("Staff");
lblWelcome.setFont(new Font("Arial", Font.BOLD,24));
con = getContentPane();
con.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
/*
class MyModel extends AbstractTableModel {
public int getColumnCount() {
return columnName.length;
}
public int getRowCount() {
return 10;
}
public String getColumnName(int col) {
return columnName[col];
}
public Object getValueAt1(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt1(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col == 0) {
return true;
} else {
return true;
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
try {
Class.forName("com.mysql.jdbc.Driver");
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8",
"Admin", "Cs170593cS");
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * from Student");
} catch (SQLException e) {
String msg = "MyModel(): Error Connecting to Database:\n"
+ e.getMessage();
System.out.println(msg);
}
}catch (ClassNotFoundException e) {
String msg = "The com.mysql.jdbc.Driver is missing\n"
+ "install and rerun the application";
System.out.println(msg);
System.exit(1);
} finally {
//fireTableCellUpdated(rowIndex, columnIndex);
}
return null;
}
}*/
JPanel top = new JPanel();
top.add(lblWelcome); top.add(new JLabel(" ")); top.add(btnView); top.add(btnUpdate); top.add(btnLogout);
//top.resize(getPreferredSize());
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.ipadx = 0;
c.gridy = 0;
c.ipady = 0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(0,0,0,0);
con.add(top, c);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(0,1));
//JPanel text = new JPanel();
//text.setBorder(new TitledBorder(new EtchedBorder(), "Disploay Area"));
//MyModel model = new MyModel();
//sorter = new TableRowSorter<MyModel>(model);
//ListModel model = ListModel.createModelFromResultSet( rs );
table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
//table.setRowSorter(sorter);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = table.getSelectedRow();
if (viewRow < 0) {
//Selection got filtered away.
txtstatus.setText("");
} else {
int modelRow =
table.convertRowIndexToModel(viewRow);
txtstatus.setText(
String.format("Selected Row in view: %d. " +
"Selected Row in model: %d.",
viewRow, modelRow));
}
}
}
);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(table);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
textPanel.add(scroll, BorderLayout.CENTER);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.ipadx = 800;
c.gridy = 1;
c.ipady = 460;
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(0,0,0,0);
con.add(textPanel, c);
JPanel bottom = new JPanel();
bottom.add(new JLabel("Filter"), BorderLayout.WEST); bottom.add(txtfilter);
bottom.add(new JLabel("Status"), BorderLayout.WEST); bottom.add(txtstatus);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.ipadx = 5;
c.gridy = 2;
c.ipady = 5;
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(0,0,0,0);
con.add(bottom, c);
btnView.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String options = (String)JOptionPane.showInputDialog(
con,
"What you want to View?",
"View",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"None");
if(options == possibilities[0]){
JOptionPane.showMessageDialog(con,
"You selected " + possibilities[0]
+ "\nNo actions will be taken",
"Message",
JOptionPane.WARNING_MESSAGE);
}
}
});
btnUpdate.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
}
});
btnLogout.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
setVisible(false);
Login_GUI log=new Login_GUI();
log.setVisible(true);
}
});
pack();
setSize(1025,600);
setLocation(125,150);
setVisible(true);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@SuppressWarnings("hiding")
private <MyModel> void newFilter() {
RowFilter<MyModel, Object> rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(txtfilter.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
TableRowSorter<MyModel> sorter;
sorter.setRowFilter(rf);
}
public void runClient() //it execute while running client test-need to have in constructor
{
try
{
connectToServer();
getStreams();
processConnection();
}
catch ( EOFException eofException )
{
System.out.println( "\nClient terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
finally
{
closeConnection();
}
}
public void connectToServer() throws IOException
{
System.out.println( "Attempting connection\n" );
client = new Socket( InetAddress.getByName( chatServer ), 12346 );
System.out.println( "Connected to: " +
client.getInetAddress().getHostName() );
}
public void getStreams() throws IOException
{
output = new ObjectOutputStream( client.getOutputStream() );
output.flush();
input = new ObjectInputStream( client.getInputStream() );
System.out.println( "\nGot I/O streams\n" );
}
public void processConnection() throws IOException
{
do
{
try
{
message = ( JTable ) input.readObject();
message = (JTable) model;
}catch ( ClassNotFoundException classNotFoundException ){
System.out.println( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
}
public void resiveData(Object model) throws ClassNotFoundException, IOException{
model = input.readObject();
}
public void closeConnection()
{
System.out.println( "\nClosing connection" );
try
{
// close streams first, then socket
output.close();
input.close();
client.close();
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
public void sendData( String message )
{
try
{
output.writeObject( message );
output.flush();
}
catch ( IOException ioException )
{
//need to do smth!
}
}
public static void main (String [] args){
Staff_GUI staff_gui = new Staff_GUI();
staff_gui.runClient();
}
}