java - JDialog 状态的 Action 监听器

标签 java swing jbutton actionlistener jdialog

当我单击 JButton 时,我想更改 JDialog 中标签的文本,因为标签位于另一个类上,我不知道如何从框架类访问它。所以我想出了一个 Action 监听器的想法来检查对话框的状态。 - 当 JDialog 可见时,检索此数据并将此数据设置为标签。 这可能吗?

这是我的房间等级的代码。

public void rooms()
    {
        bh = new ButtonHandler();
        presidentialRoom = new JButton[presidentialRoomNo.length];      
        deluxeRoom = new JButton[deluxeRoomNo.length];
        classicRoom = new JButton[classicRoomNo.length];
        for(int x = 0;x<classicRoomNo.length;x++){
            //classic rooms
            ImageIcon imageC = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\classicRooms.JPG"); // image
            classicRoom[x] = new JButton(classicRoomNo[x],imageC);
            classicRoom[x].setBackground(Color.WHITE);
            classicRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            classicRoom[x].addActionListener(bh);
            classicSubPanel.add(classicRoom[x]);
            //deluxe rooms
            ImageIcon imageD = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\deluxeRooms.JPG"); // image
            deluxeRoom[x] = new JButton(deluxeRoomNo[x],imageD);
            deluxeRoom[x].setBackground(Color.WHITE);
            deluxeRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            deluxeRoom[x].addActionListener(bh);
            deluxeSubPanel.add(deluxeRoom[x]);
            //presidential rooms
            ImageIcon imageP = new ImageIcon("C:\\Users\\John\\workspace" +
                    "\\SystemTest\\src\\Images\\presidentialRooms.JPG"); // image
            presidentialRoom[x] = new JButton(presidentialRoomNo[x],imageP);
            presidentialRoom[x].setBackground(Color.WHITE);
            presidentialRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            presidentialRoom[x].addActionListener(bh);
            presidentialSubPanel.add(presidentialRoom[x]);

        }
    }

这里的每个按钮都访问 RoomProfile 类

public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {       
            RoomProfile rooms = new RoomProfile();
                room.setVisible(true);
        }
    }

这是 RoomProfile 中的一段代码:

public void createLabels()
    {
        labels = new JLabel[topTextLabels.length];
        inputLabels = new JLabel[topTextLabels.length];
        for(int x = 0; x<topTextLabels.length;x++)
        {
            labels[x] = new JLabel(topTextLabels[x]);
            labels[x].setForeground(Color.WHITE);
            inputLabels[x] = new JLabel("test");
            inputLabels[x].setForeground(Color.WHITE);
        }
    }

当我点击房间类中的按钮时,我想要更改的文本是“inputLabels[]”,我想让用户看到该房间的配置文件。

输入标签将显示数据库中的数据。

最佳答案

在制作自定义对话框窗口时,我通常会扩展 JDialog,然后按照 AKJ 的建议添加我需要的任何功能。

通常,对话框中需要显示的内容应在显示之前确定。对话框通常是模态的(这意味着当可见时,用户界面的其他部分都不能使用)。您可以将其设置为非模态,但它们通常是模态的这一事实说明了对话框通常的使用方式。用户选择一些选项,单击“确定”,然后对话框就消失了。如果对话框要保持可见并且是用户界面的主要组件,那么我认为使用 JFrame 而不是 JDialog 更有意义。

要检查对话框是否可见,请使用 if(yourDialog.isVisible())

下面是我编写的非模式对话框的示例:

/**
 * This class allows for some plain text (a note) to be placed into a ScrollPane inside
 * a dialog.  The dialog is non-modal.
 * @author L. LaSpina
 */
public class NoteViewDialog extends javax.swing.JDialog {

    int returnValue;
    public static final int OKPRESSED = 1;
    public static final int CANCELPRESSED = 2;

    /** Creates new form NoteDialog */
    public NoteViewDialog(java.awt.Frame parent, String note) {
        super(parent, false);
        initComponents();
        this.setTitle("Faculty Assignment Summary");
        setLabel("Error List");
        setNote(note);
        setSize(500,300);       
    }

    public void setNote(String s) {
        textArea.setText(s);
    }
    public String getNote() {
        return textArea.getText();
    }

    public void setLabel(String s) {
        headingLabel.setText(s);
    }

    public int getReturnValue() {
        return returnValue;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        headingLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textArea.setColumns(20);
        textArea.setLineWrap(true);
        textArea.setRows(5);
        textArea.setWrapStyleWord(true);
        scrollPane.setViewportView(textArea);

        getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

        okButton.setText("Close");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.PAGE_END);

        headingLabel.setText("Error Report");
        getContentPane().add(headingLabel, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>                        

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.dispose();
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JLabel headingLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
    // End of variables declaration                   
}

关于java - JDialog 状态的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12772861/

相关文章:

java - panel.getVisible?

java - 难以通过 JButton 激活 BufferedReader

java - 如何在 Swing 应用程序中编写更改窗口的逻辑?

java - 将一组文本按钮添加到网格布局

java - 带有设置按钮的默认操作栏

java - 使用java将多个xml文件转换为html文件

java - recyclerview inside nestedscrollview addOnScrollListener (endless scrolllistener)

java - 强制 JPA 接受外键值而不仅仅是实体

java - 如何使用 Windows Builder Pro 创建数据库绑定(bind)?

java - 访问在方法中创建的 Swing 组件