|
KvUravnV2
Описание:
Программа, той же сути, что и KvUravn, но теперь она в отдельном окне.(Т.е. чтобы ее запустить, нужно скачать себе KvUravnV2.class, а после, в командной сроке
написать: " java KvUravnV2 ".)
Программа:
import java.awt.*;
////////////////////////////////////////////////////////////
//////// Copyright by Konstantin S. Matveyev ///////////
//////// 29.12.99 ///////////
//////// http://kopeck.4u.ru ///////////
////////////////////////////////////////////////////////
public class kvuravnv2 extends Frame {
private Button sb; // submit button
private Button ex; // ex button
private Button h; // help button
private TextField a; //
private TextField b; //
private TextField c; //
private TextArea ta; //
public kvuravnv2() {
super("Решение квадратных уравнений");
setResizable(false);
setLayout( new FlowLayout(FlowLayout.LEFT) );
TopMenu();
setBackground(Color.lightGray);
Color col = new Color(0, 0, 40);
// TEXTFIELD 1
a = new TextField("", 3);
a.setForeground(col);
a.setBackground(Color.white);
this.add(a);
// TEXTFIELD 2
b = new TextField("" , 3);
b.setForeground(col);
b.setBackground(Color.white);
this.add(b);
// TEXTFIELD 3
c = new TextField("", 3);
c.setForeground(col);
c.setBackground(Color.white);
this.add(c);
//SUBMIT BUTTON
sb = new Button("Решить");
sb.setForeground(col);
sb.setBackground(Color.lightGray);
this.add(sb);
// HELP BUTTON
h = new Button("Помощь");
h.setForeground(col);
h.setBackground(Color.lightGray);
this.add(h);
//Color.black
// EXIT BUTTON
ex = new Button("Выход");
ex.setForeground(col);
ex.setBackground(Color.lightGray);
this.add(ex);
// TEXTAREA
ta = new TextArea("", 11, 82);
ta.setForeground(col);
ta.setBackground(Color.white);
ta.setEditable(false);
this.add(ta);
resize(370, 220);
show();
}
private void TopMenu() {
MenuBar mb = new MenuBar() ;
Menu file = new Menu("Файлы");
//line
file.addSeparator();
//end of line
file.add( new MenuItem("Выход") );
mb.add(file);
setMenuBar(mb);
}
////////////////////// ACTION ///////////////////////////
public boolean action(Event event, Object obj) {
if (event.target instanceof MenuItem) {
String label = (String) obj;
if (label.equals("Выход")) {
System.exit(0);
}
return true;
}
if (event.target instanceof Button) {
String label = (String) obj;
if (label.equals("Решить")) {
try {
int a1=Integer.parseInt(a.getText());
int b1=Integer.parseInt(b.getText());
int c1=Integer.parseInt(c.getText());
double e, d, x1, x2, x3;
e = 0;
d = (b1 * b1)-(4 * a1 * c1);
double f = Math.sqrt(d);
x1 = (f - b1)/(2 * a1);
x2 = (-f -b1)/(2 * a1);
x3 = (-b1)/(2 *a1); // if (d == e)
ta.appendText ("Дискриминант = "+ d);
if (d >= e) ta.appendText("\nКорень из дискриминанта = "+ f);
if (d < e)
ta.appendText("\nРешения нет, так как дискриминант меньше нуля\n \n \n \n \n \n \n \n \n");
if (d > e) ta.appendText("\nСуществует 2 решения");
if (d > e) ta.appendText("\nПервое решение: x = "+ x1);
if (d > e) ta.appendText ("\nВторое решение: x = "+ x2 + "\n \n \n \n \n \n");
if (d == e) ta.appendText("\nСуществует одно решение, так как дискриминант равен нулю");
if (d == e) ta.appendText("\nРешение: x = "+ x3 + "\n \n \n \n \n \n \n");
} catch(NumberFormatException e) {
ta.appendText("");
System.out.println("It isn't a number!");
}
}
if (label.equals("Помощь")) {
/// В программе, ta.appendText() в одну строку.
ta.appendText(" В помощь пользователю: \n
Для того, чтобы решить квадратное, Вам следует: \n
1.Посмотреть в левый верхний угол. \n
2.Указать коэффициент при X квадрате, при X и свободном \n
члене соответственно. \n
3. Нажать на кнопку <<Решить>> и получить ответ. \n
4. Для выхода из программы нажать <<Выход>>. \n \n \n \n");
}
if (label.equals("Выход")) {
System.exit(0);
return true;
}
}
return false;
}
////////////////////// END OF ACTION ///////////////////////////
public static void main(String args[]) {
new kvuravnv2();
}
}
| |
|