MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

  import java.awt.BorderLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;

  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JTextField;

  public class MainClass extends JFrame {
      JTextField field = new JTextField();

      public static void main(String[] args) {
          MainClass st = new MainClass();
      }

public SwingTest() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JButton button = new JButton("Press me");
  button.addActionListener(new ButtonListener());
  this.getContentPane().setLayout(new BorderLayout());
  this.getContentPane().add(button, BorderLayout.SOUTH);
  this.getContentPane().add(field, BorderLayout.NORTH);
  this.pack();
  this.setVisible(true);
}

      class ButtonListener implements ActionListener {
          boolean toggle = true;

          public void actionPerformed(ActionEvent e) {
              if (toggle)
                  field.setText("Simple");
              else
                  field.setText("Sample");
              toggle = !toggle;
          }
      }
  }