Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Links:
- https://stackoverflow.com/questions/9543320/how-to-position-the-form-in-the-center-screen
- https://www.youtube.com/watch?v=mjOicuXEvwg
- */
- package com.samkough.main;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class FirstGui extends JFrame
- {
- private JLabel l1;
- private JLabel l2;
- private JButton b1;
- private JButton b2;
- private int x = 0, y = 0;
- public FirstGui()
- {
- setLayout(new FlowLayout());
- b1 = new JButton("Button 1");
- add(b1);
- b2 = new JButton("Button 2");
- add(b2);
- l1 = new JLabel("");
- add(l1);
- l2 = new JLabel("");
- add(l2);
- Gui e = new Gui();
- b1.addActionListener(e);
- Gui2 ev = new Gui2();
- b2.addActionListener(ev);
- }
- public class Gui implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if (x == 0)
- {
- l1.setText("This is Button 1.");
- x = 1;
- }
- else if (x == 1)
- {
- l1.setText("");
- x = 0;
- }
- }
- }
- public class Gui2 implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if (y == 0)
- {
- l2.setText("This is Button 2.");
- y = 1;
- }
- else if (y == 1)
- {
- l2.setText("");
- y = 0;
- }
- }
- }
- public static void main(String args[])
- {
- FirstGui frame = new FirstGui();
- frame.setVisible(true);
- frame.setSize(200, 200);
- frame.setTitle("First Gui");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment