【问题标题】:jframe actionperformed jframejframe actionperformed jframe
【发布时间】:2012-03-13 18:44:58
【问题描述】:

我目前有一个按钮,当单击该按钮时,它会执行一个方法,该方法创建一个带有可加载多个图像的面板的 jframe。如果多次单击此按钮,图像会不断添加到加载到 jframe 上的现有图像上。我应该使用什么代码,以便如果在加载 jframe 和元素后单击按钮,单击一次后,不会添加任何其他内容。

非常感谢

【问题讨论】:

    标签: java image swing jframe jbutton


    【解决方案1】:

    怎么样:

    public void actionPerformed(ActionEvent evt) {
      button.setEnabled(false);
    
      // the other code that creates imgFrame
    
      imgFrame.addWindowListener(new WindowAdapter() {
          @Override public void windowClosing(WindowEvent evt) {
              button.setEnabled(true);
          }});
    
      imgFrame.setVisible(true);
    }
    

    【讨论】:

    • 你只需要记住在另一个 JFrame 关闭时重新启用它。
    • @jschoen 是的,你知道。我在示例中添加了一种方法。谢谢。
    【解决方案2】:
    1. 不要在运行时创建大量 JFrames,因为这些 Object 从未从 Used JVM Memory 消失,直到当前 JVM 实例存在

    2. 您已经看到 CardLayout 非常舒适地解决了您的多个视图问题(在本例中为一个 JFrame

    3. 把图片作为Icon/ImageIcon放到JLabel

    【讨论】:

    • 它只创建一个jframe,每次点击按钮都会将图片添加到一个jframe
    • 将图像作为 jlabels 获取不是这里的问题
    • 我在这里说的是错误的经历、空白的过道和解决方案如何做......,一切由你决定
    • 随机项目符号,与问题无关(afaics)
    【解决方案3】:

    框架出现时禁用按钮,框架关闭时启用按钮。

    public void actionPerformed(ActionEvent evt) {
    
        final JButton finalButton = button;
        button.setEnabled(false);
        JFrame frame = new JFrame()
        {
            protected void processWindowEvent(WindowEvent e)
            {
                if (e.getID() == WindowEvent.WINDOW_CLOSING)
                {
                    finalButton.setEnabled(true);
                }
                super.processWindowEvent(e);
            }
        };
        frame.setVisible(true);
     }
    

    【讨论】:

      【解决方案4】:

      我建议程序启动时为假的布尔值,然后当单击按钮时,它会测试该布尔值是否为假。如果它是假的,那么创造你想要的东西,然后让它成为真的。如果是真的,什么也不做,或者提醒用户不要再点击按钮一次,或者类似的事情

      【讨论】:

        【解决方案5】:
        boolean isAlreadyCreated = false;
        
        yourButton.addActionListener(new ActionListener()
        {
            if(!isAlreadyCreated)
            {
                //CREATE YOUR NEW FRAME
                isAlreadyCreated = true;
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2011-10-17
          • 2013-06-21
          • 2012-05-13
          • 1970-01-01
          • 1970-01-01
          • 2011-01-26
          • 2016-01-16
          • 2023-03-29
          • 1970-01-01
          相关资源
          最近更新 更多