2015-04-17 27 views
-3

我想从另一个类中访问存储在“tf”中的值,一旦用户键入了他的消息。
这是一段时间,因为我已经与Java的工作,所以,我只是做这个项目的乐趣(不是我的代码,我只是从教程编辑它)。
我已经尝试了很多东西,并且研究了很多与方法和对象有关的事情,我觉得我已经很亲密了。我尝试访问的值是在“tf”上:如何访问不同类别的变量

client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, tf.getText())); 
tf.setText(""); 
return; 

我的目标是在另一个类中打印来自“tf”的字符串。

public class ClientGUI extends JFrame implements ActionListener { 

    private static final long serialVersionUID = 1L; 
    // will first hold "Username:", later on "Enter message" 
    private JLabel label; 
    // to hold the Username and later on the messages 
    private JTextField tf; 
    // to hold the server address an the port number 
    private JTextField tfServer, tfPort; 
    // to Logout and get the list of the users 
    private JButton login, logout, whoIsIn; 
    // for the chat room 
    private JTextArea ta; 
    // if it is for connection 
    private boolean connected; 
    // the Client object 
    private Client client; 
    // the default port number 
    private int defaultPort; 
    public String userMessage; 
    private String defaultHost; 
    // Constructor connection receiving a socket number 
    ClientGUI(String host, int port) { 

     super("Chat Client"); 
     defaultPort = port; 
     defaultHost = host; 

     // The NorthPanel with: 
     JPanel northPanel = new JPanel(new GridLayout(3,1)); 
     // the server name and the port number 
     JPanel serverAndPort = new JPanel(new GridLayout(1,5, 1, 3)); 
     // the two JTextField with default value for server address and port number 
     tfServer = new JTextField(host); 
     tfPort = new JTextField("" + port); 
     tfPort.setHorizontalAlignment(SwingConstants.RIGHT); 

     serverAndPort.add(new JLabel("Server Address: ")); 
     serverAndPort.add(tfServer); 
     serverAndPort.add(new JLabel("Port Number: ")); 
     serverAndPort.add(tfPort); 
     serverAndPort.add(new JLabel("")); 
     // adds the Server an port field to the GUI 
     northPanel.add(serverAndPort); 

     // the Label and the TextField 
     label = new JLabel("Enter your username below", SwingConstants.CENTER); 
     northPanel.add(label); 
     tf = new JTextField("Scooby Doo"); 
     tf.setBackground(Color.WHITE); 
     northPanel.add(tf); 
     add(northPanel, BorderLayout.NORTH); 

     // The CenterPanel which is the chat room 
     ta = new JTextArea("Welcome to the Chat room\n", 80, 80); 
     JPanel centerPanel = new JPanel(new GridLayout(1,1)); 
     centerPanel.add(new JScrollPane(ta)); 
     ta.setEditable(false); 
     add(centerPanel, BorderLayout.CENTER); 

     // the 3 buttons 
     login = new JButton("Login"); 
     login.addActionListener(this); 
     logout = new JButton("Logout"); 
     logout.addActionListener(this); 
     logout.setEnabled(false);  // you have to login before being able to logout 
     whoIsIn = new JButton("Who is in"); 
     whoIsIn.addActionListener(this); 
     whoIsIn.setEnabled(false);  // you have to login before being able to Who is in 

     JPanel southPanel = new JPanel(); 
     southPanel.add(login); 
     southPanel.add(logout); 
     southPanel.add(whoIsIn); 
     add(southPanel, BorderLayout.SOUTH); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(600, 600); 
     setVisible(true); 
     tf.requestFocus(); 

    } 


    // called by the Client to append text in the TextArea 
    void append(String str) { 
     ta.append(str); 
     ta.setCaretPosition(ta.getText().length() - 1); 
    } 
    // called by the GUI is the connection failed 
    // we reset our buttons, label, textfield 
    void connectionFailed() { 
     login.setEnabled(true); 
     logout.setEnabled(false); 
     whoIsIn.setEnabled(false); 
     label.setText("Enter your username below"); 
     tf.setText("Anonymous"); 
     // reset port number and host name as a construction time 
     tfPort.setText("" + defaultPort); 
     tfServer.setText(defaultHost); 
     // let the user change them 
     tfServer.setEditable(false); 
     tfPort.setEditable(false); 
     // don't react to a <CR> after the username 
     tf.removeActionListener(this); 
     connected = false; 
    } 

    /* 
    * Button or JTextField clicked 
    */ 
    public void actionPerformed(ActionEvent e) { 
     Object o = e.getSource(); 
     // if it is the Logout button 
     if(o == logout) { 
      client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, "")); 
      return; 
     } 
     // if it the who is in button 
     if(o == whoIsIn) { 
      client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, "")); 
      return; 
     } 

     // ok it is coming from the JTextField 
     if(connected) { 

      // just have to send the message 
      client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, tf.getText())); 
      tf.setText(""); 
      return; 
     } 


     if(o == login) { 
      // ok it is a connection request 
      String username = tf.getText().trim(); 
      // empty username ignore it 
      if(username.length() == 0) 
      return; 
      // empty serverAddress ignore it 
      String server = tfServer.getText().trim(); 
      if(server.length() == 0) 
      return; 
      // empty or invalid port numer, ignore it 
      String portNumber = tfPort.getText().trim(); 
      if(portNumber.length() == 0) 
      return; 
      int port = 0; 
      try { 
       port = Integer.parseInt(portNumber); 
      } 
      catch(Exception en) { 
       return; // nothing I can do if port number is not valid 
      } 

      // try creating a new Client with GUI 
      client = new Client(server, port, username, this); 
      // test if we can start the Client 
      if(!client.start()) 
      return; 
      tf.setText(""); 
      label.setText("Enter your message below"); 
      connected = true; 

      // disable login button 
      login.setEnabled(false); 
      // enable the 2 buttons 
      logout.setEnabled(true); 
      whoIsIn.setEnabled(true); 
      // disable the Server and Port JTextField 
      tfServer.setEditable(false); 
      tfPort.setEditable(false); 
      // Action listener for when the user enter a message 
      tf.addActionListener(this); 
     } 

    } 


    // to start the whole thing the server 
    public static void main(String[] args) { 
     new ClientGUI("124.178.99.83", 25565); 
    } 

} 
+1

这是否其他对象(类)具有与ClientGUI参考? – BretC

+1

只需在方法调用中传递值即可。 –

回答

0

您必须通过构造函数通过引用传递对象。您可以通过整个的JFrame,使您的TF公共 -

public static void main(String[] args){ 
    ClientGUI x = new ClientGUI("0.0.0.0", 1234); 
    OtherClass y = new OtherClass(x); 
} 

public class otherClass { 
    private ClientGUI x; 
    public otherClass(ClientGUI x){ 
      this.x = x; 
    } 
    public void print(){ 
      System.out.println(x.tf.getText()); 
    } 
} 

,或者你可以通过TF值当您创建其他类