JTextField在换行时删除String而不是追加

时间:2015-05-13 21:07:09

标签: java swing textarea newline readline

我正在尝试为客户端应用程序创建GUI。 BufferedReader的.readLine()充当输入流。问题是它在控制台中完美地打印换行符和回车符,但是一旦我尝试将它附加到TextArea中,它似乎只是重写了前一个字符串。这是示例代码

 public class AirlinesClient extends JFrame implements ActionListener {
    public static BufferedReader socketIn = null;
    public static PrintWriter socketOut = null;
    public static Socket connection = null;
    public static String breakline;
    public static String command;
    public AirlinesClient() throws UnknownHostException, IOException{
        JPanel schPanel = new JPanel();
        JButton Schedules  = new JButton ("Flight Schedules");
        JButton Pilots  = new JButton ("Pilots' shifts");
//mainFrame
          super.setSize(300, 400);
          setLocationByPlatform(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setVisible(true);
//adding ScheduleButton's panel to the root frame
          schPanel.setLayout(null);
          getContentPane().add(schPanel);
//Adding Schedule button
          Schedules.setBounds(75, 10, 150, 50);
          Schedules.setVisible(true);
          schPanel.add(Schedules);
//Adding action on click
          Schedules.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {      
                  JTextArea area = new JTextArea(20,20);
                  JFrame scheduleFrame = new JFrame("Information");
                  scheduleFrame.setSize(800,400);
                  scheduleFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                  scheduleFrame.setLocationRelativeTo(null);
                  scheduleFrame.add(area);                               
                  scheduleFrame.setVisible(true);   
                  socketOut.flush();
                  socketOut.println("1");
                  area.append(breakline);

            }
        });

//Adding pilots' shifts       
          Pilots.setBounds(75, 70, 150, 50);
          Pilots.setVisible(true);
          schPanel.add(Pilots);

    }

    public static void main(String[] args) throws UnknownHostException, IOException {

        new AirlinesClient();

        int port = 1234;
        String host = "localhost";
        connection = new Socket(host,port);
        socketIn = new BufferedReader(new      InputStreamReader(connection.getInputStream()));
        socketOut = new PrintWriter(connection.getOutputStream(),true);

        try{    
        System.out.println("Successfully connected to the server!");
do{         
//This prints the output in the console and it ends when the server sends $$$
            while (!(breakline =socketIn.readLine()).equals("$$$")){
                System.out.println(breakline);
            }
//Command is input from the user keyboard but for testing purposes I'm only using
//a single socketOut command(from the actionlistener) to send commands      
        }while(command!="0");
        System.out.println("Closing connection to server!");
    }catch(IOException e){
        e.printStackTrace();
    } finally{
           try{
               if(socketIn!=null) socketIn.close();
               if(socketOut!=null) socketOut.close();
               if(connection!=null) connection.close();
             }
             catch(IOException e){
               System.err.println("Socket could not be closed!");
             }
           }

    }
    @Override
    public void actionPerformed(ActionEvent arg0) {

    }
}

0 个答案:

没有答案