求高手点拨一个“JFrame窗口的打开”问题...
高手,您好:我在做一个“让我的程序”“在用户点击了一个JButton按钮之后”,能够打开对应的“新的JFrame窗口”的功能...
我的“用户将要点击的JFrame窗口”的源码如下:
package c_port_packageBJTeacher;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import c_port_packageYCTeacher.Advertisement;
import c_port_packageYCTeacher.MyJButton5;
import c_port_packageYCTeacher.Psychological_gas_station;
import c_port_packageYCTeacher.SQA_enterprise;
public class C extends JFrame implements WindowListener ,ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel jp = new JPanel();
JLabel JL0 = new JLabel("英语补课教室");
JLabel JL1 = new JLabel("本平台学生求学保证组织");
JLabel JL2 = new JLabel("心灵加油站专家");
JLabel JL3 = new JLabel("中文补课教室");
JLabel JL4 = new JLabel("数学补课教室");
JLabel JL5 = new JLabel("用户所在物理地址推荐资源");
MyJButton5 JB200 = new MyJButton5("进入英语补课教室",200);
MyJButton5 JB201 = new MyJButton5("<html>进入企业教师担保记录室</html>",201);
MyJButton5 JB202 = new MyJButton5("<html>进入心灵加油站专家组获得心灵力量</html>",202);
MyJButton5 JB203 = new MyJButton5("<html>进入中文补课室</html>",203);
MyJButton5 JB204 = new MyJButton5("<html>进入数学补课室</html>",204);
MyJButton5 JB209 = new MyJButton5("<html>进入本地资源推荐展板</html>",209);
Font fnt = new Font("华文中宋",0,18);
static ArrayList<C> al0 = new ArrayList<C>();
public static ArrayList<English_Teacher> al1 = new ArrayList<English_Teacher>();
public static ArrayList<SQA_enterprise> al2 = new ArrayList<SQA_enterprise>();
public static ArrayList<Psychological_gas_station> al3 = new ArrayList<Psychological_gas_station>();
public static ArrayList<Chinese_Teacher> al4 = new ArrayList<Chinese_Teacher>();
public static ArrayList<Math_Teacher> al5 = new ArrayList<Math_Teacher>();
public static ArrayList<Advertisement> al6 = new ArrayList<Advertisement>();
public void start(){
this.setTitle("请选择您需要的科室");
jp.setLayout(new GridLayout(2,6));
JB200.addActionListener(this);
JB201.addActionListener(this);
JB202.addActionListener(this);
JB203.addActionListener(this);
JB204.addActionListener(this);
JB209.addActionListener(this);
jp.add(JL0);
al0.add(this);
JB200.setFont(fnt);
JB201.setFont(fnt);
JB202.setFont(fnt);
JB203.setFont(fnt);
JB204.setFont(fnt);
JB209.setFont(fnt);
JL0.setFont(fnt);
JL1.setFont(fnt);
JL2.setFont(fnt);
JL3.setFont(fnt);
JL4.setFont(fnt);
JL5.setFont(fnt);
jp.add(JL1);
jp.add(JL2);
jp.add(JL3);
jp.add(JL4);
jp.add(JL5);
jp.add(JB200);
jp.add(JB201);
jp.add(JB202);
jp.add(JB203);
jp.add(JB204);
jp.add(JB209);
this.add(jp);
this.setBounds(0,0,1366,768);
this.setVisible(true);
}
public static void main(String[] args){
new C().start();
}
/**
* @param args
*/
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MyJButton5 btn = (MyJButton5) e.getSource();
switch( btn.getMyId()){//【重要修改点】:怎样修改可以排错?
case 200:
{
English_Teacher obj1 = new English_Teacher();
al1.add(obj1);
break;
}
case 201:
SQA_enterprise obj2 = new SQA_enterprise();
al2.add(obj2);
break;
case 202:
Psychological_gas_station obj3 = new Psychological_gas_station();
al3.add(obj3);
break;
case 203:
Chinese_Teacher obj4 = new Chinese_Teacher();
al4.add(obj4);
break;
case 204:
Math_Teacher obj5 = new Math_Teacher();
al5.add(obj5);
break;
case 209:
Advertisement obj6 = new Advertisement();
al6.add(obj6);
break;
}
}
}
上文中的代码:“Advertisement”类,“Math_Teacher”类,“Chinese_Teacher”类,“Psychological_gas_station”类,“SQA_enterprise”类,和“English_Teacher”类,都是我写的“下一级窗口”,如果用户点击了本源代码中的jb0,jb1,jb2,jb3,jb4,之后,就会“打开我的下一级窗口”..
其中的“MyJButton5”按钮的源代码如下:
package c_port_packageBJTeacher;
import javax.swing.JButton;
public class MyJButton5 extends JButton{
/**
*
*/
private static final long serialVersionUID = 1L;
int MyId = 0;
public MyJButton5(String Button,int Id){
super(Button);
MyId = Id;
}
public int getMyId() {
// TODO Auto-generated method stub
return MyId;
}
}
如果我将上文中的C类,进行了自第145行代码的修改之后:
package c_port_packageBJTeacher;Swing JFrame java java调试 Java排错 --------------------编程问答-------------------- 在进行了上面的代码的修改之后,我 就得到了一个“无法打开下一个子窗口”的本C类文件所创建的窗口...
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import c_port_packageYCTeacher.Advertisement;
import c_port_packageYCTeacher.MyJButton5;
import c_port_packageYCTeacher.Psychological_gas_station;
import c_port_packageYCTeacher.SQA_enterprise;
public class C extends JFrame implements WindowListener ,ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel jp = new JPanel();
JLabel JL0 = new JLabel("英语补课教室");
JLabel JL1 = new JLabel("本平台学生求学保证组织");
JLabel JL2 = new JLabel("心理加油站专家");
JLabel JL3 = new JLabel("中文补课教室");
JLabel JL4 = new JLabel("数学补课教室");
JLabel JL5 = new JLabel("用户所在物理地址推荐资源");
MyJButton5 JB200 = new MyJButton5("进入英语补课教室",200);
MyJButton5 JB201 = new MyJButton5("<html>进入企业教师担保记录室</html>",201);
MyJButton5 JB202 = new MyJButton5("<html>进入心灵加油站专家组获得心灵力量</html>",202);
MyJButton5 JB203 = new MyJButton5("<html>进入中文补课室</html>",203);
MyJButton5 JB204 = new MyJButton5("<html>进入数学补课室</html>",204);
MyJButton5 JB209 = new MyJButton5("<html>进入本地资源推荐展板</html>",209);
Font fnt = new Font("华文中宋",0,18);
static ArrayList<C> al0 = new ArrayList<C>();
public static ArrayList<English_Teacher> al1 = new ArrayList<English_Teacher>();
public static ArrayList<SQA_enterprise> al2 = new ArrayList<SQA_enterprise>();
public static ArrayList<Psychological_gas_station> al3 = new ArrayList<Psychological_gas_station>();
public static ArrayList<Chinese_Teacher> al4 = new ArrayList<Chinese_Teacher>();
public static ArrayList<Math_Teacher> al5 = new ArrayList<Math_Teacher>();
public static ArrayList<Advertisement> al6 = new ArrayList<Advertisement>();
public void start(){
this.setTitle("请选择您需要的科室");
jp.setLayout(new GridLayout(2,6));
JB200.addActionListener(this);
JB201.addActionListener(this);
JB202.addActionListener(this);
JB203.addActionListener(this);
JB204.addActionListener(this);
JB209.addActionListener(this);
jp.add(JL0);
al0.add(this);
JB200.setFont(fnt);
JB201.setFont(fnt);
JB202.setFont(fnt);
JB203.setFont(fnt);
JB204.setFont(fnt);
JB209.setFont(fnt);
JL0.setFont(fnt);
JL1.setFont(fnt);
JL2.setFont(fnt);
JL3.setFont(fnt);
JL4.setFont(fnt);
JL5.setFont(fnt);
jp.add(JL1);
jp.add(JL2);
jp.add(JL3);
jp.add(JL4);
jp.add(JL5);
jp.add(JB200);
jp.add(JB201);
jp.add(JB202);
jp.add(JB203);
jp.add(JB204);
jp.add(JB209);
this.add(jp);
this.setBounds(0,0,1366,768);
this.setVisible(true);
}
public static void main(String[] args){
new C().start();
}
/**
* @param args
*/
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource.equals("JB200")){
English_Teacher obj1 = new English_Teacher();
al1.add(obj1);
}
if(e.getSource.equals("JB201")){
SQA_enterprise obj2 = new SQA_enterprise();
al2.add(obj2);
}
if(e.getSource().equals("JB202")){
Psychological_gas_station obj3 = new Psychological_gas_station();
al3.add(obj3);
}
if(e.getSource().equals("JB203")){
Chinese_Teacher obj4 = new Chinese_Teacher();
al4.add(obj4);
}
if(e.getSource().equals("JB204")){
Math_Teacher obj5 = new Math_Teacher();
al5.add(obj5);
}
if(e.getSource().equals("JB209")){
Advertisement obj6 = new Advertisement();
al6.add(obj6);
}
}
}
}
请问高手:
这是怎么回事...?
在线静候高手的相助!!
一百分奉上!!
谢谢高手的点拨!!
一位日日夜夜向着理想奔跑的筑梦者
2013年10月15日早晨9点11分 --------------------编程问答-------------------- 自顶一个!!
Mark!! --------------------编程问答-------------------- 将上面的代码中的第147,第151,第155,第159,第163,第167行代码进行了“用==替换equals”的操作之后,仍然不能进行“下一级窗口的打开”的开发效果的达到...
希望得到高手的指导:
为什么第二种写法,会得到这个结果...? --------------------编程问答-------------------- 首先,楼主的精神值得肯定,但是发帖的格式还是差了点,看了好一会没看懂问什么,知道最后把代码拷贝调试后才知道楼主要问什么。
今天看到一个发帖思路很清晰的,让人一看就明白,楼主可以借鉴一下,此人一看就是老程序员。
http://bbs.csdn.net/topics/390615022
我不知道这代码是不是你写的,只是感觉应该不像,
Math_Teacher obj5 = new Math_Teacher();
al5.add(obj5);
这个本身是没错的,但是错没错得看你Math_Teacher怎么写的。
Math_Teacher如果继承JFrame的话就会弹出,如果继承JPanel的话就会被母控件所遮挡。
这个跟你用不用equals没什么关系
不知道你为什么要加入到List中,这有什么特殊用处么?
我猜你应该还不会debug调试,有空可以学一下,
这里的话先可以加入System.out.println();
看效果。
比如你的第一段代码中:
--------------------编程问答-------------------- 我在等别问题答案时候给写了个,代码如下:
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MyJButton5 btn = (MyJButton5) e.getSource();
System.out.println(btn.getMyId());//看看得到的结果是多少
switch( btn.getMyId()){//【重要修改点】:怎样修改可以排错?
case 200:
{
English_Teacher obj1 = new English_Teacher();
al1.add(obj1);
System.out.println("执行了"+obj1.toString());//下面类同。
break;
}
case 201:
SQA_enterprise obj2 = new SQA_enterprise();
al2.add(obj2);
break;
case 202:
Psychological_gas_station obj3 = new Psychological_gas_station();
al3.add(obj3);
break;
case 203:
Chinese_Teacher obj4 = new Chinese_Teacher();
al4.add(obj4);
break;
case 204:
Math_Teacher obj5 = new Math_Teacher();
al5.add(obj5);
break;
case 209:
Advertisement obj6 = new Advertisement();
al6.add(obj6);
break;
}
}
Test1表示你说的第一个窗口
Test2表示打开哪个新窗口,每次是把按钮的名字传过去了,你要是读对应科目的成绩之类就把对应传进去吧
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 extends JFrame
{
public Test1()
{
init();
}
private void init()
{
this.setTitle("看图程序");
this.setPreferredSize(new Dimension(800, 600));
JButton jButton1 = new JButton("按钮1");
jButton1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new Test2(e.getActionCommand());
}
});
JButton jButton2 = new JButton("按钮2");
jButton2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new Test2(e.getActionCommand());
}
});
this.getContentPane().add(jButton1, BorderLayout.NORTH);
this.getContentPane().add(jButton2, BorderLayout.SOUTH);
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new Test1();
}
}
class Test2 extends JFrame
{
private String str;
public Test2(String string)
{
str = string;
init();
}
private void init()
{
System.out.println(str);
this.setPreferredSize(new Dimension(80, 60));
this.getContentPane().add(new Label(str), BorderLayout.CENTER);
this.setVisible(true);
this.pack();
}
}
这个不周全,
如果考虑周全的话,建议开线程,按钮点击后开线程,在线程里面处理复杂的情形,防止主线程被延误。
补充:Java , Java SE