我想爲學校創建這個小應用程序,該應用程序會自動計算您需要通過的成績。有兩個類,框架類,基本上持有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);
}
}
你必須在'loginAction' ActionListener中放入一些實際的代碼。就像'新的登錄()'或什麼的。 – markspace 2014-09-21 18:04:14