GUI Learning (II): calculated input sides of a triangle area of a triangle

JTextField content to string you want to convert into a double type can only be converted in the actionPerformed (), the only way you can not go wrong

package Work;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class exe21 {
public static void main(String args[]) {
MyFrame mf=new MyFrame("计算三角形面积",100,100,1000,750);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
JLabel []jl= {new JLabel("边A:"),new JLabel("边B:"),new JLabel("边C:")};
JTextField jtf1= new JTextField(15);
JTextField jtf2= new JTextField(15);
JTextField jtf3= new JTextField(15);
JButton jb=new JButton("计算面积");
jp.add(jl[0]);
jp.add(jtf1);
jp.add(jl[1]);
jp.add(jtf2);
jp.add(jl[2]);
jp.add(jtf3);
jp.add(jb);
mf.add(jp,BorderLayout.NORTH);
JTextArea jta=new JTextArea();
mf.add(jta,BorderLayout.CENTER);
TriAngle tri=new TriAngle();
tri.setJButton(jb);
tri.setJTextArea(jta);
tri.setValue(jtf1,jtf2,jtf3);
jb.addActionListener(tri);
mf.setVisible(true);
}
}
class MyFrame extends JFrame{
MyFrame(String s,int x,int y,int z,int h){
setTitle(s);
setLayout(new BorderLayout());
setBounds(x,y,z,h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class TriAngle implements ActionListener{
double a;
double b;
double c;
double p;
JButton jbu;
Jea the JTextArea;
the JTextField jtd1;
the JTextField jtd2;
the JTextField jtd3;
void the setValue (the JTextField A, B the JTextField, the JTextField C) {
jtd1 = A;
jtd2 = B;
jtd3 = C; if // array form of given mass participants
}
setArea Double () {
return the Math.sqrt (P * (PA) * (Pb) * (PC));
}
void setJButton (the JButton J) {
JBU = J;
}
void setJTextArea (the JTextArea J) {
Jea = J;
}
void the actionPerformed public (the ActionEvent E) {
this.a = Double.parseDouble (jtd1.getText ());
this.b = Double.parseDouble (jtd2.getText ());
this.c = Double.parseDouble (jtd3.getText ( )); // JTextField content to string desired type can be converted into double actionPerformed conversion, the error is not the only way to
p = (a + b + c ) / 2;
IF (A + B> C && A + C> B && B + C> A) {
jea.append ( "triangle" + a + "" + b + "" + c + " area:" + setArea () + " \ n");
}
the else jea.append ( "triangle" + a + "" + b + "" + c + " area: the area can not be calculated" + "\ n-");
}
}

Guess you like

Origin www.cnblogs.com/Yshun/p/11123388.html