怪事情,怪事情,真怪..........高手进来看一下

nodreamer 2003-05-05 09:31:20
我在一个窗口文件中监听了一个JComboBox,想把在JComboBox中监听到的值付给变量find,然后在另一个文件中引用,结果出现了下面的奇怪情形
我先
String find = "aaaa"

然后是监听程序
void jButton4_actionPerformed(ActionEvent e) {
find = (String)this.jComboBox1.getSelectedItem()\\在jComboBox1中读取数据
MyTimetable_Search search = new MyTimetable_Search();
}
然后在另一个文件MyTimetable_Search中
MyFrame frame1 = new MyFrame();
search= frame1.find;
时居然发现find = aaaa
而不是在find = (String)this.jComboBox1.getSelectedItem()中读出来的值
而且如果我用
find = (String)this.jComboBox1.getSelectedItem();
MyTimetable_Search search = new MyTimetable_Search();
jTextArea1.setText(find);
则在显示的是在jTextArea1中显示的是
find = (String)this.jComboBox1.getSelectedItem();中读出来的值

百思不得其解,请指教
...全文
69 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
helpall 2003-05-05
  • 打赏
  • 举报
回复
MyFrame-->button4-->MyTimetable_Search -->MyFrame.find.
MyFrame frame1 = new MyFrame();
find = frame1.transport; // should be "" always.

solution:
void jButton4_actionPerformed(ActionEvent e) {
transport = (String)this.jComboBox1.getSelectedItem();
MyTimetable_Search search = new MyTimetable_Search();
jTextArea1.setText(search.out);
}
public MyTimetable_Search() {
MyFrame frame1 = new MyFrame();
find = frame1.transport;

===>
void jButton4_actionPerformed(ActionEvent e) {
transport = (String)this.jComboBox1.getSelectedItem();
MyTimetable_Search search = new MyTimetable_Search(transport );
jTextArea1.setText(search.out);
}
public MyTimetable_Search(String aasdf) {
find = aasdf;


nodreamer 2003-05-05
  • 打赏
  • 举报
回复
还是说不清楚,我给你全贴出来好了,这是MyFrame.java和MyTimetable_Search.java
另外还有两个文件MyTimetable.java(主函数文件)和MyFrame_AboutBox.java,因为和这个问题无关我就不贴出来了
MyFrame.java

package timetable;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class MyFrame extends JFrame {
private JPanel contentPane;
private JToolBar jToolBar = new JToolBar();
private JButton jButton3 = new JButton();
private ImageIcon image1;
private ImageIcon image2;
private ImageIcon image3;
private BorderLayout borderLayout1 = new BorderLayout();
public JTextArea jTextArea1 = new JTextArea();
private JPanel jPanel1 = new JPanel();
private JLabel jLabel1 = new JLabel();
private JComboBox jComboBox1 = new JComboBox();
private JLabel jLabel3 = new JLabel();
private JButton jButton4 = new JButton();
String day1 = "Monday";
String day2 = "Tuesday";
String day3 = "Wednesday";
String day4 = "Thursday";
String day5 = "Friday";
String transport = "";
private JLabel jLabel2 = new JLabel();
//Construct the frame
public MyFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
image1 = new ImageIcon(timetable.MyFrame.class.getResource("openFile.gif"));
image2 = new ImageIcon(timetable.MyFrame.class.getResource("closeFile.gif"));
image3 = new ImageIcon(timetable.MyFrame.class.getResource("help.gif"));
//setIconImage(Toolkit.getDefaultToolkit().createImage(MyFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("我的课程表");
jButton3.setIcon(image3);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
jButton3.setToolTipText("我是谁");

jLabel1.setText("请选择日期");
jComboBox1.addItem(day1);
jComboBox1.addItem(day2);
jComboBox1.addItem(day3);
jComboBox1.addItem(day4);
jComboBox1.addItem(day5);
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBox1_actionPerformed(e);
}
});
jLabel3.setToolTipText("");
jLabel3.setText(" ");
jButton4.setText("开始查询");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton4_actionPerformed(e);
}
});
jComboBox1.setActionMap(null);
jLabel2.setText(" 欢迎来到课程表查询系统 " +
" ");
contentPane.add(jTextArea1, BorderLayout.CENTER);
contentPane.add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(jLabel1, null);
jPanel1.add(jComboBox1, null);
jPanel1.add(jLabel3, null);
jPanel1.add(jButton4, null);
contentPane.add(jToolBar, BorderLayout.NORTH);
jToolBar.add(jLabel2, null);
jToolBar.add(jButton3);
setResizable(false);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jComboBox1_actionPerformed(ActionEvent e) {

}

void jButton3_actionPerformed(ActionEvent e) {
MyFrame_AboutBox dlg = new MyFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}

void jButton4_actionPerformed(ActionEvent e) {
transport = (String)this.jComboBox1.getSelectedItem();
MyTimetable_Search search = new MyTimetable_Search();
jTextArea1.setText(search.out);
}
}

MyTimetable_Search.java

package timetable;

import java.net.URL;
import java.sql.*;

public class MyTimetable_Search {
private String url;
String find ="";
String out = "";
private Connection connect;
private Statement statement;
private ResultSet result;

public MyTimetable_Search() {
MyFrame frame1 = new MyFrame();
find = frame1.transport;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:javatest";
connect = DriverManager.getConnection(url,"sa","777567");
statement = connect.createStatement();
String query = "SELECT "+find+" FROM timetable";
result = statement.executeQuery(query);
while(result.next())
{
out = out+result.getString(1);
}
result.close();
statement.close();
connect.close();
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}
}

为了叙述方便,我在第一篇改了一些变量的名字,这回贴的是完全的源代码,直接从UltraEdit上考下来的

helpall 2003-05-05
  • 打赏
  • 举报
回复
可能是你的大结构与我想像得不同. 你需要把下面的大结构写出来.

class MyFrame? {
String find = "aaaa"
void jButton4_actionPerformed(ActionEvent e) {
find = (String)this.jComboBox1.getSelectedItem()\\在jComboBox1中读取数据
MyTimetable_Search search = new MyTimetable_Search();
}
}

class MyTimetable_Search {

find = (String)this.jComboBox1.getSelectedItem();
MyTimetable_Search search = new MyTimetable_Search();
jTextArea1.setText(find);

MyFrame frame1 = new MyFrame();
search= frame1.find;

}

nodreamer 2003-05-05
  • 打赏
  • 举报
回复
为什么呢????

不按按钮我连这个class都没有实例化,怎么可能打出来呢?

告诉我一下解决办法好吗?

我整个程序都编完了,就差这个值的传递问题了
helpall 2003-05-05
  • 打赏
  • 举报
回复
MyFrame frame1 = new MyFrame();
search= frame1.find;
=====>
MyFrame frame1 = new MyFrame();
System.out.println("==========TTT");
search= frame1.find;

你按了按纽后,看看是否能打出TTT?我的看法是你不按按纽就能打出TTT.
nodreamer 2003-05-05
  • 打赏
  • 举报
回复
呵呵
是很乱
得认真看才能看懂呀

我总不能把所有代码都贴出来吧
我觉得有关的部分我都说清楚了
zhuzi7904 2003-05-05
  • 打赏
  • 举报
回复
有点儿不清楚 希望说清楚一点 谢谢
nodreamer 2003-05-05
  • 打赏
  • 举报
回复
to alexll(游文)
代码太多了 ,我简要地说一下结构吧
我的目的是在窗口中读入一个数据,然后传入数据库连接文件进行查询,在把查询结果返回窗口显示出来
程序结构是这样的
先监听JButton4,当按下JButton4时,
1先读入数据赋给一个本地的string即find = .......;
2然后实例化数据库连接文件即MyTimetable_Search search = new MyTimetable_Search();
3显示查询结果jTextArea1.setText(search.out);

在MyTimetable_Search文件中我是在一开始就
MyFrame frame1 = new MyFrame();
string search= frame1.find
但是search读出的却是find初始化时的值
大给情况就是这样了,这是为什么呀?

nodreamer 2003-05-05
  • 打赏
  • 举报
回复
to helpall()
可是我在第一次按jButton4的时候
是先给find赋值,在实例化MyTimetable_Search的呀
然后再在MyTimetable_Search中search= frame1.find的时候find应该已经有值了呀

按你说的我应该怎么办呀???
helpall 2003-05-05
  • 打赏
  • 举报
回复
MyFrame frame1 = new MyFrame();
search= frame1.find;
这两句有问题:
在生成了frame1后紧接着就调了search= frame1.find, 没有时间去按jButton4.
alexll 2003-05-05
  • 打赏
  • 举报
回复
贴程序结构出来

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧