我想在单击JButton时更改JDialog中标签的文本,因为标签在另一个类上,我不知道如何从框架类访问它。因此,我想到了一种检查对话框状态的动作侦听器。
-当JDialog可见时,检索此数据并将setText设置为标签。
这可能吗?

这是我房间课程的代码。

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的建议添加所需的任何功能。

通常,在显示对话框之前,应先确定需要在对话框中显示的内容。对话框通常是模式对话框(表示可见时,不能使用用户界面的其他部分)。您可以使其成为非模态的,但是它们通常是模态的事实说明了对话框的使用方式。用户选择一些选项,单击“确定”,对话框消失了。如果Dialog保持可见并且是用户界面的主要组成部分,那么我认为使用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
}

10-05 17:56