为什么在运行主程序时,还没有选择菜单项menuitem[4],
可它的监听页kaikemulu_page()却跟着一起显示了,高手解答一下,谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class manager_page extends JFrame
{
public static final int itemcount = 9;
JMenuBar menubar;
JMenu []jmenu = new JMenu[4];
JMenuItem [] menuitem = new JMenuItem[itemcount];
public manager_page()
{
super("欢迎进入主操作界面");
Container con = getContentPane();
// JPanel panel = new JPanel();
con.setBackground(Color.pink);
menubar = new JMenuBar();
menubar.setBackground(Color.pink);
jmenu[0] = new JMenu("系统");
jmenu[0].setBackground(Color.pink);
jmenu[1] = new JMenu("选退课");
jmenu[1].setBackground(Color.pink);
jmenu[2] = new JMenu("查询");
jmenu[2].setBackground(Color.pink);
jmenu[3] = new JMenu("帮助");
jmenu[3].setBackground(Color.pink);
menuitem[0] = new JMenuItem("简介");
menuitem[1] = new JMenuItem("退出");
menuitem[2] = new JMenuItem("选课");
menuitem[3] = new JMenuItem("退课");
menuitem[4] = new JMenuItem("开课目录");
menuitem[4].addActionListener(new kaikemulu_page());
menuitem[5] = new JMenuItem("已选课程");
menuitem[6] = new JMenuItem("教师查询");
menuitem[7]= new JMenuItem("点名册查询");
menuitem[8] = new JMenuItem("帮助");
for(int i = 0; i < itemcount; i ++)
if(i <2)
jmenu[0].add(menuitem[i]);
else if (i <4)
jmenu[1].add(menuitem[i]);
else if (i < 8)
jmenu[2].add(menuitem[i]);
else
jmenu[3].add(menuitem[i]);
for(int k = 0; k < 4 ; k++)
menubar.add(jmenu[k]);
this.setJMenuBar(menubar);
JPanel panel =new hua();
panel.setBackground(Color.pink);
con.add(panel);
setSize(300,200);
setVisible(true);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class hua extends JPanel
{
public void paint(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
g.drawString("************************************",55,35);
g.drawString("网上选课系统",80,60);
g.drawString("version1.0",105,85);
g.drawString("************************************",55,109);
//super.paintComponents(g);
}
}
public static void main(String[] args)
{
new manager_page();
}
}
下面是显示课程的页面,怎么还没点击开课目录菜单,就显示这一页了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class kaikemulu_page extends JFrame implements ActionListener
{
Object [][]object;
public kaikemulu_page()
{
super("开课目录显示");
Container con = getContentPane();
con.setBackground(Color.pink);
con.setLayout(new BorderLayout());
String [] lieming = {"名称","课程号","上课时间","任课教师"};
object = new Object[5][4];
JTable table = new JTable(object,lieming);
JScrollPane scrollpane = new JScrollPane(table);
table.setBackground(Color.pink);
scrollpane.setBackground(Color.pink);
con.add(scrollpane,BorderLayout.CENTER);
initobject();
setSize(360,180);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initobject()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("成功加载JDBC_ODBC驱动程序!");
}
catch (ClassNotFoundException ex)
{
System.out.println("加载JDBC_ODBC驱动程序失败!");
System.out.println(ex.getMessage());
return;
}
try
{
Connection connection = DriverManager.getConnection("jdbc:odbc:myDatabase");
Statement st = connection.createStatement();
String query = "select cname,course.cnum,ctime,tname from course,teacher where course.cnum=teacher.cnum";
ResultSet rs = st.executeQuery(query);
int count = 0;
while (rs.next())
{
object[count][0] = rs.getString(1);
object[count][1] = rs.getString(2);
object[count][2] = rs.getString(3);
object[count][3] = rs.getString(4);
count ++;
}
}
catch (SQLException ec)
{
System.out.println("查询数据库失败!");
System.out.println("SQLException:" + ec.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{}
public static void main(String[] args)
{
new kaikemulu_page();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class manager_page extends JFrame
{
public static final int itemcount = 9;
JMenuBar menubar;
JMenu []jmenu = new JMenu[4];
JMenuItem [] menuitem = new JMenuItem[itemcount];
public manager_page()
{
super("欢迎进入主操作界面");
Container con = getContentPane();
// JPanel panel = new JPanel();
con.setBackground(Color.pink);
menubar = new JMenuBar();
menubar.setBackground(Color.pink);
jmenu[0] = new JMenu("系统");
jmenu[0].setBackground(Color.pink);
jmenu[1] = new JMenu("选退课");
jmenu[1].setBackground(Color.pink);
jmenu[2] = new JMenu("查询");
jmenu[2].setBackground(Color.pink);
jmenu[3] = new JMenu("帮助");
jmenu[3].setBackground(Color.pink);
menuitem[0] = new JMenuItem("简介");
menuitem[1] = new JMenuItem("退出");
menuitem[2] = new JMenuItem("选课");
menuitem[3] = new JMenuItem("退课");
menuitem[4] = new JMenuItem("开课目录");
menuitem[4].addActionListener(new kaikemulu_page());//你这里一调用,当然就出现了,你看看你这个类,你的初始化都放在构造函数里面
menuitem[5] = new JMenuItem("已选课程");
menuitem[6] = new JMenuItem("教师查询");
menuitem[7]= new JMenuItem("点名册查询");
menuitem[8] = new JMenuItem("帮助");
for(int i = 0; i < itemcount; i ++)
if(i <2)
jmenu[0].add(menuitem[i]);
else if (i <4)
jmenu[1].add(menuitem[i]);
else if (i < 8)
jmenu[2].add(menuitem[i]);
else
jmenu[3].add(menuitem[i]);
for(int k = 0; k < 4 ; k++)
menubar.add(jmenu[k]);
this.setJMenuBar(menubar);
JPanel panel =new hua();
panel.setBackground(Color.pink);
con.add(panel);
setSize(300,200);
setVisible(true);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class hua extends JPanel
{
public void paint(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.blue);
g.drawString("************************************",55,35);
g.drawString("网上选课系统",80,60);
g.drawString("version1.0",105,85);
g.drawString("************************************",55,109);
//super.paintComponents(g);
}
}
public static void main(String[] args)
{
new manager_page();
}
}
下面是显示课程的页面,怎么还没点击开课目录菜单,就显示这一页了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class kaikemulu_page extends JFrame implements ActionListener
{
Object [][]object;
public kaikemulu_page()
{
super("开课目录显示");
Container con = getContentPane();
con.setBackground(Color.pink);
con.setLayout(new BorderLayout());
String [] lieming = {"名称","课程号","上课时间","任课教师"};
object = new Object[5][4];
JTable table = new JTable(object,lieming);
JScrollPane scrollpane = new JScrollPane(table);
table.setBackground(Color.pink);
scrollpane.setBackground(Color.pink);
con.add(scrollpane,BorderLayout.CENTER);
initobject();
setSize(360,180);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initobject()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("成功加载JDBC_ODBC驱动程序!");
}
catch (ClassNotFoundException ex)
{
System.out.println("加载JDBC_ODBC驱动程序失败!");
System.out.println(ex.getMessage());
return;
}
try
{
Connection connection = DriverManager.getConnection("jdbc:odbc:myDatabase");
Statement st = connection.createStatement();
String query = "select cname,course.cnum,ctime,tname from course,teacher where course.cnum=teacher.cnum";
ResultSet rs = st.executeQuery(query);
int count = 0;
while (rs.next())
{
object[count][0] = rs.getString(1);
object[count][1] = rs.getString(2);
object[count][2] = rs.getString(3);
object[count][3] = rs.getString(4);
count ++;
}
}
catch (SQLException ec)
{
System.out.println("查询数据库失败!");
System.out.println("SQLException:" + ec.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{}
public static void main(String[] args)
{
new kaikemulu_page();
}
}