Anda di halaman 1dari 8

/* * To change this template, choose Tools Templates * and open the template in the editor.

*/ package grafico; import java.awt.*; import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.Event.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.FlowLayout; import java.awt.Color; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import javax.swing.ListSelectionModel; import javax.swing.event.ChangeListener;

/** * * @author Diego */ public class Janela extends JFrame { private private private private private private private private private private private private private private private final Color colors[] = { Color.BLUE, Color.YELLOW}; JTextField textField,textField2; // usado para exibir alteraes de fonte Font plainFont; // fonte para texto simples Font boldFont; // fonte para texto negrito Font italicFont; // fonte para texto itlico Font boldItalicFont; // fonte para texto negrito e itlico JRadioButton plainJRadioButton; // seleciona texto simples JRadioButton boldJRadioButton; // seleciona texto negrito JRadioButton italicJRadioButton; // seleciona texto itlico JRadioButton boldItalicJRadioButton; // negrito e itlico ButtonGroup radioGroup; // buttongroup para armazenar botes de opo JRadioButton Azul; JRadioButton Amarelo; JCheckBox text1; JCheckBox text2;

public Janela() { super("Interface Grafica"); setLayout( new FlowLayout() ); textField = new JTextField( "Watch the font style change", 25 ); textField2 = new JTextField( "",25); add( textField ); add( textField2 ); //botoes

plainJRadioButton = new JRadioButton( "Plain", true ); boldJRadioButton = new JRadioButton( "Bold", false ); italicJRadioButton = new JRadioButton( "Italic", false ); boldItalicJRadioButton = new JRadioButton( "Bold/Italic", false ); Azul = new JRadioButton("Azul",false); Amarelo = new JRadioButton("Amarelo",false); //Opcoes text1 = new JCheckBox("um"); text2 = new JCheckBox("dois"); add( plainJRadioButton ); // adiciona boto simples ao JFrame add( boldJRadioButton ); // adiciona boto de negrito ao JFrame add( italicJRadioButton ); // adiciona boto de itlico ao JFrame add( boldItalicJRadioButton ); // adiciona boto de itlico e negrito add(Azul); add(Amarelo); add(text1); add(text2); // cria relacionamento lgico entre JRadioButtons radioGroup = new ButtonGroup(); // cria ButtonGroup radioGroup.add( plainJRadioButton ); // adiciona simples ao grupo radioGroup.add( boldJRadioButton ); // adiciona negrito ao grupo radioGroup.add( italicJRadioButton ); // adiciona itlico ao grupo radioGroup.add( boldItalicJRadioButton ); // adiciona negrito e itlico radioGroup.add(Azul); radioGroup.add(Amarelo); //radioGroup.add(text1); // relacionamento logico JRadioButtons2 //cria objetos de fonte plainFont = new Font( "Serif", Font.PLAIN, 14 ); boldFont = new Font( "Serif", Font.BOLD, 14 ); italicFont = new Font( "Serif", Font.ITALIC, 14 ); boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 ); textField.setFont( plainFont ); // configura fonte inicial como simples textField2.setFont(plainFont) ;

text1.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) {

if(text1.isSelected()) { plainJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setFont(plainFont); } } ) ;

boldJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setFont(boldFont); } } ) ; italicJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setFont(italicFont); } } ) ;

boldItalicJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setFont(boldItalicFont); } } ) ;

Azul.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setBackground(Color.blue); } } ); Amarelo.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setBackground(Color.yellow); } } );

} } );

text2.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean a , b; a = text1.isSelected(); b = text2.isSelected();

if(text2.isSelected()) { if(a = true) { text1.setSelected(false); } plainJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField2.setFont(plainFont); } } ) ;

boldJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField2.setFont(boldFont); } } ) ; italicJRadioButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField2.setFont(italicFont); } } ) ;

boldItalicJRadioButton.addActionListener( new ActionListener() { @Override

public void actionPerformed(ActionEvent e) { textField2.setFont(boldItalicFont); } } ) ;

Azul.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField2.setBackground(Color.blue); } } ); Amarelo.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField2.setBackground(Color.yellow); } } );

} } } );

// classe interna private para tratar eventos de boto de opo

} //private class RadioButtonHandler implements ItemListener

//{ //private Font font; // fonte associada com este listener // public RadioButtonHandler( Font f ) //{ // font = f; // configura a fonte deste listener //} // fim do construtor RadioButtonHandler // trata eventos de boto de opo // @Override //public void itemStateChanged( ItemEvent event ) // { // boolean check = text1.isSelected(); // boolean check2 = text2.isSelected(); //if(check = true) //{ // textField.setFont( font ); // configura fonte de textField //text2.setSelected(false); //}

//else if(check2 = true) //{ //textField2.setFont(font); //text1.setSelected(false); //} //} // fim do mtodo itemStateChanged } // fim da classe RadioButtonHandler interna private // fim da classe RadioButtonFrame

//private class CheckBoxHandler implements ItemListener //{ // private RadioButtonHandler a; // @Override // public void itemStateChanged(ItemEvent e) { // if (e.getSource() = text1) { // //} a.itemStateChanged(e);