可以在整个执行过程中禁用jToggleButton吗? [英] Disabling a jToggleButton throughout entire execution, possible?

查看:165
本文介绍了可以在整个执行过程中禁用jToggleButton吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计,所以我正在制作这个简单的电影票务系统
我的程序流程如下,所有页面都在不同的JFrame中:主菜单>选择日>选择电影>选择座位>返回MainMenu

Hey guys so I'm making this simple movie ticketing system My program flow is as follow and all pages are in different JFrames: Main Menu>Choose Day>Select movie>select seat>back to MainMenu

我在座位选择器中使用 JToggle 。一旦选定,是否可以在整个执行过程中禁用切换按钮?我正在使用 JToggleButton.setEnabled(false); 但每次我回到菜单并回到座位选择器时,按钮仍然没有被禁用。我想做什么即使在我回到MainMenu之后也要禁用它,所以当我回到座位选择器时,我不能再选择这个座位了。

I'm using JToggle in the seat chooser. Is it possible to disable the toggle button throughout entire execution once selected? I'm using JToggleButton.setEnabled(false); but everytime I go back to menu and come back to seat chooser,the button is still not disabled .what I want to do is to have it disable even after I'm back to MainMenu, so when I go back to seat chooser, I can't select this seat anymore.

以下是一些其中的代码:

Below are some codes in it:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    this.setVisible(false);
    MainSelection s =  new MainSelection();
    s.setVisible(true);

     if(jToggleButton1.isSelected())

    {
        jToggleButton1.setEnabled(false);
    }

    if(jToggleButton2.isSelected())

    {
        jToggleButton2.setEnabled(false);
    }

    if(jToggleButton3.isSelected())

    {
        jToggleButton3.setEnabled(false);
    }

}                                        

请查看

推荐答案

您似乎正在重新创建GUI,每次显示时都会显示切换按钮,您不应该这样做。

You seem to be re-creating the GUI that displays your toggle buttons each time it is displayed, and you should not be doing this.

相反


  • 为此窗口创建变量

  • 考虑以懒惰的方式创建它 - 当且仅当它为空时创建它

  • 否则如果不是null并且需要显示,只需通过 setVisible(true)

  • 并反过来在需要时通过 setVisible(false)使其隐藏

  • 不要在您的应用程序中显示多个JFrame。相反,应用程序应该有一个主JFrame,然后你可以让它启动对话框窗口,如JDialogs,如果合适,或者在适当的时候通过CardLayout交换views。

  • create a variable for this window
  • consider creating it in a lazy way -- create it if and only if it is null
  • otherwise if not null and it needs to show, simply make it visible via setVisible(true).
  • and conversely make it invisible when needed via setVisible(false).
  • don't show multiple JFrames in your application. Instead the application should have one main JFrame, and then you can have it launch dialog windows, such as JDialogs, if appropriate, or swap "views" via a CardLayout if appropriate.

具体来说:


  • 使MainSelection变量,s,类的实例字段 - 声明它并在课堂上初始化一次

  • 仅在此方法中将其设置为可见。不要创建一个新的。

  • 将来,不要向用户吐出一堆JFrame,因为它是一个糟糕且烦人的用户界面。而是阅读CardLayout教程(谷歌将帮助您找到它),并使用它。使你的代码转向创建JPanels,而不是JFrames。

编辑

你问:

Edit
You ask:


我真的需要帮助Make MainSelection变量,s,类的实例字段 - 声明它并在课堂上初始化一次。仅在此方法中将其设置为可见。不要创建一个新的。我如何使它成为实例字段?我也可以在主选择表格或座位选择表格中声明它吗?

I really need help Make MainSelection variable, s, an instance field of the class -- declare it and initialize it once in the class. Only set it visible in this method. Don't create a new one. How do I make it instance field?? Also do I declare it at mainselection form or the seatselection form?

您正在执行以下操作:

public class Foo {

  private void someMethod() {
    // the code below creates a new SomeClass instance each time the method is called
    SomeClass localVariable = new SomeClass();
    localVariable.setVisible(true);
  }
}

我建议您这样做:

public class Foo {
  // the code below creates a SomeClass instance only *once*.
  private SomeClass instanceField = new SomeClass();

  private void someMethod() {
    instanceField.setVisible(true);
  }
}

另外,你应该对这个重复的帖子做些什么你的:

Also, you should do something about that duplicate post of yours:


  • 首先关闭它 - 你不应该有多个同样的问题 - 这对我们和其他人来说都不公平。

  • 并接受并在其他帖子中对答案进行投票,以表达对海报帖子的努力和乐于助人的赞赏。

这篇关于可以在整个执行过程中禁用jToggleButton吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆