我想为学校创建这个小应用程序,该应用程序会自动计算您需要通过的成绩。有两个类,框架类,基本上持有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