2016-04-18 10 views
1

私はTCPサーバー/クライアントシステムを作成しています。現在、JButtonJTextAreaJTextFieldJFrameを含むConnectionFrame.javaファイルを作成しました。 。別のクラスからクラスを開始する

:私のクライアントを起動すると、私は私のclient.jarのを起動したときので、 ConnectionFrameも同様に実行されなければならない(この JFrameを表示させたい

私は、クライアントのメインメソッドに次の行を置くことによってこれを行うことが想定さ。

ConnectionFrame cf = new ConnectionFrame(); 

しかし運と もう一つの試みは書いていました:何らかの理由でめちゃくちゃに私の元のコードに見えた

ConnectionFrame.main(argv); 

EDIT:コンテキスト

のためのコードを追加ConnectionFrame:

public class ConnectionFrame extends JFrame implements ActionListener { 

    JButton buttonLayout = new JButton("Connect to server"); 
    JTextField textFieldLayout = new JTextField(1); 
    JTextArea consoleOutput = new JTextArea(1,20); 

    public void addComponentToPane(Container pane) { 

     buttonLayout.addActionListener(this); 
     textFieldLayout.setHorizontalAlignment(JTextField.CENTER); 
     consoleOutput.setEditable(false); 

     pane.add(buttonLayout, BorderLayout.PAGE_START); 
     pane.add(textFieldLayout, BorderLayout.CENTER); 
     pane.add(consoleOutput, BorderLayout.PAGE_END); 
    } 

    public static void ConnectionFrame() { 
     ConnectionFrame frame = new ConnectionFrame(); 
     frame.addComponentToPane(frame.getContentPane()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if("Connect to server".equals(e.getActionCommand())){ 
      consoleOutput.append("CONSOLE: Triggered"); 
     } 
    } 

    public static void main(String[] args) { 
     ConnectionFrame(); 
    } 
} 

クライアント:

public class TCPClient { 

    public static void main(String argv[]) throws Exception { 

     ConnectionFrame cf = new ConnectionFrame(); 

     String sentence; 
     String modifiedSentence; 
     BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     Socket clientSocket = new Socket("localhost", 54343); 
     DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 

     CanvasFrame myFrame = new CanvasFrame(); 
     myFrame.setVisible(true); 
     ArrayList<Point> points = myFrame.location; 

     outToServer.writeInt(points.size()); 

     for(Point p : points) 
     { 
      outToServer.writeInt(p.x); 
      outToServer.writeInt(p.y); 
     } 

     BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
     sentence = inFromUser.readLine(); 
     outToServer.writeBytes(sentence + '\n'); 
     modifiedSentence = inFromServer.readLine(); 
     System.out.println("FROM SERVER: " + modifiedSentence); 
     clientSocket.close(); 
    } 
} 
+2

あなたは[mcve]を作っていただけますか? –

答えて

3

あなたはこのようなevent-dispatching threadのために作成タスクのスケジュールを設定する必要があります。

SwingUtilities.invokeLater(
     new Runnable() { 
      public void run() { 
       ConnectionFrame(); 
      } 
     } 
); 

Java 8の場合は

SwingUtilities.invokeLater(ConnectionFrame::ConnectionFrame); 
+0

@flkes that's right thx –

関連する問題