import java.awt.*; import java.awt.event.*; import javax.swing.*; class Desconto extends JFrame implements ActionListener { JLabel L1, L2, L3, L4; JButton B1, B2; JTextField T1, T2; Desconto(){ setTitle("Calculo de Desconto"); setSize(320,320); getContentPane().setBackground(Color.white); getContentPane().setLayout(new GridLayout(4,2)); L1= new JLabel("Valor"); L2= new JLabel("% de Desconto"); L3= new JLabel("Valor com Desconto:"); L3.setFont(new Font ("Arial",Font.BOLD,14)); L4= new JLabel(""); Font.BOLD,14)); B1= new JButton("Calcular"); } L4.setFont(new Font ("Arial", B1.addActionListener(this); B2= new JButton("Limpar"); B2.addActionListener(this); T1= new JTextField(10); T2= new JTextField(10); getContentPane().add(L1); getContentPane().add(T1); getContentPane().add(L2); getContentPane().add(T2); getContentPane().add(B1); getContentPane().add(B2); getContentPane().add(L3); getContentPane().add(L4); public void actionPerformed(ActionEvent a){ if (a.getSource()==B1){ float valor=0, desconto=0; try{ valor=Float.parseFloat(T1.getText()); desconto=Float.parseFloat(T2.getText())/100; valor=valor*desconto; L4.setText(""+valor); } catch (NumberFormatException erro){ JOptionPane.showMessageDialog(null,"Erro no formato dos dados"); } } } public static void main(String args[]){ JFrame Janela= new Desconto(); Janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Janela.setVisible(true); } }