java表单按钮顺序执行

发布于 2024-10-01 19:20:01 字数 684 浏览 0 评论 0原文

我编写了一个方法来创建一个表单(3 个按钮和一个文本框),然后我在 main 中调用它。 但是当我运行程序时,在我在表单中输入信息之前(方法 form6 ), 执行的其他命令! “s4 和 ontname 已填写在表格中”。 这是我的代码的一部分:::::::::::

////////////////////////////////////////////////////////////////////////// 
public static void main(String[] args) {
System.out.println("*begin main*"); // call form method 

String s4= form6(); // s4 由方法返回。 System.out.println("s3333*"+s4);
System.out.println("ont
:"+ontname);//它是全局的 } /////////////////////////////////////////////////////////// /////////////////////////

我有 2 个问题:::

1--- 表单运行时,会执行其他命令! 他们的订单执行情况如何? 2. --- 我想定义一个按钮,当我单击它时,它会关闭表单。

谢谢大家。

i write a method to create a form(3 buttons and a textBox), then i call it in main.
but when i run program, before i enter information in the form (method form6 ),
Other commands that are executed! "s4 and ontname chenged in the form".
this is a part of my code:::::::::::

////////////////////////////////////////////////////////////////////////// 
public static void main(String[] args) {
System.out.println("*begin main*"); // call form method 

String s4= form6(); // s4 is returned by method.
System.out.println("s3333*"+s4);
System.out.println("ont
:"+ontname);//it's global }
//////////////////////////////////////////////////////////////////////////

i have 2 questions:::

1--- While the form is running, other commands are executed!
What is their order execution?
2. --- i want to define a button to when i click it,it closes the form.

thanks all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福丶如此 2024-10-08 19:20:01

如果我正确地获取了您的代码,则 ontname 是 (1) 类成员(在方法外部声明)或 (2) 局部变量,该变量在包含此代码片段的方法中声明。

在这两种情况下,都不需要“返回”ontname,因为它没有在匿名 ActionListener 实例中声明。

以下示例说明了此问题的典型模式:

public void someMethod() {
  // ...
  button2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
      String filename = File.separator+"c:";
      JFileChooser fc = new JFileChooser(new File(filename));
      fc.showOpenDialog(null);
      File selFile = fc.getSelectedFile();
      setOntName(selFile.getPath());  // <-- here we call another method
    }
  });
  // ...
}

void setOntName(String ontName) {
  // do something with ontName
}

或者:将 ontName 声明为静态类成员(仅):

private static String ontName = "";  // <-- accessible from main method
public static void main(String[] args) {
  // ...
}
// more methods.

If I get your code correctly, ontname is either (1) a class member (declared outside a method) or (2) a local variable, which is declared in the method that contains this code snippet.

In both cases there is no need to "return" ontname just because it is not declared inside the anonymous ActionListener instance.

The following example illustrates a typical pattern for this problem:

public void someMethod() {
  // ...
  button2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
      String filename = File.separator+"c:";
      JFileChooser fc = new JFileChooser(new File(filename));
      fc.showOpenDialog(null);
      File selFile = fc.getSelectedFile();
      setOntName(selFile.getPath());  // <-- here we call another method
    }
  });
  // ...
}

void setOntName(String ontName) {
  // do something with ontName
}

Alternativly: declare ontName as a static class member (only):

private static String ontName = "";  // <-- accessible from main method
public static void main(String[] args) {
  // ...
}
// more methods.
王权女流氓 2024-10-08 19:20:01

您无法在此方法中返回值,因为 ActionListenerInterface 不允许这样做。但是您可以从 actionPerformed() 方法中调用另一个方法并将 ontname 传递给它。

您还可以关闭新方法中的第三个按钮。或者将第三个按钮定义为final 并在actionPerformed() 方法中使用它。

例如

button2.addActionListener(new ActionListener(){ 
           public void actionPerformed(ActionEvent e){
           String filename = File.separator+"c:";
           JFileChooser fc = new JFileChooser(new File(filename));
           fc.showOpenDialog(null);
           File selFile = fc.getSelectedFile();
           ontname=selFile.getPath();
           System.out.println("filepath:  "+ontname); //it works correctly.  
           anotherMethod(ontname);   
         }
      });

private void anotherMethod(String path) {
        //doSomething with the path

        //close third button here
}

You can't return a value in this Method because the ActionListenerInterface does not allow this. But you can call another method from within the actionPerformed() method and pass the ontname to it.

You can also close the third button in the new method. Or define the third button as final and use it in the actionPerformed() method.

E.g.

button2.addActionListener(new ActionListener(){ 
           public void actionPerformed(ActionEvent e){
           String filename = File.separator+"c:";
           JFileChooser fc = new JFileChooser(new File(filename));
           fc.showOpenDialog(null);
           File selFile = fc.getSelectedFile();
           ontname=selFile.getPath();
           System.out.println("filepath:  "+ontname); //it works correctly.  
           anotherMethod(ontname);   
         }
      });

private void anotherMethod(String path) {
        //doSomething with the path

        //close third button here
}
凶凌 2024-10-08 19:20:01

您可以将变量 ontname 定义为全局变量,在函数之外:

var ontname = null;
button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
// ...
    ontname=selFile.getPath();
  }
});

// ...

System.out.println("filepath:  "+ontname);

You could probably define your variable ontname as global, outside of your function:

var ontname = null;
button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
// ...
    ontname=selFile.getPath();
  }
});

// ...

System.out.println("filepath:  "+ontname);
暮倦 2024-10-08 19:20:01

如果您想记住这些值,那么它们应该是类级别变量。

但是,通常,您希望将它们传递给其他方法来对它们进行一些处理(或者,例如,将它们保留在文件中)。您可以将它们作为参数传递给其他方法。

(大多数情况下第二个更好,我对你的应用了解不多,所以无法给出答案)

你的代码还存在其他问题:

  1. 你需要检查用户是否点击了在打开的对话框中通过“确定”或“取消”按钮来决定是否获取文件。

  2. String filename = File.separator+"c:"; 确实没有意义。也许您的意思是 String filename = "c:"+File.separator; 但即使这样也不是很有用。 File.separator 用于获取特定于平台的文件分隔符字符(Windows 中的 \,Linux 上的 /),但由于您是硬编码 c:,无论如何,您都将您的应用程序限制在 Windows 上。您可能想要一种更好的独立于平台的方式(从“默认”路径开始,不带参数的 new JFileChooser() ,然后记住用户上次使用的路径,并从那里继续)

  3. 如果 showOpenDialog 方法的参数是您的父框架,则对话框将以父框架为中心,并且在大多数情况下看起来会更好。

您可能还想重新查看变量名称。

button2.addActionListener(new ActionListener()
{    
   public void actionPerformed(ActionEvent e)
   {
      String filename = File.separator+"c:";
      JFileChooser fc = new JFileChooser(new File(filename));
      int option = fc.showOpenDialog(null);
      if(option = JFileChooser.APROVE_OPTION)
      {
         File selFile = fc.getSelectedFile();
         String ontname=selFile.getPath();
         System.out.println("filepath: "+ontname); //it works correctly.
         doSomeOperation(ontname); //Or, declare ontname as a class level variable.
      }
  }
}); 

If you want to remember the values, then they should be class level variables.

But, generally, you would want to pass these to some other method to do some processing on them (or, say, persist them in a file). You can pass these as parameters to the other method.

(The second one is better in most cases, I don't know much about your app, so I am unable to give one answer)

There are other problems with your code:

  1. You need to check whether the use has clicked the "Ok" or "Cancel" button in the open dialog to decide whether to get the file or not.

  2. String filename = File.separator+"c:"; does not really make sense. Perhaps you meant String filename = "c:"+File.separator; But even this is not very useful. File.separator is for getting the platform specific file separator char (\ in Windows, / on linux) but since you are hard coding c:, you are anyway constrainting your app to Windows. You might want to have a better platform independent way (start at the "default" path, new JFileChooser() without arguments, and then remember the path the user last used, and proceed from there)

  3. If the argument to the showOpenDialog method is your parent frame, then the dialog would be centered on the parent frame, and would, in most cases, look nicer.

You might also want to relook your variable names.

button2.addActionListener(new ActionListener()
{    
   public void actionPerformed(ActionEvent e)
   {
      String filename = File.separator+"c:";
      JFileChooser fc = new JFileChooser(new File(filename));
      int option = fc.showOpenDialog(null);
      if(option = JFileChooser.APROVE_OPTION)
      {
         File selFile = fc.getSelectedFile();
         String ontname=selFile.getPath();
         System.out.println("filepath: "+ontname); //it works correctly.
         doSomeOperation(ontname); //Or, declare ontname as a class level variable.
      }
  }
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文