且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

新 Jframe 关闭时更新 Jlist

更新时间:2022-11-01 07:56:38

我认为您可能需要考虑创建一个新的自定义对话框,该对话框在您选择按钮时显示.这是我随身携带的一段示例代码以供参考.这里的主要项目是显示对话框的静态方法,以及对话框是模态的,因此执行暂停"直到您关闭对话框,然后允许您捕获对话框的保存数据并从静态方法返回它显示对话框.将此用作模板并根据需要进行修改.更具体地说,响应"是从方法返回的值.实际上,响应"不会是一个简单的布尔值(我只是用它来测试逻辑),而是包含您从对话框的输入控件收集的所有信息的 listClass.对 getUserInput() 的调用是您要从主 JFrame 代码执行以开始滚动的操作.actionPerformed() 方法是您从对话框控件中获取数据并填充包含返回信息的类的地方.

I think you might want to consider creating a new custom dialog box that displays when you select the button. Here's a sample piece of code I keep handy for reference. The main items here are the static method that displays the dialog, and the fact that the dialog is modal, so execution "pauses" until you close the dialog, allowing you to then capture the dialog's saved data and return it from the static method that displays the dialog. Use this as a template and modify as needed. More specifically, "response" is the value returned from the method. In reality, "response" will not be a simple boolean, ( I just used that to test the logic), but your listClass that contains all the information you collected from the dialog's input controls. The call to getUserInput() is what you'll want to do from your main JFrame code to start the ball rolling. The actionPerformed() method is where you'll grab the data from the dialog box controls and populate the class that contains the return info.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ConfirmDialog extends JDialog implements ActionListener
{   
        private boolean response = false;
        private JButton btnOK = new JButton("OK");
        private JButton btnCancel = new JButton("Cancel");
        private JPanel contentPane = new JPanel();

        public static boolean getUserInput()
        {
                    return new ConfirmDialog().showDialog();

        }

        private boolean showDialog()
        {
                    setVisible(true);
                    //next line executes only after dialog is disposed,                 since dialog is modal.
                    return response;
        }

        private ConfirmDialog()
        {
                         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                    btnOK.setActionCommand("OK");
                    btnCancel.setActionCommand("Cancel");
                    btnOK.addActionListener(this);
                    btnCancel.addActionListener(this);
                    contentPane.add(btnOK);
                    contentPane.add(btnCancel);
                    setContentPane(contentPane);
                    setModal(true);
                    pack();

        }
        public void actionPerformed(ActionEvent e)
        {
                    if(e.getActionCommand().equals(btnOK.getActionCommand()))
                    {
                                response = true;
                    }
                    setVisible(false);
                    dispose();
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                    // TODO Auto-generated method stub
                    System.out.println(getUserInput());

        }

 }