简体   繁体   English

通过不使用JPanel和JFrame,如果函数返回true,我还能打开新窗口吗? 否则如何向其中添加JPanel或JFrame?

[英]by not using JPanel and JFrame can i still open a new window if a function returns true? How can I add JPanel or JFrame to this otherwise?

So this is the constructor for a window 所以这是一个窗口的构造函数

setLayout(new FlowLayout());
    username = new JTextField("Username");
    password = new JTextField("Password");
    loginlogo = new JLabel(logo6);
    login = new JButton("LOGIN");
    login.setActionCommand("connect");
    login.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            //on clicking connect: the real meat of the log in page
            usernamestring = username.getText();
            passwordstring = password.getText();
            logindataurl = ("URL/aphpscript.php?u=" + username + "&p=" + password);
            try {
                userdatabase = new URL(logindataurl);
                in = new BufferedReader(new InputStreamReader(userdatabase.openStream()));
                checkusername = in.read();
                in.close();
            } catch (MalformedURLException e) {
                new error_messagebox("Malformed URL Exception", e.toString());
            } catch (IOException e) {
                new error_messagebox("Input/Output Exception reading url ", e.toString());
            }


        }
    });

    add(loginlogo, BorderLayout.NORTH);
    add(username,BorderLayout.EAST);
    add(password,BorderLayout.WEST);
    add(login,BorderLayout.SOUTH);

and heres how i call it in main 这就是我怎么称呼它

login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    login.setSize(500,350);
    login.setIconImage(logo3);
    login.setVisible(true);

but in main if a statement is returned true i would like to go onto the next box, its at this stage I realized there was such a thing as JPanel and JFrame, 但总的来说,如果语句返回true,我想转到下一个框,在此阶段,我意识到存在诸如JPanel和JFrame之类的东西,

I dont realy understand what they are for if theres two types of containers but I have heard this was the way to use an action listener to go to another window if some function allows it, I realy dont understand the existence of panel and frame at all anymore, 我真的不明白如果有两种类型的容器它们是做什么的,但是我听说这是使用动作侦听器在某些功能允许的情况下转到另一个窗口的方式,我真的根本不了解面板和框架的存在。不再

is there an easy work around? 有没有容易解决的办法? or do i have to make big changes to the code?? 还是我必须对代码进行重大更改?

A JFrame is a Swing component that holds your Java application. JFrame是包含您的Java应用程序的Swing组件。 Think of it as the window that appears on your computer. 可以将其视为计算机上显示的窗口。 A JFrame can have a menu bar (JMenuBar). JFrame可以具有菜单栏(JMenuBar)。

A JPanel is a Swing component that holds other Swing components, like labels (JLabel), text entry fields (JTextArea) and buttons (JButton). JPanel是一个Swing组件,其中包含其他Swing组件,例如标签(JLabel),文本输入字段(JTextArea)和按钮(JButton)。

A JFrame should have one main JPanel. 一个JFrame应该有一个主JPanel。 Depending on how you want to arrange your Swing components, you may need additional JPanels. 根据您想要如何安排Swing组件,您可能需要其他JPanels。

Your code appears to be putting together a login. 您的代码似乎正在组合一个登录名。 You would usually use a JDialog to perform a login. 通常,您将使用JDialog执行登录。

The flow goes like this. 流程是这样的。

  • Start your Swing GUI by invoking SwingUtilities.invokeLater(Runnable) 通过调用SwingUtilities.invokeLater(Runnable)启动Swing GUI

  • Define your JFrame, JPanel(s), and all of your Swing components. 定义您的JFrame,JPanel和所有Swing组件。

  • Show the JFrame 显示JFrame

  • Show the login JDialog 显示登录JDialog

  • If the credentials are valid, allow access to the GUI. 如果凭据有效,则允许访问GUI。

The Oracle Swing Tutorial will help you make sense of Swing. Oracle Swing教程将帮助您理解Swing。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM