Wie zu öffnen new JFrame mit JButton aus verschiedenen Klasse

So, ich habe versucht, die Schaltfläche öffnen Sie ein JFrame aus andere Klasse als auch innerhalb der gleichen Klasse nur als Teil einer anderen Methode, und ich bin völlig verloren. Hier ist der code. Die Fälle, in denen ich bin, zunächst den Versuch zu machen, einen Knopf öffnen Sie ein neues JFrame auf der "mal" und "fem" Action-Listener.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InteractiveName {
public String name = "blank";
public String gender = "blank";
public InteractiveName()
{
    frame1();
    frame2();
    frame3();
}

public void frame1(){


    JFrame j = new JFrame("Interactive Person Identifier");
    JPanel p = new JPanel(new GridBagLayout());
    JButton mal = new JButton("Male");
    JButton fem = new JButton("Female");


    j.setSize(400,400);
    j.setVisible(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setLocationRelativeTo(null);
    GridBagConstraints c = new GridBagConstraints();

    c.insets = new Insets(40,40,40,40);

    c.gridx = -2;
    c.gridy = 0;
    p.add(mal,c);

    c.gridx = 2;
    c.gridy = 0;
    p.add(fem,c);

    mal.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e){
            gender = "male";

        }



    });

    fem.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e){
            gender = "female";
            j2.setVisible(true);


        }



    });

    j.add(p, BorderLayout.SOUTH);
}

public void frame2(){

    JFrame j2 = new JFrame("Interactive Person Identifier");
    JPanel p2 = new JPanel(new GridBagLayout());
    JButton conf = new JButton("Confirm");
    JTextField nameinput = new JTextField();



    j2.setVisible(false);
    j2.setSize(400, 400);
    j2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j2.setLocationRelativeTo(null);
    GridBagConstraints c2 = new GridBagConstraints();


    c2.insets = new Insets(40,40,40,40);

    j2.add(p2, BorderLayout.SOUTH);

    c2.gridx = 0;
    c2.gridy = 2;       
    p2.add(nameinput, c2);

    c2.gridx = 0;
    c2.gridy = 0;
    p2.add(conf,c2);

    conf.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent ae){

             name = nameinput.getText();


        }



    });




}


public void frame3(){

    JFrame f3 = new JFrame("Confirmation");
    JPanel p3 = new JPanel(new GridBagLayout());
    JButton yes = new JButton("Yes");
    JButton no = new JButton("No");
    JLabel thankyou = new JLabel("Thank you for your participation. Is the above information correct?");
    JLabel info = new JLabel("You are a " + gender + ". You name is "+name+".");

    f3.setVisible(false);
    f3.setSize(400,400);
    f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f3.setLocationRelativeTo(null);
    f3.add(p3);


    GridBagConstraints c3 = new GridBagConstraints();

    c3.insets = new Insets(40,40,40,40);

    c3.gridx = -3;
    c3.gridy = -2;
    p3.add(yes,c3);

    yes.addActionListener(new ActionListener(){

        public void actionPerformed( ActionEvent aae){

            JOptionPane.showMessageDialog(null, "This program is over");
            System.exit(0);
        }

    });

    no.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent aeeee){

            JOptionPane.showMessageDialog(null, "You're an idiot, have fun starting over.");
            System.exit(0);


        }

    });


    c3.gridx = -3;
    c3.gridy = 2;
    p3.add(no,c3);

    c3.gridx = 0;
    c3.gridy = 0;
    p3.add(info,c3);

    c3.gridx = 0;
    c3.gridy = -1;
    p3.add(thankyou,c3);


}






}
Siehe auch Die Verwendung von Mehreren JFrames, Gute/Schlechte Praxis?
JButton mal = new JButton("Male"); JButton fem = new JButton("Female"); diese sollten JRadioButton Objekte in einer ButtonGroup,..

InformationsquelleAutor Forceful | 2014-12-20

Schreibe einen Kommentar