mercredi 11 mars 2015

JButton wont show components



I want to make a simple Frame that would show a text field if I click on one button and would show a JLabel if I click on the other, but its not working and I'm not sure why?


I tried setting the variables to public but the result was the same, I also tried another commands in the button commands and it worked like showMessageDialog so I'm confused why it wont work



import java.awt.*;
import javax.swing.*;


public class CheckBoxFrame extends JFrame
{
private JTextField textField1;
private JLabel label1;
private JButton txt;
private JButton label;
public CheckBoxFrame()
{

super ("JCheckBox Test" );
textField1= new JTextField ("Text Field 1", 20 );
textField1.setFont ( new Font ("Serif", Font.PLAIN,14));
add(textField1);
textField1.setVisible(false);

label1 = new JLabel("Label1", JLabel.CENTER);
add(label1);
label1.setVisible(false);

txt = new JButton ("Text Field");
ButtonHandler b1 = new ButtonHandler();
txt.addActionListener(b1);
add(txt);

label = new JButton ("label ");
add(label);

setLayout( new FlowLayout() );
setVisible(true);
setSize(400,400);

}


private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
if (a.getSource() == txt)
{
label1.setVisible(false);
textField1.setVisible(true);

}

if (a.getSource() == label)
{
label1.setVisible(true);
textField1.setVisible(false);

}
}
}



}



Aucun commentaire:

Enregistrer un commentaire