Search code examples
javaswingawtjbutton

Connecting 2 different programs in Java (each one has its main method)


I have created 2 programs (Background and NrLojtareve) using Java GUI where each one of them creates a step of a game. Each program has its own main method.

All I want to do is after executing the first program (NrLojtareve) which includes 4 radio buttons to display the other page, I created with the other program(Background).

So going from the first step of the game to the second. So the handler of NrLojtareve class to call the Background class.

Can anyone show me how to call the second program from the first or how to display the second GUI after selecting a radio button from the first program?


Here is the code.

Nrlojtareve.java

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

public class Nrlojtareve extends JFrame {

    private JRadioButton a;
    private JRadioButton b;
    private JRadioButton c;
    private JRadioButton d;
    private ButtonGroup group ;
    private JLabel e;

    public Nrlojtareve() {
        setLayout(new FlowLayout());
        a=new JRadioButton("1");
        b=new JRadioButton("2");
        c=new JRadioButton("3");
        d=new JRadioButton("4");
        e=new JLabel("Choose the number of players!");
        add(a);
        add(b);
        add(c);
        add(d);
        add(e);
        group = new ButtonGroup();
        group.add(a);
        group.add(b);
        group.add(c);
        group.add(d);

        thehandler hand = new thehandler();
        a.addItemListener(hand);
        b.addItemListener(hand);
        c.addItemListener(hand);
        d.addItemListener(hand);
    }

    private class thehandler implements ItemListener {
        public void itemStateChanged(ItemEvent event) {

        }
    }

    public static void main(String[] args) {

        Nrlojtareve elda = new Nrlojtareve();
        elda.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        elda.setSize(300,400);
        elda.setVisible(true);
    }

}


Back.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;

public class Back extends JFrame {

private Container pane;
    public Back() {
        super("title");
        setLayout(null);

        Icon i=new ImageIcon(getClass().getResource("1.png"));
        pane=new Container();

        //konstruktori i handler merr nje instance te Background
        thehandler hand = new thehandler();

    }

     private class thehandler implements ActionListener {

         public void actionPerformed(ActionEvent event) {

         }
    }
    public static void main(String[] args) {

         Back  d = new Back() ;

         d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         d.getContentPane().setBackground(Color.GREEN);
         d.setSize(700,500);

         d.setVisible(true);    
    }
}

Solution

  • Move these lines from the main method in the Back class to the end of the Back constructor. It should look like:

    public Back(){
        super("title");
        setLayout(null);
    
        Icon i=new ImageIcon(getClass().getResource("1.png"));
        pane=new Container();
    
        thehandler hand=new thehandler();//konstruktori i handler merr nje instance te Background
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane().setBackground(Color.GREEN);
        setSize(700,500);
        setVisible(true);    
    }
    

    Then in your Nrlojtareve class put

    public void itemStateChanged(ItemEvent event){
        Back back = new Back();
    }
    

    Also, you can remove the main method completely from the Back class.