谢霆锋和景甜现场视频:java图形界面按钮问题,急

来源:百度文库 编辑:高考问答 时间:2024/05/06 04:41:55
请问在java中怎样产生一个界面,其中有一个按钮,按下去之后让它产生我编写的其他类的事件?我很着急,谢谢啦!

偶写了个代码,用俩个界面的跳转来实现,你看一下吧
import javax.swing.*;
import java.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class Face extends JFrame implements ActionListener
{

Container contentPane=this.getContentPane();
Course course; //Course类对象
JButton b1;
JButton b2;
JPanel p3;

Face()
{
super("Job Required");
contentPane.setLayout(new GridLayout(3,0));
b2=new JButton("Canel");
b2.addActionListener(this);
b1=new JButton("OK");
b1.addActionListener(this); //添加事件监听器
p3=new JPanel(new FlowLayout());
p3.add(b1);
p3.add(b2);
contentPane.add(p3);

pack();
this.setSize(200,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) //按钮事件
{
if(e.getSource()==b1) //点击OK时跳转
{
course=new Course();
this.setVisible(false);
course.setVisible(true);
}
else
{
System.exit(0);
}
}
}

class Course extends JFrame
{
Container contentPane=this.getContentPane();
static TextArea ta1;
JPanel p4;

Course()
{
super("Course Simulation");

ta1=new TextArea();
p4=new JPanel();
ta1.setText("跳转后的类");
p4.add(ta1);
contentPane.add(p4);
this.setSize(500,200);
this.setVisible(true);

}
}

public class Student
{
public static void main(String args[])
{
Face f=new Face();
}
}
已经编译过了,好使