1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| public class MyFrame extends JFrame {
private JLabel nomLabel;
private JButton nomButton = new JButton("Fenetre Fille");
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
nomLabel = new JLabel();
nomLabel.setPreferredSize(new Dimension(150, 19));
getContentPane().add(nomButton, BorderLayout.NORTH);
getContentPane().add(nomLabel, BorderLayout.CENTER);
nomButton.addActionListener(new NomButtonListener(this));
pack();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(
(screenSize.width - this.getWidth()) / 2,
(screenSize.height - this.getHeight()) / 2);
show();
}
class NomButtonListener implements ActionListener {
MyDialog nomDialog;
public NomButtonListener(JFrame parent) {
nomDialog = new MyDialog(parent);
}
public void actionPerformed(ActionEvent arg0) {
nomDialog.show();
if (nomDialog.getReturnStatus() == MyDialog.RET_OK) {
nomLabel.setText(nomDialog.getNomEntre());
}
}
} //:-
class MyDialog extends javax.swing.JDialog {
/** A return status code - returned if Cancel button has been pressed */
public static final int RET_CANCEL = 0;
/** A return status code - returned if OK button has been pressed */
public static final int RET_OK = 1;
private String nomEntre;
public MyDialog(java.awt.Frame parent) {
super(parent, true);
initComponents();
myInit();
}
public void myInit() {
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(
(screenSize.width - this.getWidth()) / 2,
(screenSize.height - this.getHeight()) / 2);
} //myInit
/** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
public int getReturnStatus() {
return returnStatus;
}
/** 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.
*/
private void initComponents() {
buttonPanel = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
setTitle("Entrez votre nom");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
buttonPanel.setLayout(
new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
buttonPanel.setBackground(new java.awt.Color(51, 102, 255));
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
buttonPanel.add(okButton);
cancelButton.setText("Cancel");
cancelButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
buttonPanel.add(cancelButton);
getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
textField.setPreferredSize(new Dimension(150, 19));
getContentPane().add(textField, java.awt.BorderLayout.CENTER);
pack();
}
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
nomEntre = textField.getText();
doClose(RET_OK);
}
private void cancelButtonActionPerformed(
java.awt.event.ActionEvent evt) {
doClose(RET_CANCEL);
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
doClose(RET_CANCEL);
}
private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
dispose();
}
// Variables declaration - do not modify
private javax.swing.JPanel buttonPanel;
private JTextField textField = new JTextField();
private javax.swing.JButton cancelButton;
private javax.swing.JButton okButton;
// End of variables declaration
private int returnStatus = RET_CANCEL;
/**
* @return
*/
public javax.swing.JButton getOkButton() {
return okButton;
}
/**
* @param panel
*/
public void setButtonPanel(javax.swing.JPanel panel) {
buttonPanel = panel;
}
/**
* @param button
*/
public void setCancelButton(javax.swing.JButton button) {
cancelButton = button;
}
/**
* @param button
*/
public void setOkButton(javax.swing.JButton button) {
okButton = button;
}
/**
* @param i
*/
public void setReturnStatus(int i) {
returnStatus = i;
}
/**
* @return
*/
public String getNomEntre() {
return nomEntre;
}
} //:-
} ///:- |
Partager