2016-10-07 28 views
0

我無法爲多個客戶端加載JFrame,但對於單個客戶端而言,它工作正常。我能夠在單個客戶端上執行所有操作。框架變得可見但內容不可見。JFrame不爲多個客戶端加載

我需要添加一些額外的東西嗎?我是否正確連接?我正在本地機器上運行它。

我可以在同一臺機器上運行多個實例嗎?

服務器:

public class ServerThread extends Thread{ 
    public static void main(String[] args) throws IOException { 
     EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       ServerSocket serverSocket = new ServerSocket(4518); 
       while(true){ 
       Socket clientSocket = serverSocket.accept(); 
       Thread window=new Frame1(); 
       window.start(); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

客戶:

public class Client extends JFrame{ 
    public static void main(String arg[]){ 
     String hostName="localhost"; 
     int portNumber=4518; 
     try{ 
      Socket echoSocket = new Socket(hostName, portNumber); 
      PrintWriter out = 
       new PrintWriter(echoSocket.getOutputStream(), true); 
      BufferedReader in = 
       new BufferedReader(
        new InputStreamReader(echoSocket.getInputStream())); 

     } 

的JFrame:

public class Frame1 extends Thread{ 

    static JFrame frame; 
    private static JTextField textField; 
    private static JPasswordField passwordField; 
    static Connection conn = null; 
    static PreparedStatement pst = null; 
    static ResultSet rs = null; 
public Frame1() { 
     System.out.println("hello"); 
     initialize(); 
    } 
private static void initialize() { 
     frame = new JFrame(); 
     frame.setVisible(true); 
     frame.setTitle("Course Registration- Spring 2017"); 
     frame.getContentPane().setBackground(new Color(204, 204, 255)); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JButton btnNewButton = new JButton("Sign in"); 
     btnNewButton.setBackground(new Color(204, 102, 153)); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       conn = DBConnect.DBConnect(); 
       String sql = "Select * from users where username =? and password=?"; 
       try{ 
        String userName=textField.getText(); 
        String password=passwordField.getText(); 
        pst = conn.prepareStatement(sql); 
        pst.setString(1, userName); 
        pst.setString(2, password); 
        rs = pst.executeQuery(); 

        if(userName.equals("admin") && password.equals("admin")){ 
         ServerSideViews s = new ServerSideViews(); 
         s.setVisible(true); 
         s.setUserName(userName); 
         s.setFrameLogin(frame); 
         System.out.println(userName); 
         frame.setVisible(false); 
        } 

        else if(rs.next() && !(userName.equals("admin"))){ 
         JOptionPane.showMessageDialog(null, "Welcome " + textField.getText()); 
         CourseRegistration c = new CourseRegistration(); 
         c.setVisible(true); 
         c.setFrameLogin(frame); 
         frame.setVisible(false); 
         String sql1 = "Select major from users where username=?;"; 
         System.out.println(sql1); 
         pst = conn.prepareStatement(sql1); 
         pst.setString(1, userName); 
         rs = pst.executeQuery(); 
         String major=""; 
         while(rs.next()) 
         { 
         major=rs.getString("major"); 
         } 
         c.setUserName(userName); 
         c.comboBox.setSelectedItem(major); 
         c.comboBox.disable(); 
        } 
        else{ 
         JOptionPane.showMessageDialog(null, "Invalid user name or password");   
        } 
       }catch(Exception e1){ 
         e1.printStackTrace(); 
       } 
      } 
     }); 


     btnNewButton.setBounds(157, 182, 117, 29); 
     frame.getContentPane().add(btnNewButton); 

     JLabel lblUserName = new JLabel("User Name"); 
     lblUserName.setBounds(76, 96, 86, 16); 
     frame.getContentPane().add(lblUserName); 

     JLabel lblPassword = new JLabel("Password"); 
     lblPassword.setBounds(86, 129, 61, 16); 
     frame.getContentPane().add(lblPassword); 

     textField = new JTextField(); 
     textField.setBounds(157, 91, 130, 26); 
     frame.getContentPane().add(textField); 
     textField.setColumns(10); 

     JButton btnNewButton_1 = new JButton("Register"); 
     btnNewButton_1.setBackground(new Color(51, 102, 204)); 
     btnNewButton_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       UserRegistration reg = new UserRegistration(); 
       reg.setVisible(true); 
       reg.setFrameLogin(frame); 
       frame.setVisible(false); 
      } 
     }); 
     btnNewButton_1.setBounds(157, 225, 117, 29); 
     frame.getContentPane().add(btnNewButton_1); 

     passwordField = new JPasswordField(); 
     passwordField.setBounds(157, 124, 130, 26); 
     frame.getContentPane().add(passwordField); 

     JLabel lblNewLabel = new JLabel("Course Registration - Spring 2017"); 
     lblNewLabel.setBounds(109, 24, 241, 16); 
     frame.getContentPane().add(lblNewLabel); 
    } 
} 
+0

'靜態JFrame幀;'只有*** 1 ***幀。從這個和任何其他組件刪除'static'標識符。事實上,除非你能解釋爲什麼從設計POV中使用'static'工作,只是不要使用它。這通常不是解決當前問題的正確解決方案,更常見的問題來源。 –

+0

'frame.getContentPane()。setLayout(null);'呃..你在哪裏得到編碼技巧?麥片盒的背面? Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –

+0

並且在將所有組件添加到jframe上之後,在jframe上執行setVisible。 –

回答

1

不要啓動一個任務,在事件指派線程(EDT)塊。 GUI將無法響應事件。

所以不要從SwingUtilities.invokeLater(..)啓動ServerSocket。

相反,您需要爲ServerSocket使用單獨的線程。

有關更多信息和示例,請參閱Concurrency上的Swing教程部分。