2014-09-21 83 views
1

我想为学校创建这个小应用程序,该应用程序会自动计算您需要通过的成绩。有两个类,框架类,基本上持有jframe和菜单栏,以及登录类(显然)处理登录表单。从菜单栏打开登录屏幕

现在,当我点击菜单中的登录按钮时,我想要一个新窗口弹出并显示登录表单,然后登录表单将继续加载到成绩中。 虽然我不知道该怎么做,但我尝试过的一切都失败了。

我该怎么做?

代码Login类:

import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

public class Login { 

    public static void placeComponents(JPanel panel) { 

     panel.setLayout(null); 

     JLabel userLabel = new JLabel("Username: "); 
     userLabel.setBounds(100, 10, 80, 25); 
     panel.add(userLabel); 

     JTextField userText = new JTextField(20); 
     userText.setBounds(100, 10, 160, 25); 
     panel.add(userText); 

     JLabel passwordLabel = new JLabel("Password: "); 
     passwordLabel.setBounds(10, 40, 80, 25); 
     panel.add(passwordLabel); 

     JPasswordField passwordText = new JPasswordField(20); 
     passwordText.setBounds(100, 40, 160, 25); 
     panel.add(passwordText); 

     JButton loginButton = new JButton("Login"); 
     loginButton.setBounds(10, 80, 80, 25); 
     panel.add(loginButton); 
    } 

} 

代码为我Frame类:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 

@SuppressWarnings("serial") 
public class Frame extends JFrame { 

    Login login = new Login(); 

    public Frame() { 

     setTitle("Grade calculation"); 
     setSize(300, 300); 

     JMenuBar menuBar = new JMenuBar(); 

     setJMenuBar(menuBar); 

     JMenu fileMenu = new JMenu("File"); 
     JMenu editMenu = new JMenu("Edit"); 
     menuBar.add(fileMenu); 
     menuBar.add(editMenu); 

     JMenuItem loginAction = new JMenuItem("Log in"); 
     JMenuItem exitAction = new JMenuItem("Close"); 
     JMenuItem cutAction = new JMenuItem("Cut"); 
     JMenuItem copyAction = new JMenuItem("Copy"); 
     JMenuItem pasteAction = new JMenuItem("Paste"); 

     JCheckBoxMenuItem checkAction = new JCheckBoxMenuItem("Inloggegevens onthouden"); 
     fileMenu.add(loginAction); 
     fileMenu.add(checkAction); 
     fileMenu.add(exitAction); 
     editMenu.add(cutAction); 
     editMenu.add(copyAction); 
     editMenu.add(pasteAction); 

     loginAction.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

      } 

     }); 

     exitAction.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       dispose(); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     Frame me = new Frame(); 
     me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     me.setResizable(false); 
     me.setLocationRelativeTo(null); 
     me.setVisible(true); 
    } 
} 
+0

你必须在'loginAction' ActionListener中放入一些实际的代码。就像'新的登录()'或什么的。 – markspace 2014-09-21 18:04:14

回答

1

你可以在的Java Swing使用弹出窗口,看到一些信息here

这是上面链接的第一个例子,只是创建一个简单的消息对话窗口。

//default title and icon 
    JOptionPane.showMessageDialog(frame, 
     "Eggs are not supposed to be green."); 

你可以把你在弹出窗口需要的任何输入字段。

+0

ALl正确,评论已删除。这太好了! – 2014-09-21 19:56:22

+0

不错!这些信息帮助了我很多,明天我会改变一些事情! – Goldfishblub 2014-09-21 21:14:29

0

下面是一个例子,可能可以指出你在正确的方向做出太多的项目。我不会将我的图形用户界面分成多个类,以防止您的框架无法控制其内部发生的情况,还有一些其他原因。你也可能想为你的数据创建另一个类。如果您只是想在新框架中显示登录详细信息和成绩,那么您可以轻松创建另一个扩展JFrame的类。

代码:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

@SuppressWarnings("serial") 
public class Frame extends JFrame implements ActionListener{ 

    private JMenuItem loginAction; //defined here to allow access in actionPerformed(). 
    private JMenuItem exitAction; 

    private JButton loginButton; 

    public Frame() { 

     setTitle("Grade calculation"); 
     setSize(300, 300); 

     JMenuBar menuBar = new JMenuBar(); 

     setJMenuBar(menuBar); 

     JMenu fileMenu = new JMenu("File"); 
     JMenu editMenu = new JMenu("Edit"); 
     menuBar.add(fileMenu); 
     menuBar.add(editMenu); 

     loginAction = new JMenuItem("Log in"); 
     exitAction = new JMenuItem("Close"); 
     JMenuItem cutAction = new JMenuItem("Cut"); 
     JMenuItem copyAction = new JMenuItem("Copy"); 
     JMenuItem pasteAction = new JMenuItem("Paste"); 

     JCheckBoxMenuItem checkAction = new JCheckBoxMenuItem("Inloggegevens onthouden"); 
     fileMenu.add(loginAction); 
     fileMenu.add(checkAction); 
     fileMenu.add(exitAction); 
     editMenu.add(cutAction); 
     editMenu.add(copyAction); 
     editMenu.add(pasteAction); 

     loginAction.addActionListener(this); 

     exitAction.addActionListener(this); 
    } 

    public JPanel loginPanel() 
    { 
     JPanel panel = new JPanel(); 

     panel.setLayout(null); 

     JLabel userLabel = new JLabel("Username: "); 
     userLabel.setBounds(10, 10, 80, 25); 
     panel.add(userLabel); 

     JTextField userText = new JTextField(20); 
     userText.setBounds(100, 10, 160, 25); 
     panel.add(userText); 

     JLabel passwordLabel = new JLabel("Password: "); 
     passwordLabel.setBounds(10, 40, 80, 25); 
     panel.add(passwordLabel); 

     JPasswordField passwordText = new JPasswordField(20); 
     passwordText.setBounds(100, 40, 160, 25); 
     panel.add(passwordText); 

     loginButton = new JButton("Login"); 
     loginButton.addActionListener(this); 
     loginButton.setBounds(10, 80, 80, 25); 
     panel.add(loginButton); 

     return panel;   
    } 

    public void close() 
    { 
     System.exit(0); 
    } 

    public void addLogin() 
    { 
     add(loginPanel()); 
     validate(); 
    } 

    //action method 
    @Override 
    public void actionPerformed(ActionEvent e) { 


     if (e.getSource() == loginButton) 
      System.out.println("loginButton was pressed so I should do something, maybe?"); 

     if (e.getSource() == loginAction) 
      addLogin(); 

     if (e.getSource() == exitAction) 
      close(); 
    }  

    public static void main(String[] args) { 
     Frame me = new Frame(); 
     me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     me.setResizable(false); 
     me.setLocationRelativeTo(null); 
     me.setVisible(true); 
    } 
} 

你会发现,我创建了loginPanel()方法调用时返回一个JPanel。当您选择loginAction菜单项时,它会将面板添加到框架。如果你想用另一个面板替换它,它不是那么容易被删除,但我相信你想从这个框架转向一个纯粹显示数据的框架。

这可能不是您希望与您的项目一起使用的方向。如果没有,留下评论,我会看看我是否能够提供帮助。

+1

如果你要遵循逻辑,最好使用CardLayout – MadProgrammer 2014-09-21 20:40:00

+0

伟大的建议。希望OP可以对他的程序流进行排序并且可以使用它。 – MarGar 2014-09-21 20:51:06

+0

我还没有开始实际创建登录面板,但这将是一个不错的开始!谢谢你的帮助! – Goldfishblub 2014-09-21 21:16:21