English 中文(简体)
我该如何从帮手班派弦乐队去JText Field?
原标题:How do I send String to JTextField from helper class?

例如,在下面的代码中,我想修改 JTextField 来显示端口的扫描是否打开, 但我不知道该如何操作。 也许我需要一些辅助方法?

public void actionPerformed(ActionEvent arg0) {
  console.setText("Starting check
" +
    "Start = "+stopServTf.getText()+
    "
Stop ="+stopServTf.getText()+
    "
Port Start ="+startPortTf.getText()+
    "
Port Stop ="+stopPortTf.getText());
  PortScanner p= new PortScanner(Integer.parseInt(startPortTf.getText()),
    Integer.parseInt(stopPortTf.getText()),startServTf.getText());
}

其他方法/构件

public   PortScanner(int portStart, int portStop ,String ip) {
  for (int i = portStart; i <= portStop; i++) {
    try {
      Socket ServerSok = new Socket(ip, i);
      setString(i);
      //System.out.println("Port in use: " + i);
      ServerSok.close();
    } catch (Exception e) {
    }
    setStrinN(i);
    System.out.println("Port not in use: " + i);
  }
}

public static String setString(int i) {
  return "Port in use: " +i;
}

public static String setStrinN(int i){
  return "Port not in use: "+i;
} 

所以我要发送到 condole. setText ();

最佳回答

当您调用 setString(i); 时,该函数返回一个被丢弃的字符串( 即不存储任何地方 ) 。 您要么需要存储字符串, 然后再取回它, 如果您想要调用 concolole. setText() 与该返回的字符串, 您需要将 < code> concolole 传给 < code> 到 < code> PortScanner 构建器 :

public PortScanner(int portStart, int portStop, String ip, JTextArea console)

将调用电话改为 setString(i);

console.setText(setString(i));

此外(由于 < code> PortScanner 构建器已更改),调用

 PortScanner p= new PortScanner(Integer.parseInt(startPortTf.getText()),
   Integer.parseInt(stopPortTf.getText()),startServTf.getText()); 

actionPerfact ()

问题回答

建议:

  • Be sure to do your port scanning in a background thread with respect to the Swing GUI, such as one provided by a SwingWorker.
  • When updating any Swing components, be sure to do this on the Swing event thread, the EDT. Again, a SwingWorker will help as it has facilities to automate this (publish/process).
  • Give your GUI class a public method that would allow outside classes to update its textfield, such as public void consoleSetText(String text)
  • Pass a reference to the GUI class into the helper class, so that the helper class can call the above mentioned method. It would of course call the method on the Swing event thread, using the SwingWorker process(...) method.

Edit 1
this is an example of what I am suggestion:

class PortScannerGui {
   private JTextField startPortTf;
   private JTextField stopPortTf;
   private JTextField console;
   private JTextField stopServTf;
   private JTextField startServTf;

   // ... lots of code mising

   public void actionPerformed(ActionEvent arg0) {
      console.setText("Starting check
" + "Start = " + stopServTf.getText()
            + "
Stop =" + stopServTf.getText() + "
Port Start ="
            + startPortTf.getText() + "
Port Stop =" + stopPortTf.getText());

      new SwingWorkerPortScannerWrapper(this, 
            Integer.parseInt(startPortTf.getText()), 
            Integer.parseInt(stopPortTf.getText()),
            startServTf.getText()).execute();
   }

   public void setConsoleText(String text) {
      console.setText(text);
   }
}

class SwingWorkerPortScannerWrapper extends SwingWorker<Void, Integer> {
   private PortScannerGui gui;
   private PortScanner portScanner;

   public SwingWorkerPortScannerWrapper(PortScannerGui gui, int portStart,
         int portStop, String ip) {
      this.gui = gui;
      portScanner = new PortScanner(portStart, portStop, ip);
   }

   @Override
   protected Void doInBackground() throws Exception {
      portScanner.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            if (PortScanner.PORT_IN_USE_NUMBER.equals(evt.getPropertyName())) {
               publish((Integer) evt.getNewValue());
            }
         }
      });

      portScanner.scan();
      return null;
   }

   @Override
   protected void process(List<Integer> chunks) {
      for (Integer chunk : chunks) {
         gui.setConsoleText("Port in use: " + chunk);
      }
   }
}

class PortScanner {
   public static final String PORT_IN_USE_NUMBER = "port in use number";
   public static final String STRIN_N = "strinN";
   private int portStart;
   private int portStop;
   private String ip;
   private int string;
   private PropertyChangeSupport propChangeSupport = new SwingPropertyChangeSupport(
         this);
   private int portInUseNumber;

   public PortScanner(int portStart, int portStop, String ip) {
      this.portStart = portStart;
      this.portStop = portStop;
      this.ip = ip;

   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      propChangeSupport.addPropertyChangeListener(listener);
   }

   public void scan() {
      for (int i = portStart; i <= portStop; i++) {
         try {
            Socket ServerSok = new Socket(ip, i);

            setPortInUseNumber(i);
            // System.out.println("Port in use: " + i);

            ServerSok.close();
         } catch (UnknownHostException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }
         setStrinN(i);
         System.out.println("Port not in use: " + i);
      }
   }

   public void setStrinN(int i) {
      // TODO do something with this?
   }

   public void setPortInUseNumber(int i) {
      int oldValue = portInUseNumber;
      int newValue = i;
      portInUseNumber = i;
      propChangeSupport.firePropertyChange(PORT_IN_USE_NUMBER, oldValue,
            newValue);
   }

}

您需要能够访问您的 JText Field 实例, 有两个方法可以做到这一点:

  • pass a variable to the PortScanner class as a local variable
  • have a static reference to the frame that contains the textfield (or to the textfield itself) so that you can access it from wherever you want

第一种方法更正确,因为它保持封装,第二种方法更简单、简单,但是当不需要田地时,也可以将田地暴露在外部,而在OOP环境中,这是设计上的错误选择。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签