点击按钮打开新屏幕或页面(java-eclipse)

时间:2014-06-10 17:48:30

标签: java button window

我有一个标题和几个按钮的主页当我点击按钮时,我无法打开一个新窗口。这是我对主页的代码以及具有下一个屏幕的类我试图打开为相似的东西打开。 NewTicketWindow类也附属于此。任何帮助表示赞赏。

public class Home
{    

private JFrame frame;
JInternalFrame internalFrame;

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

/**
 * Create the application.
 */
public Home()
{
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize()
{
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel title1 = new JLabel("City of Murphy");

    JLabel title2 = new JLabel("Traffic Ticket Input System");

    JButton newTicketButton = new JButton("New Ticket");
    newTicketButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {

        }

    });

    JButton payTicketButton = new JButton("Make a Payment");

    JButton reportButtton = new JButton("Ticket Report");

    JButton exitButton = new JButton("Exit");
    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}

第二课(我想在按下newticket按钮时打开的屏幕

public class NewTicketWindow extends JFrame
{

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                NewTicketWindow frame = new NewTicketWindow();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public NewTicketWindow()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
    GroupLayout gl_contentPane = new GroupLayout(contentPane);

}

2 个答案:

答案 0 :(得分:1)

只需将这些行添加到您执行的操作代码中即可 -

NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);

答案 1 :(得分:1)

ActionListener的{​​{1}}应该通过调用NewTicketWindow的构造函数来创建新框架(您在newTicketButton的{​​{1}}中执行的操作相同):

main

您还需要将newTicketButton添加到主窗口:

NewTicketWindow

相关问题