当我点击JButton时,我想更改JDialog中标签的文本,因为标签在另一个类上,我不知道如何从帧类中访问它。所以我想出了一个动作监听器的概念,它检查对话框的状态。 - 当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 []”,只要点击房间类中的按钮我想让用户看到该房间的个人资料。
输入标签将显示数据库中的数据。
答案 0 :(得分:2)
在制作自定义对话框窗口时,我通常会扩展JDialog,然后按照AKJ的建议添加我需要的任何功能。
通常,在显示之前,应确定对话框中需要显示的内容。对话框通常是模态的(意味着当可见时,不能使用用户界面的其他部分)。你可以使它成为非模态的,但它们通常是模态的这一事实说明了通常使用对话框的方式。用户选择一些选项,单击o.k.,对话框消失。如果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
}