比如你的Frame对象为f其中有一个JTextField output= new JTextField;
另一个文件为DBClass db.
你可以有两种选择办法
1>在Frame中生成DBClass对象
如在Frame的某个方法中调用
DBClass db=new DBClass(this);
或者
DBClass db=new DBlass();
之后在DBClass中就可以利用传递过来的对象访问output了。
db.setHandle(this);
当然这个setHandle你要自己定义。
2>在DBClass生成Frame
在DBClass的某个方法中调用
Frame f=new yourFrame()
之后调用f.output付值
另一个文件为DBClass db.
你可以有两种选择办法
1>在Frame中生成DBClass对象
如在Frame的某个方法中调用
DBClass db=new DBClass(this);
或者
DBClass db=new DBlass();
之后在DBClass中就可以利用传递过来的对象访问output了。
db.setHandle(this);
当然这个setHandle你要自己定义。
2>在DBClass生成Frame
在DBClass的某个方法中调用
Frame f=new yourFrame()
之后调用f.output付值
另一个
public class view extends JFrame {}
的.java文件里的
JTextField output= new JTextField;里输出
应该怎么办???????????
view v=new view()
之后调用v.output啊。
view frame = new view();
然后
frame.output.setText("111");可是在运行的时候根本就没有显示111,
output中还是什么都没有
在frame.output.setText("111");之后
调用frame.repaint();是市。
可还是没用
public static void main(String args[])
{
view v=new view();
v.output.setText("asdfasdfasdf");
}
之后编译运行看有没有。
public class view extends JFrame {
JPanel contentPane,top,mid,bottom;
JTextField input,output;
JLabel labelup,labelmid,labeldown;
JButton button;
BorderLayout borderLayout1 = new BorderLayout(); //Construct the frame
public view() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//窗口初始化
public void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(view.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1); top = new JPanel(new FlowLayout());
contentPane.add(top,BorderLayout.NORTH );
labelup = new JLabel("软件技术基础考试成绩查询");
top.add(labelup); mid = new JPanel(new BorderLayout());
contentPane.add(mid,BorderLayout.SOUTH ); labelmid = new JLabel("请输入准考证号");
mid.add(labelmid,BorderLayout.WEST ); input = new JTextField(9);
mid.add(input,BorderLayout.CENTER ); button = new JButton("查询");
mid.add(button,BorderLayout.EAST );
bottom = new JPanel();
mid.add(bottom,BorderLayout.SOUTH ); labeldown = new JLabel("成绩");
bottom.add(labeldown,BorderLayout.WEST );
bottom.add(output,BorderLayout.EAST ); this.setSize(new Dimension(400, 120));
this.setTitle("软件技术基础考试成绩查询系统");
}//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);
}
}
}
JPanel contentPane,top,mid,bottom;
JTextField input,output;
JLabel labelup,labelmid,labeldown;
JButton button;
BorderLayout borderLayout1 = new BorderLayout(); //Construct the frame
public view() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//窗口初始化
public void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1); top = new JPanel(new FlowLayout());
contentPane.add(top,BorderLayout.NORTH );
labelup = new JLabel("软件技术基础考试成绩查询");
top.add(labelup); mid = new JPanel(new BorderLayout());
contentPane.add(mid,BorderLayout.SOUTH ); labelmid = new JLabel("请输入准考证号");
mid.add(labelmid,BorderLayout.WEST ); input = new JTextField(9);
mid.add(input,BorderLayout.CENTER ); button = new JButton("查询");
mid.add(button,BorderLayout.EAST );
bottom = new JPanel();
mid.add(bottom,BorderLayout.SOUTH ); labeldown = new JLabel("成绩");
bottom.add(labeldown,BorderLayout.WEST );
bottom.add(output,BorderLayout.EAST ); this.setSize(new Dimension(400, 120));
this.setTitle("软件技术基础考试成绩查询系统");
}//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);
}
}
}
output怎么没有
output = new JTextField(9);
output = new JTextField(30);
整个窗体都没问题
我在view()里定义output.setText("111");就能显示
只是在find中定义
view frame = new view();
frame.output.setText("111");
没法显示
public settext()
{
output.setText("asdf");
}
之后在
find中调用
view frame = new view();
frame.settext();是是
public void settext()
{
output.setText("asdf");
}
才不报错,不加void,就显示有错误.加了void之后能运行了,但还是显示不出来
3333333333333333333QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
后面调用frame.validate()是是,我对窗口编程不是很了解,惭愧。
view类和你的一样,在另一个类中调用:
view v = new view();
v.output.setText("haha");
v.setVisible(true);是不是你没有用 v.setVisible(true);
在view写了main,然后new 了2个view来让第二个
设置对方,都是可以的哦
package test;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class view extends JFrame {
JPanel contentPane,top,mid,bottom;
view other;
JTextField input,output;
JLabel labelup,labelmid,labeldown;
JButton button=new JButton("");
BorderLayout borderLayout1 = new BorderLayout();
private JButton jButton1 = new JButton(); //Construct the frame
public view(view other1) {
other = other1;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public view() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//窗口初始化
public void jbInit() throws Exception {
output = new JTextField(30);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1); top = new JPanel(new FlowLayout());
jButton1.setText(" 点我吧,可以设置另一个窗口");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.add(top,BorderLayout.NORTH );
labelup = new JLabel("软件技术基础考试成绩查询");
top.add(jButton1, null);
top.add(labelup); mid = new JPanel(new BorderLayout());
contentPane.add(mid,BorderLayout.SOUTH ); labelmid = new JLabel("请输入准考证号");
mid.add(labelmid,BorderLayout.WEST ); input = new JTextField(9);
mid.add(input,BorderLayout.CENTER ); button = new JButton("查询");
mid.add(button,BorderLayout.EAST );
bottom = new JPanel();
mid.add(bottom,BorderLayout.SOUTH ); labeldown = new JLabel("成绩");
bottom.add(labeldown,BorderLayout.WEST );
bottom.add(output,BorderLayout.EAST ); this.setSize(new Dimension(400, 120));
this.setTitle("软件技术基础考试成绩查询系统");
}//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);
}
} public void setother(view mother)
{
mother.output.setText("别人设置我了哦");
System.out.println("click");
} public static void main(String[] args) {
view v=new view();
v.output.setText("我是窗口1,不要点我啊");
v.pack();
v.setVisible(true);
view v1=new view(v);
v1.output.setText("我是窗口2,请先把我移到一边,再点我啊!");
v1.pack();
v1.setVisible(true);
} void jButton1_actionPerformed(ActionEvent e) {
this.setother(other);
}
}