2014-09-21 87 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