Java 提交按钮的Swing方法

Java 提交按钮的Swing方法,java,swing,frame,jbutton,Java,Swing,Frame,Jbutton,如何关闭当前帧(Frame1)并打开已创建的新帧(Frame2),并通过单击按钮将数据从Frame1传递到Frame2?使用1。或者一个JFrame和一个或多个JDialog2个实例 使用1。或者一个JFrame和一个或多个JDialog2个实例 实现这一点的最佳方法是由@Andrew Thompson告诉您的。 而另一种实现方式,就是代码中描述的问题动机。在这里,当您为新的JFrame创建对象时,您必须将另一个类中需要的东西作为参数传递给另一个类,或者您可以简单地传递对象(这样您就可以

如何关闭当前帧(Frame1)并打开已创建的新帧(Frame2),并通过单击按钮将数据从Frame1传递到Frame2?

使用1。或者一个
JFrame
和一个或多个
JDialog
2个实例

  • 使用1。或者一个
    JFrame
    和一个或多个
    JDialog
    2个实例


  • 实现这一点的最佳方法是由@Andrew Thompson告诉您的。 而另一种实现方式,就是代码中描述的问题动机。在这里,当您为新的
    JFrame
    创建对象时,您必须将另一个类中需要的东西作为参数传递给另一个类,或者您可以简单地传递对象(这样您就可以一次性地将所有东西传递给另一个类)

    下面是一个示例代码,以获得一些帮助:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TwoFramesExample
    {
        public JFrame frame;
        private JPanel panel;
        private JButton button;
        private JTextField tfield;
        private SecondFrame secondFrame;
    
        public TwoFramesExample()
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            tfield = new JTextField(10);
            tfield.setBackground(Color.BLACK);
            tfield.setForeground(Color.WHITE);
    
            button = new JButton("NEXT");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    // Here we are passing the contents of the JTextField to another class
                    // so that it can be shown on the label of the other JFrame.
                    secondFrame = new SecondFrame(tfield.getText());                
                    frame.dispose();
                }
            });
    
            frame.setContentPane(panel);
            panel.add(tfield, BorderLayout.CENTER); 
            panel.add(button, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TwoFramesExample();
                }
            });
        }
    }
    
    class SecondFrame
    {
        private JFrame frame;
        private JPanel panel;
        private JLabel label;
        private JButton button;
        private TwoFramesExample firstFrame;
    
        public SecondFrame(String text)
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            label = new JLabel(text);
            button = new JButton("BACK");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    firstFrame = new TwoFramesExample();
                    frame.dispose();
                }
            });
    
            frame.setContentPane(panel);
            panel.add(label, BorderLayout.CENTER);
            panel.add(button, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    希望这能有所帮助


    @Andrew Thompson向您介绍了实现这一目标的最佳方法。 而另一种实现方式,就是代码中描述的问题动机。在这里,当您为新的
    JFrame
    创建对象时,您必须将另一个类中需要的东西作为参数传递给另一个类,或者您可以简单地传递对象(这样您就可以一次性地将所有东西传递给另一个类)

    下面是一个示例代码,以获得一些帮助:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TwoFramesExample
    {
        public JFrame frame;
        private JPanel panel;
        private JButton button;
        private JTextField tfield;
        private SecondFrame secondFrame;
    
        public TwoFramesExample()
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            tfield = new JTextField(10);
            tfield.setBackground(Color.BLACK);
            tfield.setForeground(Color.WHITE);
    
            button = new JButton("NEXT");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    // Here we are passing the contents of the JTextField to another class
                    // so that it can be shown on the label of the other JFrame.
                    secondFrame = new SecondFrame(tfield.getText());                
                    frame.dispose();
                }
            });
    
            frame.setContentPane(panel);
            panel.add(tfield, BorderLayout.CENTER); 
            panel.add(button, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TwoFramesExample();
                }
            });
        }
    }
    
    class SecondFrame
    {
        private JFrame frame;
        private JPanel panel;
        private JLabel label;
        private JButton button;
        private TwoFramesExample firstFrame;
    
        public SecondFrame(String text)
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            panel = new JPanel();
            panel.setLayout(new BorderLayout());
    
            label = new JLabel(text);
            button = new JButton("BACK");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    firstFrame = new TwoFramesExample();
                    frame.dispose();
                }
            });
    
            frame.setContentPane(panel);
            panel.add(label, BorderLayout.CENTER);
            panel.add(button, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    希望这能有所帮助


    问候

    +1<代码>帧.setLocationRelativeTo(空)另请参见。非常适合这种情况@安德烈·霍姆普森:谢谢,这正是我想要的,因为我刚刚从你的一个答案中发现了这件事:-)问候+1<代码>帧.setLocationRelativeTo(空)另请参见。非常适合这种情况@安德烈·霍姆普森:谢谢你,这正是我想要的,因为我刚刚从你关于这件事的某个答案中发现:-)请你再详细说明一下你的问题。“情况如何?”Waqasahmed先生问得很好+你能再详细说明一下你的问题吗。“情况如何?”Waqasahmed先生问得很好+1.