codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Venti extends JFrame implements ActionListener
{
private JPanel p = new JPanel();
private JLabel e1 = new JLabel ("Numero");
private JLabel e2 = new JLabel ("Premi per calcolare il 20 %");
private JTextField t1 = new JTextField(20);
private JButton b = new JButton("20 %");
public Venti()
{
Container c = this.getContentPane();
c.add(p);
//aggiunge le componenti al pannello
p.setLayout(new GridLayout(2,2,10,10));
p.add(e1);
p.add(t1);
p.add(e2);
p.add(b);
//ascoltatore
b.addActionListener(this);
b.setActionCommand("Premi per calcolare il 20 %");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s = t1.getText();
float n = Float.valueOf(s).intValue();
n = (n/100)*20;
t1.setText(""+n);
}
}
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Usa
{
public static void main(String argv[])
{
Venti a = new Venti();
a.setTitle("Esercizio");
a.pack();
a.setVisible(true);
}
}
prova cosi a me va