Baixe o app para aproveitar ainda mais
Prévia do material em texto
import java.awt.Component; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Preferencias extends JFrame implements ActionListener{ private JLabel lblNome,lblSexo,lblPreferencia; private JTextField txtNome; private JComboBox cboSexo,cboPreferencia; private JButton btnOk,btnFechar; private GridBagConstraints restricoes=new GridBagConstraints(); protected JPanel pnlCima,pnlBaixo,pnlPreferencia; public Preferencias() { this.setTitle("Preferências"); this.setSize(320,180); this.setLocation(300, 80); this.setResizable(false); restricoes.fill=GridBagConstraints.BOTH; lblNome = new JLabel("Nome:", JLabel.RIGHT); txtNome = new JTextField(20); txtNome.setToolTipText("Digite o seu nome"); lblSexo = new JLabel("Sexo:", JLabel.RIGHT); cboSexo = new JComboBox(); cboSexo.setToolTipText("Selecione o sexo"); cboSexo.addItem("Masculino"); cboSexo.addItem("Feminino"); lblPreferencia = new JLabel("Preferências:", JLabel.RIGHT); cboPreferencia = new JComboBox(); cboPreferencia.setToolTipText("Selecione a suas Preferências"); cboPreferencia.addItem("Uma Moto Básica"); cboPreferencia.addItem("Uma Moto Esportiva"); cboPreferencia.addItem("Um JETSKY"); cboPreferencia.addItem("Um Sanduíche de Mortadela"); cboPreferencia.addItem("Um Pastel de Vento"); btnOk = new JButton("OK"); btnOk.setMnemonic('O'); btnOk.setToolTipText("Verifica se os dados foram preenchidos"); btnOk.addActionListener(this); btnFechar = new JButton("Fechar"); btnFechar.setMnemonic('F'); btnFechar.setToolTipText("Fecha a tela"); btnFechar.addActionListener(this); pnlCima = new JPanel(new GridBagLayout()); restricoes.insets = new Insets(8,8,8,8); addGridBag(0,0,lblNome,pnlCima); addGridBag(1,0,txtNome,pnlCima); addGridBag(0,1,lblSexo,pnlCima); addGridBag(1,1,cboSexo,pnlCima); addGridBag(0,2,lblPreferencia,pnlCima); addGridBag(1,2,cboPreferencia,pnlCima); pnlBaixo = new JPanel(new GridBagLayout()); addGridBag(0,0,btnOk,pnlBaixo); addGridBag(1,0,btnFechar,pnlBaixo); pnlPreferencia = new JPanel(new GridBagLayout()); addGridBag(0,0,pnlCima,pnlPreferencia); addGridBag(0,1,pnlBaixo,pnlPreferencia); Container P = getContentPane(); P.add(pnlPreferencia); pack(); } public void actionPerformed(ActionEvent event) { Object origem=event.getSource(); if (origem == btnOk) { if (txtNome.getText().equals("")) { JOptionPane.showMessageDialog(null,"é necessário informar o seu nome","Nome Inválido", JOptionPane.ERROR_MESSAGE); txtNome.requestFocus(); }else { String texto = "Eu " + txtNome.getText().toString() + "\n"; texto = texto + "Vou Conseguir comprar com a minha aposentadoria: " + cboPreferencia.getSelectedItem().toString(); JOptionPane.showMessageDialog(null,texto,"Suas Preferências", JOptionPane.INFORMATION_MESSAGE); } }else if (origem == btnFechar) { dispose(); } } public void addGridBag(final int x,final int y,final Component obj,final JPanel pnl) { restricoes.gridx=x; restricoes.gridy=y; pnl.add(obj,restricoes); } public static void main(String[] args) { Preferencias faixa = new Preferencias(); faixa.setVisible(true); } }
Compartilhar