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
|
public class FormulaCalculatorDialog extends JDialog {
private String formula;
private JTextField formulaText;
private boolean isOk = false;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem saveItem;
private JMenuItem loadItem;
private JMenu helpMenu;
private JMenuItem helpItem;
private JPanel textPanel;
private JPanel numPanel;
private JButton b0;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
public FormulaCalculatorDialog(JFrame parent, String initFormula) throws HeadlessException {
super(parent, "Définition d'une formule mathématique", true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
formulaText = new JTextField();
if (initFormula != null && initFormula.length() > 0) {
formula = initFormula;
}
else {
formula = "";
}
//**** Menu Bar : save + help
[...]
//**** Content of the window
BorderLayout layout = new BorderLayout();
layout.setHgap(5);
layout.setVgap(5);
getContentPane().setLayout(layout);
//** Text field containing the formula
textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
getContentPane().add(textPanel, BorderLayout.NORTH);
formulaText.setEditable(false);
formulaText.setBackground(Color.WHITE);
formulaText.setText(formula);
textPanel.add(formulaText);
//** Numeric buttons
numPanel = new JPanel();
numPanel.setLayout(new GridLayout(5,4));
getContentPane().add(numPanel, BorderLayout.WEST);
b0 = new JButton("0");
b0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
addTerm("0");
}
});
b1 = new JButton("1");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
addTerm("1");
}
});
[...]
numPanel.add(bOpenBracket);
numPanel.add(bCloseBracket);
numPanel.add(bExp);
numPanel.add(bLog);
numPanel.add(b7);
numPanel.add(b8);
numPanel.add(b9);
numPanel.add(bDiv);
numPanel.add(b4);
numPanel.add(b5);
numPanel.add(b6);
numPanel.add(bMult);
numPanel.add(b1);
numPanel.add(b2);
numPanel.add(b3);
numPanel.add(bMinus);
numPanel.add(b0);
numPanel.add(bPm);
numPanel.add(bDot);
numPanel.add(bPlus);
[...]
// où ajouter le keyListener???
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
System.out.println("ok");
switch (ke.getKeyCode()) {
case KeyEvent.VK_0 :
System.out.println("0");
b0.doClick();
break;
case KeyEvent.VK_1 :
break;
default :
break;
}
}
});
this.pack();
setLocation(Dialog.getCenterPosition(this));
setVisible(true); |