You are on page 1of 5

JanelaBaskaraSwingEvAnonimo.

java
3 import java.awt.Container;
19
20
21 public class JanelaBaskaraSwingEvAnonimo extends JFrame{
22
23
/**
24
*
25
*/
26
private static final long serialVersionUID = 1L;
27
private JButton btnCalcular;
28
private JButton btnLimpar;
29
private JTextField textA;
30
private JTextField textB;
31
private JTextField textC;
32
private JLabel lblA;
33
private JLabel lblB;
34
private JLabel lblC;
35
private JLabel lblX1;
36
private JLabel lblX2;
37
private JLabel lblResultX1;
38
private JLabel lblResultX2;
39
private Container container;
40
private JMenuBar menu;
41
private JMenu mnArquivo;
42
private JMenu mnAjuda;
43
private JMenuItem miNovo;
44
private JMenuItem miSair;
45
private JMenuItem miSobre;
46
47
JanelaBaskaraSwingEvAnonimo(){
48
49
super("Calculo Baskara");
50
51
container = this.getContentPane();
52
container.setLayout(new FlowLayout());
53
54
btnLimpar = new JButton("Limpar");
55
btnCalcular = new JButton("Calcular");
56
57
btnCalcular.addActionListener( new ActionListener(){
58
public void actionPerformed(ActionEvent e){
59
calculo();
60
}
61
});
62
63
btnLimpar.addActionListener( new ActionListener(){
64
public void actionPerformed(ActionEvent e){
65
limpar();
66
}
67
});
68
69
70
textA = new JTextField(10);
71
textB = new JTextField(10);
72
textC = new JTextField(10);
73
74
lblA = new JLabel("A: ");
75
lblB = new JLabel("B: ");
76
lblC = new JLabel("C: ");
77
78
lblX1 = new JLabel("X': ");
79
lblResultX1 = new JLabel("0.00");
Page 1

JanelaBaskaraSwingEvAnonimo.java
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

lblX2 = new JLabel("X'': ");


lblResultX2 = new JLabel("0.00");
menu = new JMenuBar();
mnArquivo = new JMenu("Arquivo");
mnAjuda = new JMenu("Ajuda");
miNovo = new JMenuItem("Novo");
miSair = new JMenuItem("Sair");
miSobre = new JMenuItem("Sobre...");
miNovo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
limpar();
}
});
miSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
miSairOnclick();
}
});
miSobre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sobre();
}
});
miSobre.setMnemonic(KeyEvent.VK_S);
menu.add(mnArquivo);
menu.add(mnAjuda);
mnArquivo.add(miNovo);
mnArquivo.add(miSair);
mnAjuda.add(miSobre);
this.setJMenuBar(menu);
container.add(lblA);
container.add(textA);
container.add(lblB);
container.add(textB);
container.add(lblC);
container.add(textC);
container.add(lblX1);
container.add(lblX2);
container.add(lblResultX1);
container.add(lblResultX2);
container.add(btnCalcular);
container.add(btnLimpar);
this.setSize(750, 100);
this.setVisible(true);
}
protected void miSairOnclick() {
Page 2

JanelaBaskaraSwingEvAnonimo.java
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
194
195
196
197
198
199
200
201
202
203

System.exit(1);
}
public static void main(String args[]){
JanelaBaskaraSwingEvAnonimo janela = new JanelaBaskaraSwingEvAnonimo();
}
public void windowActivated(WindowEvent arg0) {
System.out.println("windowActivated");
}
public void windowClosed(WindowEvent arg0) {
System.out.println("windowClosed");
}
public void windowClosing(WindowEvent arg0) {
System.out.println("windowCloseind");
System.exit(1);
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == btnLimpar){
limpar();
} else if (arg0.getSource() == btnCalcular){
calculo();
} else if (arg0.getSource() == miNovo){
limpar();
} else if (arg0.getSource() == miSobre){
sobre();
} else if (arg0.getSource() == miSair){
System.exit(1);;
}

}
private void sair() {
JOptionPane.showMessageDialog(null, "Clicou sair");
Page 3

JanelaBaskaraSwingEvAnonimo.java
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

}
private void sobre() {
//new JanelaSobre(this);
new NovaSobre();
}
private void novo() {
// TODO Auto-generated method stub
}
private void calculo() {
if (isCamposConsistentes()){
double
valorA
valorB
valorC

valorA, valorB, valorC;


= Double.parseDouble(textA.getText());
= Double.parseDouble(textB.getText());
= Double.parseDouble(textC.getText());

double delta = Math.pow(valorB, 2) - 4 * valorA * valorC;


if(delta > 0){
double x1 = -valorB + Math.sqrt(delta)/ (2 * valorA);
double x2 = -valorB - Math.sqrt(delta)/ (2 * valorA);
lblResultX1.setText(String.valueOf(x1));
lblResultX2.setText(String.valueOf(x2));
} else { JOptionPane.showMessageDialog(this, "Delta negativo no consigo!");}
} else {
JOptionPane.showMessageDialog(this, "Campos Inconsistentes Verificar!");
}

}
private boolean isCamposConsistentes() {
String valorA, valorB, valorC;
valorA = textA.getText();
valorB = textB.getText();
valorC = textC.getText();
if(valorA.equals("")){
return false;
} else if(valorB.equals("")){
return false;
} else if(valorC.equals("")){
return false;
}
try{
Double.parseDouble(valorA);
} catch(Exception e){
return false;
}
try{
Double.parseDouble(valorB);
} catch(Exception e){
return false;
}
Page 4

JanelaBaskaraSwingEvAnonimo.java
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 }

try{
Double.parseDouble(valorC);
} catch(Exception e){
return false;
}
return true;
}
private void limpar() {
textA.setText("");
textB.setText("");
textC.setText("");
}
public JTextField getTextA(){
return textA;
}

Page 5

You might also like