问题描述
- java, client传值控制server端 JLabel上的text值, 可是值一直不变
-
client端代码:
public class KeyboardGUI{public final String PASSWORD = "123"; public JFrame frame; public JTextField textField; public JLabel lblOnoff; public Socket socket; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { KeyboardGUI window = new KeyboardGUI(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public KeyboardGUI() { initialize(); } /** * Initialize the contents of the frame. */ public void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 323, 437); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); lblOnoff = new JLabel("ON/OFF"); lblOnoff.setFont(new Font("Tahoma", Font.PLAIN, 35)); lblOnoff.setBounds(132, 11, 150, 53); frame.getContentPane().add(lblOnoff); JButton button = new JButton("1"); button.setFont(new Font("Tahoma", Font.PLAIN, 18)); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "1"); } }); button.setBounds(30, 110, 80, 50); frame.getContentPane().add(button); JButton button_1 = new JButton("2"); button_1.setFont(new Font("Tahoma", Font.PLAIN, 18)); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "2"); } }); button_1.setBounds(116, 110, 80, 50); frame.getContentPane().add(button_1); JButton button_2 = new JButton("3"); button_2.setFont(new Font("Tahoma", Font.PLAIN, 18)); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "3"); } }); button_2.setBounds(202, 110, 80, 50); frame.getContentPane().add(button_2); JButton button_3 = new JButton("4"); button_3.setFont(new Font("Tahoma", Font.PLAIN, 18)); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "4"); } }); button_3.setBounds(30, 167, 80, 50); frame.getContentPane().add(button_3); JButton button_4 = new JButton("5"); button_4.setFont(new Font("Tahoma", Font.PLAIN, 18)); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText() + "5"); } }); button_4.setBounds(116, 167, 80, 50); frame.getContentPane().add(button_4); JButton btnReset = new JButton("Reset"); btnReset.setFont(new Font("Tahoma", Font.PLAIN, 18)); btnReset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); } }); btnReset.setBounds(30, 337, 80, 50); frame.getContentPane().add(btnReset); JButton btnSubmit = new JButton("Submit"); btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18)); btnSubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { KeypadClientSocket(); } catch (Exception e3) { // TODO Auto-generated catch block e3.printStackTrace(); } DataOutputStream out = null; try { out = new DataOutputStream(socket.getOutputStream()); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } String textFieldSting = new String(); textFieldSting = textField.getText(); if(textFieldSting.equals(PASSWORD)) { lblOnoff.setText("OFF"); lblOnoff.setForeground(Color.GREEN); try { out.writeInt(1); out.flush(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } else { lblOnoff.setText("ON"); lblOnoff.setForeground(Color.RED); try { out.writeInt(0); out.flush(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } textField.setText(""); } }); btnSubmit.setBounds(116, 337, 166, 50); frame.getContentPane().add(btnSubmit); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.PLAIN, 15)); textField.setBounds(30, 65, 252, 39); textField.setText(""); frame.getContentPane().add(textField); } public void KeypadClientSocket() throws Exception { socket = new Socket("10.0.0.29", 4000); }
}
Server端代码:
Main.java:public class Main
{
public static void main(String[] args) throws Exception
{
SensorGUI1 sensorGUI = new SensorGUI1();
sensorGUI.SensorGUI();KeypadServerThread(); } public static void KeypadServerThread() throws Exception { ServerSocket serverSocket = new ServerSocket(4000); while (true) { Socket socket = serverSocket.accept(); new KeypadServerThread(socket).start(); } }
}
KeypadServerThread.java:
public class KeypadServerThread extends Thread
{
private Socket socket;
public int data=0;public KeypadServerThread(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { DataInputStream is = new DataInputStream(socket.getInputStream()); NumObservable numObservable = new NumObservable(); numObservable.addObserver(new SensorGUI1()); int dataFromUser = is.readInt(); numObservable.setData(dataFromUser); if (dataFromUser == 1) { is.close(); } } catch (IOException e) { e.printStackTrace(); } }
}
class NumObservable extends Observable {
public int data=2;public int getData() { return data; } public void setData(int i) { data = i; setChanged(); notifyObservers(); }
}
SensorGUI1.java:
public class SensorGUI1 implements Observer {
private JFrame frame; JButton btnOn; JLabel lblBackgroud; int feedBack; NumObservable myObserable; /** * Launch the application. */ public void SensorGUI() { EventQueue.invokeLater(new Runnable() { public void run() { try { SensorGUI1 window = new SensorGUI1(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public SensorGUI1() { initialize(); } public void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 749, 414); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLayeredPane layeredPane = new JLayeredPane(); layeredPane.setBounds(10, 11, 713, 353); frame.getContentPane().add(layeredPane); lblBackgroud = new JLabel("Backgroud"); lblBackgroud.setBounds(622, 282, 62, 14); layeredPane.add(lblBackgroud); btnOn = new JButton("ON"); btnOn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnOn.setBounds(603, 11, 89, 44); layeredPane.add(btnOn); } @Override public void update(Observable o, Object arg) { myObserable = (NumObservable) o; new Thread(new Runnable() { @Override public void run() { if (myObserable.getData() == 1) { System.out.println(myObserable.getData()); lblBackgroud.setText("OFF"); JOptionPane.showMessageDialog(frame, "The user turned off the system"); } else if (myObserable.getData() == 0) { lblBackgroud.setText("ON"); JOptionPane.showMessageDialog(frame, "The user turned on the system"); } } }).start();; }
}
时间: 2025-01-18 19:22:58