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
|
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestJtfldBtApp extends JFrame
{
JPanel pan;
JTextFieldBt tfb1;
JTextFieldBt tfb2;
JTextArea affichage;
JButton showbutton;
TestJtfldBtApp()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan = new JPanel();
pan.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
// 1er JTextField avec bouton
tfb1 = new JTextFieldBt();
pan.add(tfb1);
// 2eme JTextField avec bouton
tfb2 = new JTextFieldBt();
pan.add(tfb2);
// JTextArea pour affichage resultat sur clic showbutton
affichage = new JTextArea();
affichage.setPreferredSize(new Dimension(200, 100));
pan.add(affichage);
// création boutton affichage
showbutton = new JButton("Display");
pan.add(showbutton);
// listener pour action du showbutton
showbutton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
affichage.append("\nTBF1 <"+tfb1.getText()+">");
affichage.append("\nTBF2 <"+tfb2.getText()+">");
}
});
getContentPane().add(pan);
setSize(300, 300);
setVisible(true);
}
private class JTextFieldBt extends JTextField
{
private JTextField jtf;
private JButton button;
JTextFieldBt()
{
// le constructeur par defaut créé un JTextField de 10 cars
super(null, 10);
jtf = this;
this.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 0));
// création boutton lié au JTextField
button = new JButton("Eff.");
this.add(button);
// listener pour action du boutton
button.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
// exemple action - efface le text du JTextField
jtf.setText("");
}
});
}
}
public static void main(String[] args)
{
new TestJtfldBtApp();
}
} |
Partager