Java Swing在同一窗口中打开新内容

时间:2017-08-31 21:44:51

标签: java swing user-interface

我是布鲁塞尔的学生javadevelopment。 该课程的重点在于后端和webapp,现在我们正在覆盖spring(核心,企业和mvc,我们将跳过安全和消息传递)。

我希望能够创建独立的图形应用程序,所以我开始了一个侧面项目,对于一个非盈利的历史组织,我想要一个连接本地数据库的应用程序,让他们管理他们的成员和图书馆。中学将会有一个webapp,让网站访问者在图书馆中搜索可用性并保留项目。

我从adminapp开始(管理成员和libary)。 它首先是一个简单的用户登录文本域用户,另一个密码和一个登录按钮。点击它会存储用户名和密码,并应在同一帧中打开另一个JPanel,这是用户在应用程序中导航的方式。 当我点击登录按钮时,会打开一个新的空框架(不应该这样),当我最大化我的新框架并再次最小化它时,我的旧框架显示它应该是什么。 我知道我称之为我的第二个Jpanel的方式并不是最优雅的方式,但是我没有预先知道摇摆的开始,我只是开始在互联网上进行实验和困惑并拼凑起来。

无论如何,这是我得到的nullpointer异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at  com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent
(GridLayoutManager.java:134)
at java.awt.Container.addImpl(Container.java:1127)
at java.awt.Container.add(Container.java:417)
at be.kokw.views.MainApp$1.actionPerformed(MainApp.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed
(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl
.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl
.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.
doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters
(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter
(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

My MainClass:

public class MainApp{
private JButton login;
private JPanel panel1;

private JLabel labelForUser;
private JTextField userField;
private JLabel labelForPassword;
private JTextField passwordField;
private String user;
private String password;
static JFrame frame = new JFrame("KokwApp");

public String getUser() {
    return user;
}

public String getPassword() {
    return password;
}


public MainApp() {

    login.addActionListener(new ActionListener() {
        /**
         * Invoked when an action occurs.
         *
         * @param e
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            user = userField.getText();
            password = passwordField.getText();
            DbSelection db = new DbSelection();
            frame.getContentPane().removeAll();
            frame.getContentPane().add(db.getPanel1());
            frame.repaint();



        }
    });
}

public static void main(String[] args) {
    frame.setContentPane(new MainApp().panel1);
    frame.pack();
    frame.setVisible(true);
}
}

今天我问老师...... 他的回答是:我不喜欢使用秋千。阅读javafx ... Lol

所以任何有关错误的提示都会受到欢迎。 确切地说,我使用intelij。 最后,抱歉我的糟糕的gramar,但正如我在开始时说的,我是比利时人

0 个答案:

没有答案