做了一个图书管理系统,加了四个菜单:系统设置(登录,退出),用户管理,借阅管理,图书管理
主窗体设置在mainframe类里,出示状态把用户管理,借阅管理,图书管理设为灰色不可用,
想登陆之后,再让他们可用,登录在类login中,该怎样做
部分代码如下
public class MainFrame extends JFrame implements ActionListener { JPanel panelWelcome;
JPanel panel;
JMenuBar menubar;
JMenu SystemMMenu, BookMMenu, BorrowMMenu, ReaderMMenu;
JMenuItem LoginMenuItem, ExitMenuItem, BookAddMenuItem, BorrowBookMenuItem,
ReturnBookMenuItem, BorrowerMMenuItem;
// JButton Gobn;
JLabel label;
ImageIcon icon;
JLabel labelw; public MainFrame() throws HeadlessException {
super("图书管理系统 V1.0");
menubar = new JMenuBar();
setJMenuBar(menubar);
// Gobn = new JButton("进入");
// Gobn.setSize(20, 10);
// Gobn.addActionListener(this);
icon=new ImageIcon("src:\\libary.jpg");
label=new JLabel(icon);
// label=new JLabel("图书管理系统");
// label = new JLabel(new ImageIcon("src:\\Jeff:\\libary.jpg"));
labelw=new JLabel("作者: ");
// ****系统管理****
SystemMMenu = new JMenu("系统管理");
LoginMenuItem = new JMenuItem("登录");
ExitMenuItem = new JMenuItem("退出");
SystemMMenu.add(LoginMenuItem);
SystemMMenu.add(ExitMenuItem);
LoginMenuItem.addActionListener(this);
ExitMenuItem.addActionListener(this);
menubar.add(SystemMMenu);
// ****图书管理****
BookMMenu = new JMenu("图书管理");
BookAddMenuItem = new JMenuItem("图书录入");
BookMMenu.add(BookAddMenuItem);
BookAddMenuItem.addActionListener(this);
menubar.add(BookMMenu);
// ****借阅管理****
BorrowMMenu = new JMenu("借阅管理");
BorrowBookMenuItem = new JMenuItem("借书");
ReturnBookMenuItem = new JMenuItem("还书");
BorrowMMenu.add(BorrowBookMenuItem);
BorrowMMenu.add(ReturnBookMenuItem);
BorrowBookMenuItem.addActionListener(this);
ReturnBookMenuItem.addActionListener(this);
menubar.add(BorrowMMenu);
// ****读者管理****
ReaderMMenu = new JMenu("读者管理");
BorrowerMMenuItem = new JMenuItem("借阅人管理");
ReaderMMenu.add(BorrowerMMenuItem);
BorrowerMMenuItem.addActionListener(this);
menubar.add(ReaderMMenu);
// ********************
// ********************
this.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(labelw);
this.add(panel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setSize(400, 300);
setVisible(true); ****设置菜单初始状态****
BookMMenu.setEnabled(false);
BorrowMMenu.setEnabled(false);
ReaderMMenu.setEnabled(false); }
public class Login extends JFrame implements ActionListener { JPanel Loginpanel1, Loginpanel2;
JLabel UserLabel, PasswordLabel;
JTextField UserTextField;
JPasswordField PasswordTextField;
JButton YesBtn, CancelBtn; public Login() {
super("登陆"); UserLabel = new JLabel("用户名", JLabel.CENTER);
PasswordLabel = new JLabel("密码", JLabel.CENTER);
UserTextField = new JTextField(10);
PasswordTextField = new JPasswordField(10);
YesBtn = new JButton("确定");
CancelBtn = new JButton("取消");
YesBtn.addActionListener(this);
CancelBtn.addActionListener(this);
Loginpanel1 = new JPanel();
Loginpanel1.setLayout(new GridLayout(2, 2));
Loginpanel2 = new JPanel();
setLayout(new BorderLayout());
Loginpanel1.add(UserLabel);
Loginpanel1.add(UserTextField);
Loginpanel1.add(PasswordLabel);
Loginpanel1.add(PasswordTextField);
add(Loginpanel1, BorderLayout.CENTER);
Loginpanel2.add(YesBtn);
Loginpanel2.add(CancelBtn);
add(Loginpanel2, BorderLayout.SOUTH);
// setSize(300, 300);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == CancelBtn) { this.dispose();
} else {
char[] password = PasswordTextField.getPassword();
String passwordSTR = new String(password);
if (UserTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不可为空!");
return;
}
if (passwordSTR.equals("")) {
JOptionPane.showMessageDialog(null, "密码不可为空!");
return; }
if ((UserTextField.getText().trim().equals("admin"))
&& (passwordSTR.equals("admin"))) { this.dispose(); } }
}}
主窗体设置在mainframe类里,出示状态把用户管理,借阅管理,图书管理设为灰色不可用,
想登陆之后,再让他们可用,登录在类login中,该怎样做
部分代码如下
public class MainFrame extends JFrame implements ActionListener { JPanel panelWelcome;
JPanel panel;
JMenuBar menubar;
JMenu SystemMMenu, BookMMenu, BorrowMMenu, ReaderMMenu;
JMenuItem LoginMenuItem, ExitMenuItem, BookAddMenuItem, BorrowBookMenuItem,
ReturnBookMenuItem, BorrowerMMenuItem;
// JButton Gobn;
JLabel label;
ImageIcon icon;
JLabel labelw; public MainFrame() throws HeadlessException {
super("图书管理系统 V1.0");
menubar = new JMenuBar();
setJMenuBar(menubar);
// Gobn = new JButton("进入");
// Gobn.setSize(20, 10);
// Gobn.addActionListener(this);
icon=new ImageIcon("src:\\libary.jpg");
label=new JLabel(icon);
// label=new JLabel("图书管理系统");
// label = new JLabel(new ImageIcon("src:\\Jeff:\\libary.jpg"));
labelw=new JLabel("作者: ");
// ****系统管理****
SystemMMenu = new JMenu("系统管理");
LoginMenuItem = new JMenuItem("登录");
ExitMenuItem = new JMenuItem("退出");
SystemMMenu.add(LoginMenuItem);
SystemMMenu.add(ExitMenuItem);
LoginMenuItem.addActionListener(this);
ExitMenuItem.addActionListener(this);
menubar.add(SystemMMenu);
// ****图书管理****
BookMMenu = new JMenu("图书管理");
BookAddMenuItem = new JMenuItem("图书录入");
BookMMenu.add(BookAddMenuItem);
BookAddMenuItem.addActionListener(this);
menubar.add(BookMMenu);
// ****借阅管理****
BorrowMMenu = new JMenu("借阅管理");
BorrowBookMenuItem = new JMenuItem("借书");
ReturnBookMenuItem = new JMenuItem("还书");
BorrowMMenu.add(BorrowBookMenuItem);
BorrowMMenu.add(ReturnBookMenuItem);
BorrowBookMenuItem.addActionListener(this);
ReturnBookMenuItem.addActionListener(this);
menubar.add(BorrowMMenu);
// ****读者管理****
ReaderMMenu = new JMenu("读者管理");
BorrowerMMenuItem = new JMenuItem("借阅人管理");
ReaderMMenu.add(BorrowerMMenuItem);
BorrowerMMenuItem.addActionListener(this);
menubar.add(ReaderMMenu);
// ********************
// ********************
this.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(labelw);
this.add(panel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setSize(400, 300);
setVisible(true); ****设置菜单初始状态****
BookMMenu.setEnabled(false);
BorrowMMenu.setEnabled(false);
ReaderMMenu.setEnabled(false); }
public class Login extends JFrame implements ActionListener { JPanel Loginpanel1, Loginpanel2;
JLabel UserLabel, PasswordLabel;
JTextField UserTextField;
JPasswordField PasswordTextField;
JButton YesBtn, CancelBtn; public Login() {
super("登陆"); UserLabel = new JLabel("用户名", JLabel.CENTER);
PasswordLabel = new JLabel("密码", JLabel.CENTER);
UserTextField = new JTextField(10);
PasswordTextField = new JPasswordField(10);
YesBtn = new JButton("确定");
CancelBtn = new JButton("取消");
YesBtn.addActionListener(this);
CancelBtn.addActionListener(this);
Loginpanel1 = new JPanel();
Loginpanel1.setLayout(new GridLayout(2, 2));
Loginpanel2 = new JPanel();
setLayout(new BorderLayout());
Loginpanel1.add(UserLabel);
Loginpanel1.add(UserTextField);
Loginpanel1.add(PasswordLabel);
Loginpanel1.add(PasswordTextField);
add(Loginpanel1, BorderLayout.CENTER);
Loginpanel2.add(YesBtn);
Loginpanel2.add(CancelBtn);
add(Loginpanel2, BorderLayout.SOUTH);
// setSize(300, 300);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == CancelBtn) { this.dispose();
} else {
char[] password = PasswordTextField.getPassword();
String passwordSTR = new String(password);
if (UserTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不可为空!");
return;
}
if (passwordSTR.equals("")) {
JOptionPane.showMessageDialog(null, "密码不可为空!");
return; }
if ((UserTextField.getText().trim().equals("admin"))
&& (passwordSTR.equals("admin"))) { this.dispose(); } }
}}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class MainFrame extends JFrame implements ActionListener { JPanel panelWelcome; JPanel panel; JMenuBar menubar; JMenu SystemMMenu, BookMMenu, BorrowMMenu, ReaderMMenu; JMenuItem LoginMenuItem, ExitMenuItem, BookAddMenuItem, BorrowBookMenuItem,
ReturnBookMenuItem, BorrowerMMenuItem; // JButton Gobn;
JLabel label; ImageIcon icon; JLabel labelw; public MainFrame() throws HeadlessException {
super("图书管理系统 V1.0");
menubar = new JMenuBar();
setJMenuBar(menubar);
// Gobn = new JButton("进入");
// Gobn.setSize(20, 10);
// Gobn.addActionListener(this);
icon = new ImageIcon("src:\\libary.jpg");
label = new JLabel(icon);
// label=new JLabel("图书管理系统");
// label = new JLabel(new ImageIcon("src:\\Jeff:\\libary.jpg"));
labelw = new JLabel("作者: ");
// ****系统管理****
SystemMMenu = new JMenu("系统管理");
LoginMenuItem = new JMenuItem("登录");
ExitMenuItem = new JMenuItem("退出");
SystemMMenu.add(LoginMenuItem);
SystemMMenu.add(ExitMenuItem);
LoginMenuItem.addActionListener(this);
ExitMenuItem.addActionListener(this);
menubar.add(SystemMMenu);
// ****图书管理****
BookMMenu = new JMenu("图书管理");
BookAddMenuItem = new JMenuItem("图书录入");
BookMMenu.add(BookAddMenuItem);
BookAddMenuItem.addActionListener(this);
menubar.add(BookMMenu);
// ****借阅管理****
BorrowMMenu = new JMenu("借阅管理");
BorrowBookMenuItem = new JMenuItem("借书");
ReturnBookMenuItem = new JMenuItem("还书");
BorrowMMenu.add(BorrowBookMenuItem);
BorrowMMenu.add(ReturnBookMenuItem);
BorrowBookMenuItem.addActionListener(this);
ReturnBookMenuItem.addActionListener(this);
menubar.add(BorrowMMenu);
// ****读者管理****
ReaderMMenu = new JMenu("读者管理");
BorrowerMMenuItem = new JMenuItem("借阅人管理");
ReaderMMenu.add(BorrowerMMenuItem);
BorrowerMMenuItem.addActionListener(this);
menubar.add(ReaderMMenu);
// ********************
// ********************
this.setLayout(new BorderLayout()); panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(labelw);
this.add(panel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // setSize(400, 300);
setVisible(true); // ****设置菜单初始状态****
BookMMenu.setEnabled(false);
BorrowMMenu.setEnabled(false);
ReaderMMenu.setEnabled(false); } public void actionPerformed(ActionEvent e) {
if (e.getSource() == LoginMenuItem) {
Login login = new Login();
login.pack();
login.setVisible(true);
}
} public class Login extends JFrame implements ActionListener { JPanel Loginpanel1, Loginpanel2; JLabel UserLabel, PasswordLabel; JTextField UserTextField; JPasswordField PasswordTextField; JButton YesBtn, CancelBtn; public Login() {
super("登陆"); UserLabel = new JLabel("用户名", JLabel.CENTER);
PasswordLabel = new JLabel("密码", JLabel.CENTER);
UserTextField = new JTextField(10);
PasswordTextField = new JPasswordField(10);
YesBtn = new JButton("确定");
CancelBtn = new JButton("取消");
YesBtn.addActionListener(this);
CancelBtn.addActionListener(this);
Loginpanel1 = new JPanel();
Loginpanel1.setLayout(new GridLayout(2, 2));
Loginpanel2 = new JPanel();
setLayout(new BorderLayout());
Loginpanel1.add(UserLabel);
Loginpanel1.add(UserTextField);
Loginpanel1.add(PasswordLabel);
Loginpanel1.add(PasswordTextField);
add(Loginpanel1, BorderLayout.CENTER);
Loginpanel2.add(YesBtn);
Loginpanel2.add(CancelBtn);
add(Loginpanel2, BorderLayout.SOUTH);
setSize(300, 300);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == CancelBtn) { this.dispose();
} else {
char[] password = PasswordTextField.getPassword();
String passwordSTR = new String(password);
if (UserTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不可为空!");
return;
}
if (passwordSTR.equals("")) {
JOptionPane.showMessageDialog(null, "密码不可为空!");
return; }
if ((UserTextField.getText().trim().equals("admin"))
&& (passwordSTR.equals("admin"))) {
BookMMenu.setEnabled(true);
BorrowMMenu.setEnabled(true);
ReaderMMenu.setEnabled(true);
this.dispose(); } }
}
}
}