禁用 jToggleButton [英] Disabling a jToggleButton

查看:27
本文介绍了禁用 jToggleButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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); 但每次我回到菜单并回到座位选择器时,按钮仍然没有被禁用.我想要做的是让它禁用甚至回到主菜单后,所以当我回到座位选择器时,我不能再选择这个座位了.

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);
    }

}                                        

请检查一下

推荐答案

好吧,JToggleButton 的启用"属性很可能不会将自身重置为 true当您将其设置为 false 时.我认为您实际上正在查看 other JToggleButton 的实例.也许您每次访问选择座位"GUI 时都会重新创建它?在这种情况下,您想要的是将应用程序数据(您可以在其中了解已预订座位的信息等)与用户界面的关联.

Well, the JToggleButton's "enabled" property most probably doesn't reset itself to true when you set it to false. I think that you actually are looking at an other JToggleButton's instance. Maybe you re-create it every time you access the "select seat" GUI? In that case, what you want is to dissociate the application-data (where you have the information about which seat are already reserved etc) from the user interface.

我认为这样做的一个好方法是使用模型-视图-控制器模式.让我向您介绍这种设计模式:

A good way to do so, I believe, is to work with the Model-View-Controller pattern. Let me introduce you to this design pattern:

  • Model 保存您的应用程序数据(例如,可以是 Set)
  • View 用于显示您的数据(即您的 JToggleButton)
  • Controller 是更新模型的入口点(在您的情况下,它们也是 JToggleButton)
  • the Model holds your application data (e.g., could be a Set<Seat>)
  • the View is use to display your data (i.e. your JToggleButton)
  • the Controller is the entry point from which your Model is updated (in your case, those are also the JToggleButton)

您可以在此处阅读更多相关信息:MVC 模式 - 教程点.

You can read more about it there: MVC Pattern - tutorialspoint.

基本上,不是每次都创建一个新的空"视图,而是希望使用数据容器(模型)包含的值加载它.然后,当您单击代表座位(控制器)的某个 JToggleButton 时,您不会直接更改按钮的状态,而只是简单地更新您的 Model 以切换您座位的状态(保留或不保留).然后,每次更新模型时,您的视图都会使用可用的新数据进行刷新.就这样!

Basically, instead of creating a new "empty" View every time, you want to load it with the values contained by your data container (the Model). Then, when you click on some JToggleButton that represents a seat (a Controller), you don't directly change the state of the button, but you just simply update your Model to toggle the state (reserved or not) of your seat. Then, each time the Model is updated, your View refreshes using the new data available. That's it!

+--------------------------------------+
|    +-----------+                     |
|    |   Model   <-------+             |
|    +-----v-----+       |             |
|          |             |             |
|          |             |             |
|    +-----v---+---------^----------+  |
|    |  View   |     Controller     |  |
|    +---------+--------------------+  |
|                                      |
+---------- Your application ----------+

随意询问更多关于它的信息,它可能看起来真的有很多变化,但它具有许多有趣的设计功能:)
特别是如果您需要另一个视图(例如,一个文件,每次进行更改时都希望在其中保存数据)!

Feel free to ask more about it, it might seem really like a lot to change but it comes with many interesting design features :)
Especially if you need to have another View (for example, a File in which you would like to save your data every time a change is made)!

这篇关于禁用 jToggleButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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