我应该如何从一个使用应用程序窗口的窗口导航到另一个应用程序窗口中的另一个框架?

时间:2017-04-25 15:41:58

标签: java swing

我正在开发一个应用程序,我已经创建了不同的应用程序窗口。现在我对如何使用按钮将一个链接到另一个时感到困惑,就是当我单击一个按钮时,它必须导航到另一个应用程序窗口。我用相同文件中的帧尝试了它,但这并不能满足我的需要。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.UIManager;
import java.awt.event.*;
public class Start {

private JFrame frame;
private JTextField txtLogInTo;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Start window = new Start();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(SystemColor.inactiveCaption);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("USER");
    btnNewButton.setBackground(SystemColor.activeCaption);
    btnNewButton.setBounds(152, 81, 132, 23);
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("BUS ADMIN");
    btnNewButton_1.setBackground(SystemColor.activeCaption);
    btnNewButton_1.setBounds(152, 131, 132, 23);
    frame.getContentPane().add(btnNewButton_1);


    JButton btnNewButton_2 = new JButton("SYSTEM ADMIN");
    btnNewButton_2.setBackground(SystemColor.activeCaption);
    btnNewButton_2.setBounds(152, 179, 132, 23);
    frame.getContentPane().add(btnNewButton_2);

    txtLogInTo = new JTextField();
    txtLogInTo.setBounds(95, 27, 252, 36);
    txtLogInTo.setBackground(SystemColor.inactiveCaption);
    txtLogInTo.setHorizontalAlignment(SwingConstants.CENTER);
    txtLogInTo.setFont(new Font("Tekton Pro Cond", Font.BOLD | Font.ITALIC, 20));
    txtLogInTo.setText("LOG IN TO ENTER THE SYSTEM");
    frame.getContentPane().add(txtLogInTo);
    txtLogInTo.setColumns(10);
     class handler implements ActionListener
        {
            //must implement method
            //This is triggered whenever the user clicks the login button
            public void actionPerformed(ActionEvent ae)
            {
                if(btnNewButton.isEnabled())
                {User u;

                }

                }//if
            }//method

        }//inner class

}

以上程序是我点击以下程序中的按钮时必须导航的地方

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class User {

private JFrame frame,frame2;
private JTextField txtWelcomeUser;
private JTextField txtEnterYourDetails;
private JTextField txtName;
private JTextField txtAge;
private JTextField txtPhoneNo;
private JTextField txtMailId;
private JTextField txtStart;
private JTextField txtDestination;
private JTextArea textArea_4;
private JTextArea textArea_5;
private JTextField txtEn;
   private ActionListener action;

public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                User window = new User();
                window.frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}


public User() {
    initialize();
}


private void initialize() {
    frame = new JFrame();
    frame.setBounds(0, 0, 750, 550);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);



    txtWelcomeUser = new JTextField();
    txtWelcomeUser.setBackground(SystemColor.inactiveCaption);
    txtWelcomeUser.setFont(new Font("Tahoma", Font.PLAIN, 26));
    txtWelcomeUser.setText("Welcome user");
    txtWelcomeUser.setBounds(176, 11, 173, 52);
    frame.getContentPane().add(txtWelcomeUser);
    txtWelcomeUser.setColumns(10);

    txtEnterYourDetails = new JTextField();
    txtEnterYourDetails.setBackground(SystemColor.activeCaption);
    txtEnterYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 20));
    txtEnterYourDetails.setText("Enter your details");
    txtEnterYourDetails.setBounds(176, 74, 173, 36);
    frame.getContentPane().add(txtEnterYourDetails);
    txtEnterYourDetails.setColumns(10);

    txtName = new JTextField();
    txtName.setBackground(SystemColor.inactiveCaption);
    txtName.setText("Name");
    txtName.setBounds(93, 136, 86, 20);
    frame.getContentPane().add(txtName);
    txtName.setColumns(10);

    txtAge = new JTextField();
    txtAge.setBackground(SystemColor.inactiveCaption);
    txtAge.setText("Age");
    txtAge.setBounds(93, 167, 86, 20);
    frame.getContentPane().add(txtAge);
    txtAge.setColumns(10);

    txtPhoneNo = new JTextField();
    txtPhoneNo.setBackground(SystemColor.inactiveCaption);
    txtPhoneNo.setText("Phone No");
    txtPhoneNo.setBounds(93, 198, 86, 20);
    frame.getContentPane().add(txtPhoneNo);
    txtPhoneNo.setColumns(10);

    txtMailId = new JTextField();
    txtMailId.setBackground(SystemColor.inactiveCaption);
    txtMailId.setText("Mail id");
    txtMailId.setBounds(93, 229, 86, 20);
    frame.getContentPane().add(txtMailId);
    txtMailId.setColumns(10);

    JTextArea textArea = new JTextArea();
    textArea.setBounds(236, 134, 124, 20);
    frame.getContentPane().add(textArea);

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setBounds(236, 165, 124, 20);
    frame.getContentPane().add(textArea_1);

    JTextArea textArea_2 = new JTextArea();
    textArea_2.setBounds(236, 196, 124, 20);
    frame.getContentPane().add(textArea_2);

    JTextArea textArea_3 = new JTextArea();
    textArea_3.setBounds(236, 227, 124, 20);
    frame.getContentPane().add(textArea_3);

    txtStart = new JTextField();
    txtStart.setBackground(SystemColor.inactiveCaption);
    txtStart.setText("Start ");
    txtStart.setBounds(93, 260, 86, 20);
    frame.getContentPane().add(txtStart);
    txtStart.setColumns(10);

    txtDestination = new JTextField();
    txtDestination.setBackground(SystemColor.inactiveCaption);
    txtDestination.setText("Destination");
    txtDestination.setBounds(93, 291, 86, 20);
    frame.getContentPane().add(txtDestination);
    txtDestination.setColumns(10);

    textArea_4 = new JTextArea();

    textArea_4.setBounds(236, 258, 124, 20);
    frame.getContentPane().add(textArea_4);

    textArea_5 = new JTextArea();
    textArea_5.setBounds(236, 289, 124, 20);
    frame.getContentPane().add(textArea_5);



    JPanel panel = new JPanel();
    panel.setForeground(new Color(0, 0, 0));
    panel.setBackground(SystemColor.inactiveCaption);
    panel.setBounds(433, 213, 124, 115);
    frame.getContentPane().add(panel);

    txtEn = new JTextField();
    txtEn.setBackground(SystemColor.inactiveCaption);
    txtEn.setText("Enter Bus No");
    txtEn.setBounds(93, 322, 86, 20);
    frame.getContentPane().add(txtEn);
    txtEn.setColumns(10);

    JTextArea textArea_6 = new JTextArea();
    textArea_6.setBounds(236, 320, 124, 20);
    frame.getContentPane().add(textArea_6);

    JButton btnBookTicket = new JButton("Book Ticket");
    btnBookTicket.setBackground(SystemColor.activeCaption);
    btnBookTicket.setBounds(176, 376, 116, 23);
    frame.getContentPane().add(btnBookTicket);

    JPopupMenu popupMenu_1 = new JPopupMenu();
    popupMenu_1.setBounds(327, 376, 200, 50);
    frame.getContentPane().add(popupMenu_1);



                    if(!textArea.isEnabled()||!textArea_2.isEnabled()||!textArea_3.isEnabled()||!textArea_3.isEnabled()||!textArea_4.isEnabled()||!textArea_5.isEnabled()||!textArea_6.isEnabled())
                {


                        JOptionPane.showMessageDialog(null, "You have not entered a few details","UnSuccessful",
                        JOptionPane.INFORMATION_MESSAGE);

                }
                if(btnBookTicket.isEnabled())
                {

                    btnBookTicket.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e) {



                        JOptionPane.showMessageDialog(null, "You have booked successfully","Success",
                        JOptionPane.INFORMATION_MESSAGE);

                                    JButton button = (JButton) e.getSource();


                                    if (button == btnBookTicket)
                                    {
                                        frame2 = new JFrame("FRAME 2");
                                        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
                                        frame2.setLocationByPlatform(true);

                                        JPanel contentPane2 = new JPanel();
                                        contentPane2.setBackground(Color.DARK_GRAY);
                                        JButton btnBookTicket1 = new JButton("Cancel Ticket");
                                        btnBookTicket1.setBackground(SystemColor.activeCaption);

                                        btnBookTicket1.setBounds(0, 30, 35, 23);
                                        contentPane2.add(btnBookTicket1);
                                        frame2.getContentPane().add(contentPane2);
                                        frame2.setSize(600, 600);
                                        frame2.setVisible(true);
                                        frame.setVisible(false);


                                    }



                            btnBookTicket.addActionListener(action);



                       }
                 });


            }
        }
    }

0 个答案:

没有答案