在另一个类java中访问其他类公共变量

时间:2017-03-14 13:39:35

标签: java jframe jlabel netbeans-8

我正在创建一个使用套接字连接连接到服务器的Android应用程序。在服务器中,我在同一个包中有两个类( server.java和MainFrame.java )。当连接获得接受时,我想要 变量值 属于 其他类。我有定义该变量公众但仍然无法实现我的目标。

所以这里我想在接受连接时从 Server.class 更改 JLable 值但是它不会更改值并保留前一个值。标签位于 JFrame 上。我正在使用netbeans来编写服务器代码。

这是第一个文件:

Server.java

package server;

import java.io.*;
import java.net.*;
import javax.swing.JFrame;


public class Server extends Thread {
public static int SERVERPORT = 8002;
private boolean running = false;
public volatile boolean stop = false;
public Socket client = null;
ServerSocket sc = null;
final MainFrame framea = new MainFrame();//creating instance of second class

public static void main(String[] args) {
    MainFrame frame = new MainFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

@Override
public void run() {
    super.run();
    running = true;
    try {
        System.out.println("Server Has Started........ \nWaiting for client........");
        sc = new ServerSocket(SERVERPORT);
        try {
            while (!stop && running) {
                client = sc.accept();
                System.out.println("Connection Accepted......");
                framea.labInfo.setText("Connected To Client");//HERE i want to change value
            }
            if (stop) {
                sc.close();
                client.close();
                System.out.println("Server Has Stopped");
                System.exit(0);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    } catch (Exception e) {
        System.out.println("Error");
        e.printStackTrace();
    }

}
 public void requestStop(){ 
   try{stop = true;
   sc.close();
   client.close();
   System.out.println("Server Has Stopped");
   }catch(Exception e){System.out.println("Error"+e.getMessage());}
   }
}

第二档:

MainFrame.java

package server;

import javax.swing.*;
import java.awt.*;
import java.net.*;

public class MainFrame extends JFrame {
private Server mServer;
public MainFrame() {
    super("MainFrame");
    //mServer = new Server();
    initComponents();
    setIcon();
    labInfo.setText("Not Connected");
    try{ ipAdd.setText(String.valueOf(InetAddress.getLocalHost().getHostAddress()));
    }catch(Exception e){System.out.println(e.getMessage());}

}                     

private void connActionPerformed(java.awt.event.ActionEvent evt) {                                     
            mServer = new Server();
            conn.setEnabled(false);
            labInfo.setText("Waiting for Connection.....");
            mServer.start();                
}                                    

private void disconnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    mServer.requestStop();
    labInfo.setText("Not Connected");
    conn.setEnabled(true);        
}                                       

public static void main(String args[]) {
          /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MainFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton conn;
private javax.swing.JButton disconn;
private javax.swing.JLabel ipAdd;
private javax.swing.JLabel ipAddLab;
public javax.swing.JLabel labInfo;
private javax.swing.JTextField port;
private javax.swing.JLabel portLab;
// End of variables declaration          
}

0 个答案:

没有答案