简体   繁体   English

通过单击Jbutton尝试打开另一个类的新Jframe

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

(I just started programming in java.) So here is my code! (我刚开始用java编程。)所以这是我的代码!

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: 这是一个我想通过przyciska按钮打开的新窗口:

    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 您可以通过键入将ActionListener添加到按钮

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) (假设您在frameKontraestGame类中编写的代码在其构造函数中启动了一个jframe)

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. 将实现接口ActionListener的ActionHandler(或您希望赋予类的任何名称)。 Then within the actionPerformed function you can detect the button that was preesed by using the ev.getSource() (ev is your ActionEvent variable) method. 然后在actionPerformed函数中,您可以使用ev.getSource()(ev是您的ActionEvent变量)方法检测已预先处理的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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