2016-04-24 4 views
-1

私のメインサーバループは完璧に動作します:JFrameを持つJavaサーバがデバッグモードで動作していますが、通常の実行モードで動作していないのはなぜですか?私は、デバッグモードでのステップ実行時に以下の

  • スタートボタンが押されたとき、サーバーは、ユーザーがテキストフィールドに書いたとスタートボタンが
  • を無効にされたソケットで始まります
  • サーバがメッセージを開始したときに大きなテキストエリアに表示されます。クライアントが接続すると、
  • メッセージが表示されます。
  • サーバが取得できませんメインのものを除いて他のループにはまっています。ユーザーが

を終了することを選択した場合にのみ、終了しました。しかし、私は通常モードでプログラムを実行し、スタートボタンが押されて起こることすべてがあるとき:

  • スタートボタンが
  • 何もメッセージを無効にしませんます指定されたソケット

感謝すべてのヘルプに接続することはできません

  • クライアントを表示されます。

    public class ServerLoop { 
        static boolean running; 
        static int currentState=0; 
        static int port; 
        static ServerSocket server = null; 
    
        public static void main (String[] args) throws IOException { 
         MainServer main = new MainServer(); 
         while (true) { 
          currentState = main.getCurrentState(); 
          switch (currentState) { 
          case 1: 
           port = main.getPortnumber(); 
           JTextArea text = main.getTextArea(); 
           String temp = text.getText(); 
           temp = temp + "Portnumber: "+ port +"\n" + 
            "Server ip-address: " + main.getIpAddress()+ "\n"; 
           main.updateText(temp); 
           server = new ServerSocket(port); 
           currentState++; 
           break; 
    
          case 2: 
           Socket clientConnection = server.accept(); 
           text = main.getTextArea(); 
           temp = text.getText(); 
           temp = temp + "Connection with: " + 
            clientConnection.getInetAddress().getHostName() + 
            "established. \n"; 
           main.updateText(temp); 
           new ClientConnected(clientConnection); 
           break; 
    
          case 3: 
           text = main.getTextArea(); 
           temp = text.getText(); 
           temp = temp + "Server shutting down...\n"; 
           main.updateText(temp); 
           server.close(); 
           text = main.getTextArea(); 
           temp = text.getText(); 
           temp = temp + "Server shutdown\n"; 
           main.updateText(temp); 
           main.setIdle(true); 
           currentState=0; 
           break; 
    
          default: 
           break; 
          } 
         } 
        } 
    } 
    

    このクラスは、サーバー用のインターフェイスそのわずか一つの大きなテキストエリアを持つシンプルなもの、一つの小さなテキストフィールドとリスナーとの2つのボタンを作成します。

    私はサーバーループとアクションリスナーをお互いを嫌うボタンで推測する必要がありますが、なぜデバッグモードで動作しているのかわかりません。

    public class MainServer extends JFrame { 
        JTextArea mainTextfield; 
        String k = ""; 
        JTextField portnumber; 
        JFrame frame; 
        JButton start,close; 
        int currentState = 0; 
        int porten; 
        boolean idle = true; 
    
        public MainServer() { 
         frame = new JFrame("ArenanServer v1.0"); 
         frame.setLayout(new FlowLayout()); 
         frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
         frame.setSize(600, 550); 
         frame.setResizable(false); 
         frame.setBackground(Color.LIGHT_GRAY); 
    
         mainTextfield = new JTextArea(); 
         mainTextfield.setPreferredSize(new Dimension(585,450)); 
         mainTextfield.setBackground(Color.WHITE); 
         mainTextfield.setOpaque(true); 
         mainTextfield.setBorder(BorderFactory.createLoweredBevelBorder()); 
         mainTextfield.setEditable(false); 
         JScrollPane scroll = new JScrollPane(mainTextfield) 
         frame.add(scroll); 
    
         JLabel port = new JLabel(); 
         port.setText("Port: "); 
         frame.add(port); 
    
         portnumber = new JTextField(); 
         portnumber.setBorder(BorderFactory.createLoweredBevelBorder()); 
         portnumber.setPreferredSize(new Dimension(100,25)); 
         portnumber.setDocument(new JTextFieldLimit(5)); 
         frame.add(portnumber); 
    
         ////I think this might be a problem with the actionlisteners on buttons// 
         start = new JButton("Start server"); 
         close = new JButton("Close server"); 
    
         start.addActionListener(new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent arg0) { 
           if (portnumber.getText().length()>0) { 
            porten = Integer.parseInt(portnumber.getText()); 
            start.setEnabled(false); 
            close.setEnabled(true); 
            currentState = 1; 
            idle=false; 
           } 
           else { 
            k = mainTextfield.getText(); 
            k = k + "Please enter a port number.\n"; 
            updateText(k); 
           } 
          } 
         }); 
    
        close.setEnabled(false); 
        close.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent arg0) { 
          currentState = 3; 
          start.setEnabled(true); 
          close.setEnabled(false); 
         } 
        }); 
    
        frame.add(start); 
        frame.add(close); 
        frame.setVisible(true); 
    } 
    

    これらは、メインループとインターフェイスと同じクラスに配置されたインターフェイス間の通信を強化するために作成されたいくつかの機能です。

    protected JButton getStartButton() { 
        return start; 
    } 
    protected JButton getCloseButton() { 
        return close; 
    } 
    protected int getPortnumber() { 
        return porten; 
    } 
    
    protected JTextArea getTextArea() { 
        return mainTextfield; 
    } 
    protected JFrame getFrame() { 
        return frame; 
    } 
    protected boolean updateText(String l) { 
        mainTextfield.setText(l); 
        frame.repaint(); 
        if (mainTextfield.getText().equals(l)) { 
         return true; 
        } else { 
         return false; 
        } 
    } 
    protected boolean updateview() { 
        frame.repaint(); 
        return true; 
    } 
    protected int getCurrentState() { 
        if (idle) { 
         return 0; 
        } else { 
         if (currentState != 1) { 
          return currentState; 
         } else { 
          currentState++; 
          return 1; 
         } 
        } 
    } 
    protected void setIdle(boolean t) { 
        idle = t; 
    } 
    

    Picture of what it looks like in both runs after pressing the start button and trying to connect to the socket, debug mode to the right.

  • 答えて

    1

    あなたの質問を検討している間、私は、その読みやすさを向上させるために、それを編集しました。タブとスペースの混在と一貫性のないインデントがStackOverflowコードパーサーを台無しにしてしまったため、コードを再フォーマットする必要がありました。

    私が思うことをしながら私は答えを見つけた: public MainServer() {から中括弧の閉鎖とstart.addActionListener(new ActionListener()からの第一ラウンドブラケットの閉鎖は間違っそれぞれ、相互に交差しています。

    これはあなたが持っているものです。

    public MainServer() { 
         ... 
         start.addActionListener(new ActionListener() { 
          ...     
         } 
        }); 
    

    これは、私はあなたが持つべきだと思うものです:

    public MainServer() { 
         ... 
         start.addActionListener(new ActionListener() { 
          ...     
         }); 
        } 
    

    私は教訓を学んだことが推測:書式設定

    • コード重要です - あなたはコードフォーマットで一貫しているようにしようとしている間もエラーを見つけたでしょうティン
    • ない良いアイデアは
    • スペースがインデントのために優れているインデントにタブとスペースを混在させる - StackOverflowのエディタ:)に優しい
    +0

    が、これは今まで私の初めてのstackoverflowを使用してあなたの答えに感謝ですこんにちは私はちょうどコピー/エディタにEclipseから私のコードを貼り付けて、スペースですべての行に衝撃を与えた。多分最善の方法はありません。しかし、私の質問の編集を編集していただきありがとうございます。あなたの答えに、あなたが正しくあなたを理解しているなら、あなたはそれを意味します。このようにすべきです ")};" start.addActionListenter?または私はあなたを間違って理解していますか? :) もし私がそれをしようとすると、私は多くのエラーを取得します。他のアイデアはありますか? :)ありがとう – ThePerzon

    +0

    私は答えを明確にしました –

    +0

    私はあなたが今何を意味するかを見ることができました。あなたが示唆しているものは、私が持っているものです。あなたが言及したところでは、1対多の "}"があることがわかりました。私はそれを編集した。あなたは何か問題があるかもしれない他の考えを持っていますか? – ThePerzon

    関連する問題