2012-10-07 16 views
1

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

这是我的房间类的代码。

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 [] “当单击房间课程中的按钮时,我想让用户看到该房间的配置文件。

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

+0

为什么你不能从访问类的JLabel JDialog类?为什么不能递归循环JDialog#getComponents()直到找到JLabel? – DankMemes

+0

@ZoveGames如果JLabel在另一个类上,我怎么能设置文本?从我的框架上的动作侦听器?请演示 – NOOBprogrammer

+0

正确的处理方法是在Dialog类中提供一个方法来更新标签。您可以检查对话框是否可见,然后调用该方法。 –

回答

2

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

通常,在显示对话框之前,需要确定需要显示的任何需求。对话框通常是模态的(也就是说,当可视化时,不能使用用户界面的其他部分)。您可以将其设置为非模态,但它们通常是模态的事实说明了对话框通常使用的方式。用户选择一些选项,点击o.k.,对话框消失。如果对话框保持可见并且是用户界面的主要组件,那么我认为使用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     
}