如何使用JButton从一个JFrame转到另一个Jframe

时间:2017-03-30 02:07:15

标签: jframe jbutton

除了JFrame之外,我已经删除了所有代码,因此更容易看到。我希望能够单击一个JButton,这应该将我带到另一个类中的现有JFrame。我将在下面提供两个课程。我添加了动作监听器,但它什么也没做。谢谢你提前。

package testjframe;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 *
 * 
 */
public class TestJframe 
{
    public static void main(String[] args) 
    {
        JFrame P1Frame = new JFrame ( "Project 1" );
        
        P1Frame.pack();
        P1Frame.getContentPane().setBackground(Color.BLACK);
        P1Frame.setSize(new Dimension(800, 900));
        P1Frame.setLayout(null);
        P1Frame.setLocationRelativeTo(null);
        P1Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        P1Frame.setLayout ( new FlowLayout () );
        P1Frame.setVisible ( true );
        P1Frame.setResizable(false);
        
        JButton f2 = new JButton("Frame 2"); 
        P1Frame.add(f2);
        
        f2.addActionListener(new ActionListener()
        {
        @Override
        public void actionPerformed(ActionEvent e)
        {
         new NewForm();        
        }
        });             
    }   
}

package testjframe;

import java.awt.*;
import javax.swing.*;

/**
 *
 * 
 */
class NewForm 
{
        public static void main(String[] args) 
    {
        
        JFrame P2Frame = new JFrame ( "Project 1 second Frame" );
        
        P2Frame.pack();
        P2Frame.getContentPane().setBackground(Color.RED);
        P2Frame.setSize(new Dimension(800, 900));
        P2Frame.setLayout(null);
        P2Frame.setLocationRelativeTo(null);
        P2Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        P2Frame.setLayout ( new FlowLayout () );
        P2Frame.setVisible ( true );
        P2Frame.setResizable(false);
    }
    }
    

0 个答案:

没有答案