简体   繁体   中英

Trying to open new Jframe form another class by clicking a Jbutton

(I just started programming in java.) So here is my code!

Main:

    import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class buttons {{


        /*Frame creation*/
        final JFrame frameKontrast = new JFrame();
        frameKontrast.setTitle("Main Menu");
        frameKontrast.setSize(500,350);
        frameKontrast.setLocationRelativeTo(null);
        frameKontrast.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        /*JPanel creation*/
        JPanel panel = new JPanel ();                                                       
        panel.setLayout(new GridLayout(0, 1));                                              




        /*Okno dialogowe*/
        final JLabel dialog = new JLabel();                                                     
        dialog.setText("<html>Jestem bardzo długim bezsensownym dialogiem mam nawet kolejną bardzo długą bezsensowną część która istnieje tylko po to, by ten tekst był ow wiele, wiele dłuższy niż wszystkie inne oraz aby zmusić się do napisania skryptu ktory robi enter w tekście. Koniec.</html>");
        JMenuBar menuKontrast = new JMenuBar();
        frameKontrast.setJMenuBar(menuKontrast); 

        /*Przyciski wyboru*/
        final JButton przyciskb = new JButton();
        przyciskb.setText("Exit");



        final JButton przyciska = new JButton();                                                    
            przyciska.setText("Start the game");







        /*Rozwijane menu (zadania)*/
        JMenu objectives = new JMenu("Objectives");
        JMenuItem zad1 = new JMenuItem("Zadanie numer 1");
        JMenuItem zad2 = new JMenuItem("Zadanie numer 2");
        JMenuItem zad3 = new JMenuItem("Zadanie numer 3");




        /*Dołączanie obiektów do GUI*/
        panel.add(dialog);                                                                  
        menuKontrast.add(objectives);
        objectives.add(zad1);
        objectives.add(zad2);
        objectives.add(zad3);
        ButtonGroup group = new ButtonGroup();                                              
        group.add(przyciska);
        group.add(przyciskb);
        panel.add(przyciska);
        panel.add(przyciskb);
        frameKontrast.getContentPane().add(panel);
        frameKontrast.getContentPane().add(panel);
        frameKontrast.setVisible(true);
    }};

And here is a new window that I want to open by the przyciska button:

    public class frameKontrastGame {{
    {
        JFrame frameKontrastGame = new JFrame();
        frameKontrastGame.setTitle("Kontrast");
        frameKontrastGame.setSize(1000,700);
        frameKontrastGame.setLocationRelativeTo(null);
        frameKontrastGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel ();                                                       
        panel.setLayout(new GridLayout(0, 1));



        JLabel dialog = new JLabel();                                                       
        dialog.setText("<html>Kontrast</html>");
        JMenuBar menuKontrast = new JMenuBar();
        frameKontrastGame.setJMenuBar(menuKontrast); 
        frameKontrastGame.setVisible(true);
        panel.add(dialog);
        frameKontrastGame.getContentPane().add(panel);
        frameKontrastGame.getContentPane().add(panel);
        frameKontrastGame.setVisible(true);
    }}}

I know that its not the best code. It's working and I'm pleased with it. My problem is that I cant use "this" listeners. I hope that someone have a little time to help me with this simple problem. Thanks for reading all of this.

You can add an ActionListener to a button by typing

przyciska.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ev) {
                new frameKontrastGame();                    
            }
});

(assuming that the code you've written in the frameKontraestGame class starts a jframe within its constructor)

In case you want to handle actions from more than 1 buttons then you can write another class eg. ActionHandler (or whatever name you want to give the class) which will implement the Interface ActionListener. Then within the actionPerformed function you can detect the button that was preesed by using the ev.getSource() (ev is your ActionEvent variable) method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM