Java JDialog状态的Actionlistener

Java JDialog状态的Actionlistener,java,swing,jbutton,actionlistener,jdialog,Java,Swing,Jbutton,Actionlistener,Jdialog,我想在单击JButton时更改JDialog中标签的文本,因为标签位于另一个类上,我不知道如何从frame类访问它。所以我想出了一个动作监听器的主意,它可以检查对话的状态。 -当JDialog可见时,检索此数据并将此数据设置为标签。 这可能吗 这是我房间的代码 public void rooms() { bh = new ButtonHandler(); presidentialRoom = new JButton[presidentialRoomNo.l

我想在单击JButton时更改JDialog中标签的文本,因为标签位于另一个类上,我不知道如何从frame类访问它。所以我想出了一个动作监听器的主意,它可以检查对话的状态。 -当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 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);
        }
    }
public void createLabels()
{
标签=新的JLabel[topTextLabels.length];
inputLabels=新的JLabel[topTextLabels.length];

对于(intx=0;x在创建自定义对话框窗口时,我通常会扩展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                   
}
/**
*此类允许将一些纯文本(注释)放置到内部的滚动窗格中
*对话框。该对话框是非模态的。
*@作者L.拉斯皮纳
*/
公共类NoteViewDialog扩展了javax.swing.JDialog{
返回值;
公共静态最终整数=1;
公共静态最终int=2;
/**创建新的表单注释对话框*/
公共NoteViewDialog(java.awt.Frame父对象,字符串注释){
超级(父,假);
初始化组件();
本课程名称(“教师任务总结”);
setLabel(“错误列表”);
setNote(注);
设置大小(500300);
}
公共void setNote(字符串s){
textArea.setText(s);
}
公共字符串getNote(){
返回textArea.getText();
}
公共void setLabel(字符串s){
headingLabel.setText(多个);
}
public int getReturnValue(){
返回值;
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
scrollPane=newjavax.swing.JScrollPane();
textArea=newjavax.swing.JTextArea();
buttonPanel=newjavax.swing.JPanel();
okButton=newjavax.swing.JButton();
headingLabel=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE);
textArea.setColumns(20);
textArea.setLineWrap(true);
textArea.setRows(5);
textArea.setWrapStyleWord(true);
scrollPane.setViewportView(文本区域);
getContentPane().add(scrollPane,java.awt.BorderLayout.CENTER);
okButton.setText(“关闭”);
okButton.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
已执行的按钮操作(evt);
}
});
按钮面板。添加(确定按钮);
getContentPane().add(buttonPanel,java.awt.BorderLayout.PAGE_END);
headingLabel.setText(“错误报告”);
getContentPane().add(headingLabel,java.awt.BorderLayout.PAGE_START);
包装();
}//                         
私有void okButtonActionPerformed(java.awt.event.ActionEvent evt){
这个。dispose();
}                                        
//变量声明-不修改
私有javax.swing.JPanel按钮面板;
私有javax.swing.JLabel头标签;
私有javax.swing.JButton-okButton;
私有javax.swing.JScrollPane滚动窗格;
私有javax.swing.JTextArea textArea;
//变量结束声明
}

为什么不能从JDialog类访问JLabel类?为什么不能递归地循环JDialog#getComponents()在找到JLabel之前?@ZoveGames如果JLabel在另一个类上,我如何设置文本?从框架上的我的动作侦听器?请演示正确的处理方法是在对话框类中提供一个方法来更新标签。您可以检查对话框是否可见,然后调用该方法。@AKJ我知道该方法,但c您可以给我一个示例,说明我将在主框架的操作侦听器上设置的条件,以了解对话框是否可见但由于您将自己创建对话框,所以可以在对话框关闭后执行类似于将引用设为null的操作,以便在用户可见时引用为非null。
/**
 * 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                   
}