Как связать 2 фрейма в Java вместе?

У меня есть 2 Класса JAVA. Мне необходимо связать 2 GUI. Точнее переход на второй класс по кнопке "Next", должно получиться по типу HTML .

Вот что у меня сейчас есть:

Первый класс (main):

package game_programmer;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.text.Normalizer.Form;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Frame;


public class main extends FirstGUI{

    private int count = 0;
    private JFrame frame;
    private JLabel lblBalance;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    main window = new main();
                    window.frame.setVisible(true);;
                    window.frame.setResizable(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("Ne title text");;
        frame.getContentPane().setBackground(new Color(255, 228, 196));
        frame.setBackground(new Color(255, 228, 196));
        frame.getContentPane().setForeground(new Color(255, 222, 173));
        frame.setBounds(100, 100, 695, 404);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JButton btnNewButton = new JButton("Работать");
        btnNewButton.setFont(new Font("Acrom", Font.PLAIN, 22));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                count++;
                lblBalance.setText("Баланс: "+count + "$");
            }
        });
        btnNewButton.setBounds(238, 131, 178, 84);
        frame.getContentPane().add(btnNewButton);
        
        lblBalance = new JLabel("Баланс: 0$");
        lblBalance.setFont(new Font("Acrom", Font.PLAIN, 17));
        lblBalance.setBounds(540, 0, 139, 73);
        frame.getContentPane().add(lblBalance);
        
        JButton btnNext = new JButton("NEXT...");
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.setVisible(false);
                FirstGUI frame1.setVisible(true);
            }
        });
        btnNext.setBounds(286, 270, 89, 23);
        frame.getContentPane().add(btnNext);
    }
    }

Второй класс (FirstGUI):

package game_programmer;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FirstGUI<FirstGui> extends JFrame{

    private int count = 0;
    private JFrame frame;
    private JLabel lblBalance;

    /**
     * Launch the application.

    /**
     * Create the application.
     */
    public FirstGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        FirstGUI frame1 = new FirstGUI();
        frame1.setVisible(true);
        frame1.getContentPane().setBackground(new Color(255, 228, 196));
        frame1.setBackground(new Color(255, 228, 196));
        frame1.getContentPane().setForeground(new Color(255, 222, 173));
        frame1.setBounds(100, 100, 695, 404);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.getContentPane().setLayout(null);
        
        JButton btnNewButton = new JButton("Работать");
        btnNewButton.setFont(new Font("Acrom", Font.PLAIN, 22));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                count++;
                lblBalance.setText("Баланс: "+count + "$");
            }
        });
        btnNewButton.setBounds(238, 131, 178, 84);
        frame.getContentPane().add(btnNewButton);
        
        lblBalance = new JLabel("Баланс: 0$");
        lblBalance.setFont(new Font("Acrom", Font.PLAIN, 17));
        lblBalance.setBounds(540, 0, 139, 73);
        frame.getContentPane().add(lblBalance);
    }
        
    }

Что у меня получается. main

FirstGUI


Ответы (1 шт):

Автор решения: Agzam

Объединил FirstGUI.java и MyFrame.java в один класс

Как и говорил под другим Вашим вопросом - используйте CardLayout

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JButton;

public class MyFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    CardLayout cardLayout = new CardLayout(0, 0);
    
    /**
     * Create the frame.
     */
    
    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(cardLayout);

        
        JPanel panel = new JPanel();
        contentPane.add(panel, "frame1");
        frame1(panel);
        
        JPanel panel2 = new JPanel();
        contentPane.add(panel2, "frame2");
        frame2(panel2);
        
    }
    
    private int count1 = 0;
    private JLabel lblBalance1;

    private void frame1(JPanel frame) {
        frame.setBackground(new Color(255, 228, 196));
        frame.setBackground(new Color(255, 228, 196));
        frame.setForeground(new Color(255, 222, 173));
        frame.setBounds(100, 100, 695, 404);
        frame.setLayout(null);

        JButton btnNewButton = new JButton("Работать");
        btnNewButton.setFont(new Font("Acrom", Font.PLAIN, 22));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                count1++;
                lblBalance1.setText("Баланс: "+count1 + "$");
            }
        });
        btnNewButton.setBounds(238, 131, 178, 84);
        frame.add(btnNewButton);

        lblBalance1 = new JLabel("Баланс: 0$");
        lblBalance1.setFont(new Font("Acrom", Font.PLAIN, 17));
        lblBalance1.setBounds(540, 0, 139, 73);
        frame.add(lblBalance1);

        JButton btnNext = new JButton("NEXT...");
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                setTitle("Ne title text");
                cardLayout.next(contentPane); // Следующее "окно" 
            }
        });
        btnNext.setBounds(286, 270, 89, 23);
        frame.add(btnNext);
    }



    
    private int count = 0;
    private JLabel lblBalance;
    
    private void frame2(JPanel frame2) {
        frame2.setVisible(true);
        frame2.setBackground(new Color(255, 228, 196));
        frame2.setBackground(new Color(255, 228, 196));
        frame2.setForeground(new Color(255, 222, 173));
        frame2.setBounds(100, 100, 695, 404);
        frame2.setLayout(null);
        
        JButton btnNewButton = new JButton("Работать");
        btnNewButton.setFont(new Font("Acrom", Font.PLAIN, 22));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                count++;
                lblBalance.setText("Баланс: "+count + "$");
            }
        });
        btnNewButton.setBounds(238, 131, 178, 84);
        frame2.add(btnNewButton);
        
        lblBalance = new JLabel("Баланс: 0$");
        lblBalance.setFont(new Font("Acrom", Font.PLAIN, 17));
        lblBalance.setBounds(540, 0, 139, 73);
        frame2.add(lblBalance);
    }
}

cardLayout.next(contentPane); // Следующее "окно"

→ Ссылка