package lucky;import javax.swing.*;import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Main extends JFrame { /*
* 判断当前是否有用户登陆,如果没有用户登陆的话,则菜单栏除了登陆一项其余都是灰选,即Visible置为false;
* 还有就是修改用户应该给ReUser();传一个当前用户名的值,即只能修改当前登陆的用户的信息
*
*/
private MenuBar menu = new MenuBar();
private String usestr;
Main(){}
Main(String userxx) { JFrame f = new JFrame(usexx);
f.setSize(600, 400);
f.setLocation(200, 100);
f.setResizable(false);
f.setLayout(new BorderLayout());
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void Closing(WindowEvent e) {
System.exit(0);
}
}); CrearMenu();
f.setMenuBar(menu); } public void CrearMenu() {
Menu filem = new Menu("用户管理"); Menu editm = new Menu("学生管理"); Menu toolm = new Menu("成绩管理"); Menu helpm = new Menu("帮助"); Menu exitm = new Menu("退出"); MenuItem filem1 = new MenuItem("登陆"); MenuItem filem2 = new MenuItem("注册用户"); MenuItem fielm3 = new MenuItem("修改用户"); MenuItem fielm4 = new MenuItem("切换用户"); MenuItem editm1 = new MenuItem("查询学生信息"); MenuItem editm2 = new MenuItem("修改学生信息"); MenuItem editm3 = new MenuItem("删除学生信息"); MenuItem editm4 = new MenuItem("添加学生信息"); MenuItem toolm1 = new MenuItem("查询成绩"); MenuItem toolm2 = new MenuItem("修改成绩"); MenuItem toolm3 = new MenuItem("删除成绩"); MenuItem toolm4 = new MenuItem("添加成绩"); MenuItem helpm1 = new MenuItem("关于程序"); MenuItem helpm2 = new MenuItem("关于作者"); MenuItem exitm1 = new MenuItem("退出");
menu.add(filem);
menu.add(editm);
menu.add(toolm);
menu.add(helpm);
menu.add(exitm);
MenuListener m1 = new MenuListener(); // 菜单栏的事件监听
filem.add(filem1);
filem.add(filem2);
filem.add(fielm3);
filem.add(fielm4); filem1.addActionListener(m1);
filem2.addActionListener(m1);
fielm3.addActionListener(m1); editm.add(editm1);
editm.add(editm2);
editm.add(editm3);
editm.add(editm4); editm1.addActionListener(m1);
editm2.addActionListener(m1);
editm3.addActionListener(m1);
editm4.addActionListener(m1); toolm.add(toolm1);
toolm.add(toolm2);
toolm.add(toolm3);
toolm.add(toolm4); toolm1.addActionListener(m1);
toolm2.addActionListener(m1);
toolm3.addActionListener(m1);
toolm4.addActionListener(m1); helpm.add(helpm1);
helpm.add(helpm2); helpm1.addActionListener(m1);
helpm2.addActionListener(m1);
exitm.add(exitm1);
exitm1.addActionListener(m1);
} public void OnScreen() {
Dimension meSize = getToolkit().getScreenSize();
Dimension windowSize = getSize();
int x, y;
x = (meSize.width - windowSize.width) / 2;
y = (meSize.height - windowSize.height) / 2;
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
setLocation(x, y); } class MenuListener implements ActionListener { public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("登陆")) {
JFrame f = new Login();
f.show();
dispose();
}
if (e.getActionCommand().equals("注册用户")) {
JFrame b = new Register();
b.show();
dispose();
}
if (e.getActionCommand().equals("修改用户")) {
System.out.println("runing now");
JFrame f = new ReUser();
f.show();
dispose();
}
if (e.getActionCommand().equals("切换用户")) {
System.exit(0);
}
if (e.getActionCommand().equals("查询学生信息")) {
new findStu();
}
if (e.getActionCommand().equals("修改学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("添加学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("关于程序")) {
JFrame f = new Help();
f.show(); }
if (e.getActionCommand().equals("关于作者")) {
JFrame f = new useHelp();
f.show();
}
if (e.getActionCommand().equals("退出")) {
System.exit(1);
}
else{
return;
}
} } public static void main(String args[]) {
JFrame f = new Main();
f.show();
}}
为什么在运行的时候会产生两个窗体呢?
一个什么也没有,一个是我想生成的Main 这个窗体,实在找不到那还产生了一个事例,谢谢啦...
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Main extends JFrame { /*
* 判断当前是否有用户登陆,如果没有用户登陆的话,则菜单栏除了登陆一项其余都是灰选,即Visible置为false;
* 还有就是修改用户应该给ReUser();传一个当前用户名的值,即只能修改当前登陆的用户的信息
*
*/
private MenuBar menu = new MenuBar();
private String usestr;
Main(){}
Main(String userxx) { JFrame f = new JFrame(usexx);
f.setSize(600, 400);
f.setLocation(200, 100);
f.setResizable(false);
f.setLayout(new BorderLayout());
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void Closing(WindowEvent e) {
System.exit(0);
}
}); CrearMenu();
f.setMenuBar(menu); } public void CrearMenu() {
Menu filem = new Menu("用户管理"); Menu editm = new Menu("学生管理"); Menu toolm = new Menu("成绩管理"); Menu helpm = new Menu("帮助"); Menu exitm = new Menu("退出"); MenuItem filem1 = new MenuItem("登陆"); MenuItem filem2 = new MenuItem("注册用户"); MenuItem fielm3 = new MenuItem("修改用户"); MenuItem fielm4 = new MenuItem("切换用户"); MenuItem editm1 = new MenuItem("查询学生信息"); MenuItem editm2 = new MenuItem("修改学生信息"); MenuItem editm3 = new MenuItem("删除学生信息"); MenuItem editm4 = new MenuItem("添加学生信息"); MenuItem toolm1 = new MenuItem("查询成绩"); MenuItem toolm2 = new MenuItem("修改成绩"); MenuItem toolm3 = new MenuItem("删除成绩"); MenuItem toolm4 = new MenuItem("添加成绩"); MenuItem helpm1 = new MenuItem("关于程序"); MenuItem helpm2 = new MenuItem("关于作者"); MenuItem exitm1 = new MenuItem("退出");
menu.add(filem);
menu.add(editm);
menu.add(toolm);
menu.add(helpm);
menu.add(exitm);
MenuListener m1 = new MenuListener(); // 菜单栏的事件监听
filem.add(filem1);
filem.add(filem2);
filem.add(fielm3);
filem.add(fielm4); filem1.addActionListener(m1);
filem2.addActionListener(m1);
fielm3.addActionListener(m1); editm.add(editm1);
editm.add(editm2);
editm.add(editm3);
editm.add(editm4); editm1.addActionListener(m1);
editm2.addActionListener(m1);
editm3.addActionListener(m1);
editm4.addActionListener(m1); toolm.add(toolm1);
toolm.add(toolm2);
toolm.add(toolm3);
toolm.add(toolm4); toolm1.addActionListener(m1);
toolm2.addActionListener(m1);
toolm3.addActionListener(m1);
toolm4.addActionListener(m1); helpm.add(helpm1);
helpm.add(helpm2); helpm1.addActionListener(m1);
helpm2.addActionListener(m1);
exitm.add(exitm1);
exitm1.addActionListener(m1);
} public void OnScreen() {
Dimension meSize = getToolkit().getScreenSize();
Dimension windowSize = getSize();
int x, y;
x = (meSize.width - windowSize.width) / 2;
y = (meSize.height - windowSize.height) / 2;
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
setLocation(x, y); } class MenuListener implements ActionListener { public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("登陆")) {
JFrame f = new Login();
f.show();
dispose();
}
if (e.getActionCommand().equals("注册用户")) {
JFrame b = new Register();
b.show();
dispose();
}
if (e.getActionCommand().equals("修改用户")) {
System.out.println("runing now");
JFrame f = new ReUser();
f.show();
dispose();
}
if (e.getActionCommand().equals("切换用户")) {
System.exit(0);
}
if (e.getActionCommand().equals("查询学生信息")) {
new findStu();
}
if (e.getActionCommand().equals("修改学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("添加学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("删除学生信息")) { }
if (e.getActionCommand().equals("关于程序")) {
JFrame f = new Help();
f.show(); }
if (e.getActionCommand().equals("关于作者")) {
JFrame f = new useHelp();
f.show();
}
if (e.getActionCommand().equals("退出")) {
System.exit(1);
}
else{
return;
}
} } public static void main(String args[]) {
JFrame f = new Main();
f.show();
}}
为什么在运行的时候会产生两个窗体呢?
一个什么也没有,一个是我想生成的Main 这个窗体,实在找不到那还产生了一个事例,谢谢啦...
解决方案 »
- jedit入门问题请教
- java 程序打包后出现:javax.comm.NoSuchPortException
- 写一个函数,2个参数,1个字符串,1个字节数,返回截取的字符串,要求字符串中的中文不能出现乱码,如("我 ABC",4) 应该截取为"我 AB",输入("我 ABC 汉 DEF",6)应该输出"我 ABC"而不是"我 ABC+汉的半个"
- 一个关于poi操作excel文件的问题!
- 用java写文件为什么大小最多只能有64KB,要怎么改;
- Swing窗体内是否能嵌套显示HTML页面
- 有SWING高手吗? 帮我改一下这个JTree 能遍历整个系统的 拜托了
- 博客中国迷失于又一个信息门户旋涡之中,将如何面对个人门户的兴起?
- 如何让两个java进程间通信?急!
- 新手--散分求助--UP有分!!!--JB 7+Weblogic 7 入门小问题--分不够再加!!!
- Java 的电子书籍
- 关于一个boolean型的初始化
public static void main(String args[]) 也是自动执行的,你在main中创建实例f,等于又重新构造了,这样Main执行了两次