2

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[]」です。

入力ラベルには、データベースからのデータが表示されます。

4

1 に答える 1

2

カスタム ダイアログ ウィンドウを作成するときは、通常、JDialog を拡張してから、AKJ の提案に従って必要な機能を追加します。

通常、ダイアログに表示する必要があるものは、表示する前に決定する必要があります。通常、ダイアログはモーダルです (つまり、表示されている場合、ユーザー インターフェイスの他の部分は使用できません)。非モーダルにすることもできますが、モーダルであることが多いという事実は、ダイアログ ボックスが通常どのように使用されるかを示しています。ユーザーがいくつかのオプションを選択し、[OK] をクリックすると、ダイアログが消えます。ダイアログが表示されたままになり、ユーザー インターフェイスの主要なコンポーネントである場合は、JDialog の代わりに JFrame を使用する方が理にかなっていると思います。

ダイアログが表示されているかどうかを確認するには、次を使用します 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                   
}
于 2012-10-08T01:23:07.537 に答える