简体   繁体   English

java gui swing用户操作输入

[英]java gui swing manipulating input by user

I'm new to GUI. 我是GUI的新手。 I'm trying to create a gui for an already made java program I have. 我正在尝试为我已有的Java程序创建GUI。 I want the user to enter the port number and location of file, and from there I want to use my already made program to do the rest. 我希望用户输入文件的端口号和位置,然后我要使用已经制作好的程序来完成其余的工作。 I'm confused about how I will get the values from the user input and implement to the program. 我对如何从用户输入中获取值并将其实现到程序感到困惑。

This is the framework of my program 这是我程序的框架

public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener  {


   private Label lblPort;    // declare component Label
   private TextField tfPort; // declare component TextField    
   private int port;     // port number



   /** WindowEvent handlers */
   // Called back upon clicking close-window button
   @Override
   public void windowClosing(WindowEvent e) {
      System.exit(0);  // terminate the program
   }




   //constructor for frame
   public TcpServerCompareCSV () {
      setLayout(new FlowLayout());
         // "this" Frame sets its layout to FlowLayout, which arranges the components
         //  from left-to-right, and flow to next row from top-to-bottom.

      lblPort = new Label("Port"); // construct Label
      add(lblPort);                   // "this" Frame adds Label

      tfPort = new TextField("0", 10); // construct TextField
      tfPort.setEditable(true);       //edit text
      add(tfPort);                     // "this" Frame adds tfCount



      tfPort.addActionListener(this); // for event-handling

      setTitle("compare");  // "this" Frame sets title
      setSize(250, 100);        // "this" Frame sets initial window size
      setVisible(true);         // "this" Frame shows


      addWindowListener(this);
        // "this" Frame fires WindowEvent its registered WindowEvent listener
        // "this" Frame adds "this" object as a WindowEvent listener

   }




   /** ActionEvent handler - Called back when user clicks the button. */
   @Override
   public void actionPerformed(ActionEvent evt) {
    // Get the String entered into the TextField tfPort, convert to int
      port = Integer.parseInt(tfPort.getText());

   }





   /** The entry main() method */

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

      // Invoke the constructor to setup the GUI, by allocating an instance
    TcpServerCompareCSV app = new TcpServerCompareCSV();

Your code is incomplete, so I'm having to guess about a few things, esp. 您的代码不完整,所以我不得不猜测一些事情,尤其是。 about what your "already-made" program looks like. 关于您的“现成”程序的外观。

Let's say you have a command-line program in a class that starts out something like: 假设您在一个类中有一个命令行程序,它的开始类似:

public class AlreadyMade
{
  public static void main(String[] arguments)
  {
    AlreadyMade am = new AlreadyMade();
    am.goToIt(arguments[0], arguments[1]);
  }

  public void goToIt(String s1, String s2)
  {
    // insert logic here for what to do with your port number and location
  }
}

In that case, you can invoke the goToIt() method from your GUI, possibly from the actionPerformed method, the same way you invoke it from main in your AlreadyMade class: 在这种情况下,您可以从GUI中调用goToIt()方法,也可以从actionPerformed方法中调用,方法与从AlreadyMade类中的main调用方法相同:

...
AlreadyMade am = new AlreadyMade();
am.goToIt(tfPort.getText(), tfFileLocation.getText());  // assume tfFileLocation, etc.
...

I'm also having to guess that this is what you wanted to know; 我还不得不猜测这就是您想知道的。 if not, at least you know what it LOOKS like you wanted to know and can refine the question. 如果不是,至少您知道您想知道的内容,并且可以完善问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM