Swing:Exemplo Locadora parte 1
<syntaxhighligth lang="java">
package aula1404;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List;
import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;
public class LocadoraSwing extends JFrame { private final static int marginLeft = 20; private final static int marginMeio = 220; private final static int paddingTop = 40; private static int top = 20; private List<Carro> garagem;
public LocadoraSwing(String title){ super(title); garagem = new ArrayList<Carro>();
setBounds(100,100,500,400); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); inicializaGaragem(); getContentPane().setLayout(null);
JLabel nomeLbl = new JLabel("Escolha um carro:"); nomeLbl.setBounds(marginLeft,top,200,30); add(nomeLbl);
JTextField precoTxt = new JTextField();
JComboBox jc = new JComboBox(garagem.toArray()); jc.insertItemAt("--Selecione--", 0); jc.setSelectedIndex(0); jc.setBounds(marginMeio,top,200,30); jc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(jc.getSelectedIndex()!=0){ Carro c = (Carro)jc.getSelectedItem(); precoTxt.setText(c.getDiaria()+""); } } }); add(jc); top+=paddingTop;
JLabel precoLbl = new JLabel("Preço da diária"); precoLbl.setBounds(marginLeft,top,200,30); add(precoLbl);
precoTxt.setBounds(marginMeio,top,200,30); add(precoTxt);
top+=paddingTop;
JLabel descLbl = new JLabel("Desconto:"); descLbl.setBounds(marginLeft,top,200,30); add(descLbl);
JTextField descTxt = new JTextField(); descTxt.setBounds(marginMeio,top,200,30); descTxt.setText("0.0"); add(descTxt);
top+=paddingTop;
JSeparator sep = new JSeparator(); sep.setBounds(marginLeft, top, 400, 5); add(sep);
top+=this.paddingTop;
JLabel valorFinalRSLbl = new JLabel();
JButton jb = new JButton("Calcular"); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Double preco = Double.parseDouble(precoTxt.getText()); Double desc = Double.parseDouble(descTxt.getText()); Double precoFinal = preco - desc; valorFinalRSLbl.setText("R$ "+precoFinal+""); } }); jb.setBounds(marginLeft,top,150,30); add(jb);
top+=this.paddingTop;top+=this.paddingTop;
JLabel valorLbl = new JLabel("Valor final:"); valorLbl.setBounds(marginLeft,top,200,30); add(valorLbl);
valorFinalRSLbl.setBounds(marginMeio,top,200,30); add(valorFinalRSLbl);
} public void inicializaGaragem(){ Carro fusca = new Carro("Fusca",75d,"C"); Carro jetta = new Carro("Jetta",140d,"A"); Carro gol = new Carro("Gol",90d,"B");
garagem.add(fusca); garagem.add(gol); garagem.add(jetta); }
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { // UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
LocadoraSwing s = new LocadoraSwing("Locadora de carro"); s.setVisible(true); s.setResizable(false);
}
}
</syntaxhighligth>