Baixe o app para aproveitar ainda mais
Prévia do material em texto
Aula 07 JcheckBox / Cores JChekBox Um objeto JCheckBox está associado a um item que pode estar selecionado ou não, independentemente de outros. Possui três construtores: public Checkbox(); public Checkbox(String labeltext); public Checkbox(String Labeltext, CheckboxGroup g, boolean ini); O terceiro construtor parece estranho, mas isto é devido ao fato de Checkbox é usado também para criar botões de rádio. O primeiro e segundo construtor tem nada a explicar. O terceiro construtor é usado, quando deseja criar ChekBox já ligado ou desligao pelo default. O segundo parâmetro CheckboxGroup, por enquanto, esquece. Passe o new Checkboxgoup() no lugar dele que instanciará um novo checkboxgroup. Exemplo1: package programas; import java.awt.Container; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Exemplo1JchekBox extends JFrame { private JLabel lblIdioma; private JTextField txtNome; private JCheckBox rbtIngles, rbtFrances, rbtAlemao; public Exemplo1JchekBox() { super("Testando JCheckBox"); } public void inicializar() { setLayout(null); Container container = getContentPane(); lblIdioma = new JLabel("Idioma:"); lblIdioma.setBounds(100, 50, 100, 30);//Col Lin com alt rbtIngles = new JCheckBox("Ingles"); rbtFrances = new JCheckBox("Frances"); rbtAlemao = new JCheckBox("Alemão"); rbtIngles.setBounds(150, 50, 100, 30);//Col Lin com alt rbtFrances.setBounds(250, 50, 100, 30); rbtAlemao.setBounds(350, 50, 100, 30); container.add(lblIdioma); container.add(rbtIngles); container.add(rbtFrances); container.add(rbtAlemao); } public static void main(String[] args) { Exemplo1JchekBox ex2 = new Exemplo1JchekBox(); ex2.inicializar(); ex2.setVisible(true); ex2.setSize(500, 200); ex2.setDefaultCloseOperation(EXIT_ON_CLOSE); } } Exemplo2: package programas; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Exemplo2JchekBox extends JFrame { private JLabel lblIdioma; private JCheckBox cbxIngles, cbxFrances, cbxAlemao; private JButton botao; public Exemplo2JchekBox() { super("Testando "); } public void inicializar() { setLayout(null); Container container = getContentPane(); lblIdioma = new JLabel("Idioma:"); lblIdioma.setBounds(100, 50, 100, 30);//Col Lin com alt cbxIngles = new JCheckBox("Ingles"); cbxFrances = new JCheckBox("Frances"); cbxAlemao = new JCheckBox("Alemão"); cbxIngles.setBounds(150, 50, 100, 30);//Col Lin com alt cbxFrances.setBounds(250, 50, 100, 30); cbxAlemao.setBounds(350, 50, 100, 30); botao = new JButton("Clik"); botao.setBounds(100, 100, 100, 30);//Col Lin com alt container.add(botao); container.add(lblIdioma); container.add(cbxIngles); container.add(cbxFrances); container.add(cbxAlemao); botao.addActionListener(new TrataEvento()); } public class TrataEvento implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String ingles = null; String frances = null; String alemao = null; if (cbxIngles.isSelected()) { ingles = "Ingles"; } else { ingles = null; } if (cbxFrances.isSelected()) { frances = "Frances"; } else { frances = null; } if (cbxAlemao.isSelected()) { alemao = "Alemão"; } else { alemao = null; } JOptionPane.showMessageDialog(null, "Idiomas: " + ingles + " " + frances + " " + alemao); } } public static void main(String[] args) { Exemplo2JchekBox ex2 = new Exemplo2JchekBox(); ex2.inicializar(); ex2.setVisible(true); ex2.setSize(500, 200); ex2.setDefaultCloseOperation(EXIT_ON_CLOSE); } } Exemplo3: package programas; import java.awt.Container; import java.awt.Font; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Exemplo3JcheckBox extends JFrame { private JLabel lblNome; private JLabel lblCorTela; private JTextField txtNome; private JLabel lblFormatacao; private JCheckBox chkArial; private JCheckBox chkItalico; private JCheckBox chkNegrito; public Exemplo3JcheckBox() { super("Testando JChekBox"); } public void init() { setLayout(null); Container container = getContentPane(); lblFormatacao = new JLabel("Formatação: "); lblFormatacao.setBounds(100, 50, 100, 30); chkArial = new JCheckBox("Arial"); chkArial.setBounds(200, 50, 100, 30); chkNegrito = new JCheckBox("Negrito"); chkNegrito.setBounds(300, 50, 100, 30); chkItalico = new JCheckBox("Itálico"); chkItalico.setBounds(400, 50, 100, 30); lblNome = new JLabel("Nome:"); lblNome.setBounds(100, 90, 100, 30); txtNome = new JTextField(); txtNome.setBounds(200, 90, 300, 25); lblCorTela = new JLabel("Cor da Tela:"); lblCorTela.setBounds(100, 120, 100, 30); txtNome.setFont(new Font("Times New Roman", Font.PLAIN, 15)); container.add(lblFormatacao); container.add(chkArial); container.add(chkNegrito); container.add(chkItalico); container.add(lblNome); container.add(txtNome); chkArial.addItemListener(new FormataTexto()); chkNegrito.addItemListener(new FormataTexto()); chkItalico.addItemListener(new FormataTexto()); } public class FormataTexto implements ItemListener { @Override public void itemStateChanged(ItemEvent e) { if (chkArial.isSelected()) { txtNome.setFont(new Font("Arial", Font.PLAIN, 15)); } if (chkItalico.isSelected()) { txtNome.setFont(new Font("Arial", Font.ITALIC, 15)); } if (chkNegrito.isSelected()) { txtNome.setFont(new Font("Arial", Font.BOLD, 15)); } if ((chkArial.isSelected()) && (chkNegrito.isSelected()) && (chkItalico.isSelected())) { txtNome.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 15));} } } public static void main(String[] args) { Exemplo3JcheckBox x = new Exemplo3JcheckBox(); x.init(); x.setVisible(true); x.setSize(600, 200); x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Exemplo4: package programas; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JTextField; public class Exemplo5JcheckBox extends JFrame { private JLabel lblNome; private JLabel lblCorTela; private JTextField txtNome; private JRadioButton btrCorTelaCinza; private JRadioButton btrCorTelaAzul; private JRadioButton btrCorTelaAmarelo; private ButtonGroup grupoCor; public Exemplo5JcheckBox() { super("Testando JChekBox"); } public void init() { setLayout(null); Container container = getContentPane(); getContentPane().setBackground(Color.pink);//Mudar cor fundo da tela lblNome = new JLabel("Nome:"); lblNome.setBounds(100, 90, 100, 30); lblNome.setForeground(Color.blue); // Cor do caracter txtNome = new JTextField(); txtNome.setBounds(200, 90, 300, 25); txtNome.setBackground(Color.yellow); // Cor do combo txtNome.setForeground(Color.red); // Cor do caracter // txtNome.setForeground(new Color(255, 20, 147));//Rosa choque // txtNome.setBackground(new Color(127, 255, 212));//Verde lblCorTela = new JLabel("Cor da Tela:"); lblCorTela.setBounds(100, 120, 100, 30); btrCorTelaCinza = new JRadioButton("Cinza"); btrCorTelaCinza.setBounds(200, 120, 100, 25); btrCorTelaAzul = new JRadioButton("Azul"); btrCorTelaAzul.setBounds(300, 120, 100, 25); btrCorTelaAmarelo = new JRadioButton("Amarelo"); btrCorTelaAmarelo.setBounds(400, 120, 100, 25); grupoCor = new ButtonGroup(); grupoCor.add(btrCorTelaCinza); grupoCor.add(btrCorTelaAzul); grupoCor.add(btrCorTelaAmarelo); txtNome.setFont(new Font("Times New Roman", Font.PLAIN, 15)); container.add(lblNome); container.add(txtNome); container.add(lblCorTela); container.add(btrCorTelaCinza); container.add(btrCorTelaAzul); container.add(btrCorTelaAmarelo); btrCorTelaCinza.addItemListener(new ModificaTela()); btrCorTelaAzul.addItemListener(new ModificaTela()); btrCorTelaAmarelo.addItemListener(new ModificaTela()); } public class ModificaTela implements ItemListener { @Override public void itemStateChanged(ItemEvent e) { if (btrCorTelaAmarelo.isSelected()) { getContentPane().setBackground(Color.yellow); } else if (btrCorTelaCinza.isSelected()) { getContentPane().setBackground(Color.LIGHT_GRAY); } else if (btrCorTelaAzul.isSelected()) { getContentPane().setBackground(Color.CYAN); } } } public static void main(String[] args) { Exemplo5JcheckBox x = new Exemplo5JcheckBox(); x.init(); x.setVisible(true); x.setSize(600, 200); x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Exercícios: Escrever o código em Java Swing das interfaces abaixo.
Compartilhar