L’esercizio della verifica consisteva nel fare la conversione da gradi centigradi a gradi Fahrenheit e nel tracciare un grafico
import javax.swing.*;
import java.awt.*;
public class Grafico extends JPanel {
private int x2=100;
private int p1;
private int p2;
double cast =x2*1.8+32;
private int y2=(int)cast;
private int x1=0;
private int y1=32;
public void paintComponent(Graphics g){
int h=this.getHeight();
g.setColor(Color.RED);
g.drawLine(x1, h-y1, x2, h-y2);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Verifica {
JFrame f1=new JFrame("Converti");
JTextField t1=new JTextField();
JLabel conv=new JLabel();
JLabel farin=new JLabel("Temperatura in °F");
JLabel celsius=new JLabel("temperatura in °C");
JButton b=new JButton("Converti");
Grafico g=new Grafico();
public Verifica(){
b.addActionListener(new Bottone());
f1.setLayout(new GridLayout(3,4));
f1.add(t1);
f1.add(celsius);
f1.add(conv);
f1.add(farin);
f1.add(g);
f1.add(BorderLayout.SOUTH,b);
f1.setSize(500,500);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Bottone implements ActionListener{
public void actionPerformed(ActionEvent e){
String valore=t1.getText();
double valore1=Double.parseDouble(valore);
Double farinheit=valore1*1.8+32;
String conversione=Double.toString(farinheit);
int v=(int)valore1;
conv.setText(conversione);
f1.repaint();
}
}
public static void main(String Args[]){
Verifica v=new Verifica();
}
}
Commenti
Posta un commento