【问题标题】:How to open new JFrame with JButton from different class如何使用不同类的 JButton 打开新的 JFrame
【发布时间】:2014-12-20 21:15:07
【问题描述】:

所以我试图让一个按钮从不同的类以及同一个类中打开一个 JFrame,就像作为不同方法的一部分一样,我完全迷失了。这是代码。我第一次尝试让按钮打开新 JFrame 的实例位于“mal”和“fem”动作侦听器。

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);


}






}

【问题讨论】:

标签: java swing jframe


【解决方案1】:

从同一类初始化 JFrame 的一种方法:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Try extends JFrame implements ActionListener{

    JButton button=new JButton("Button");
    public Try(){
      setLayout(new FlowLayout());
      button.addActionListener(this);
      add(button);
    }

public void actionPerformed(ActionEvent event){
    JFrame newFrame=new JFrame();
    newFrame.setVisible(true);
    newFrame.setSize(new Dimension(500,400));
    }

public static void main(String[] args) {

    Try t=new Try();
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.setVisible(true);
    t.setSize(new Dimension(500,400));  
 }

}

您可以在第一帧和 actionPerformed 方法中为按钮添加动作侦听器,初始化另一个帧。谢谢。

【讨论】:

    【解决方案2】:

    您所要做的就是将ActionListener 添加到JButton,一旦单击,您就可以简单地实例化新框架。类似的东西:

      fem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
    
            SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              MyNewFreame newFrame = new MyNewFrame();
              newFrame.setVisible(true);
            }
            });
    
        }
     });
    

    显然,一旦actionPerformed 发生,您想做什么完全取决于您。有什么不明白的就问吧。

    【讨论】:

      【解决方案3】:

      快速解决方法是在公共类旁边添加implements ActionListener

      该类通常会在您运行 ActionListener 时识别它。

      【讨论】:

        【解决方案4】:

        试试这个。我在 Netbeans 中运行这段代码,效果很好。

        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        
        public class NewJFrame extends javax.swing.JFrame {
        
        public NewJFrame() {
            initComponents();
            frame1();
        }
        
        
        
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
        
            jTextField1 = new javax.swing.JTextField();
        
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
            jTextField1.setText("jTextField1");
        
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(140, 140, 140)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(164, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(169, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(111, 111, 111))
            );
        
            pack();
        }// </editor-fold>                        
        
        
        public String name = "blank";
        public String gender = "blank";
        
        
        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";
                frame2();
            }
        
        });
        
        fem.addActionListener(new ActionListener(){
        
            public void actionPerformed(ActionEvent e){
                gender = "female";
                frame2();
            }
        
        });
        
        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();
        
         nameinput.setPreferredSize(new Dimension(200, 40));
        j2.setVisible(true);
        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();
        frame3();
        
            }
        
        });
        
        }
        
        
        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(true);
        f3.setSize(600,400);
        f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f3.setLocationRelativeTo(null);
        f3.add(p3);
        info.setPreferredSize(new Dimension(600, 20));
        thankyou.setPreferredSize(new Dimension(600, 20));
        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);
        
        
        }
        
        
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(false);
        
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-09-02
          • 2013-04-09
          • 1970-01-01
          • 2017-12-27
          • 2011-03-03
          • 1970-01-01
          • 2016-04-26
          相关资源
          最近更新 更多