Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package name2;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- public class Name2 implements ActionListener {
- final static String LABEL_TEXT = JOptionPane.showInputDialog("Enter your name.");
- JFrame frame;
- JPanel contentPane;
- JLabel label;
- JButton button;
- public Name2(){
- frame = new JFrame("Name");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- contentPane = new JPanel();
- label = new JLabel(LABEL_TEXT);
- contentPane.add(label);
- button = new JButton("Hide");
- button.setActionCommand("Hide");
- button.addActionListener(this);
- contentPane.add(button);
- frame.setContentPane(contentPane);
- frame.pack();
- frame.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent event) {
- String eventName = event.getActionCommand();
- if (eventName.equals("Hide")) {
- label.setText(" ");
- button.setText("Show");
- button.setActionCommand("Show");
- } else {
- label.setText(LABEL_TEXT);
- button.setText("Hide");
- button.setActionCommand("Hide");
- }
- }
- private static void runGUI() {
- JFrame.setDefaultLookAndFeelDecorated(true);
- Name2 name = new Name2();
- }
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- runGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment