我的题目是:"有一个窗口,该窗口有两个依赖于它的对话框,一个对话框负责求圆的面积,另一个负责求三角形的面积"
package A;import java.awt.*;
import java.applet.*;import java.awt.Frame;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Frame1 extends Frame {
private static Frame win;
private Button button1;
private Button button2;
Dialog D;
Dialog D1;
public double area()
{
int r=5;
return r*r*3.14;
}
public double area1()
{
int r=5;
return r*r;
}
public Frame1() {
win=new Frame("My window");//设置窗体
win.setBackground(Color.white ) ;
win.resize(200,300) ;
win.setVisible(true); button1 = new Button("计算圆形面积");
button2 = new Button("计算四方形面积");
add(button1);
add(button2);
button1.getMouseListeners();
button2.getMouseListeners();//设置弹出对话框}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
D = new Dialog(win,"圆形",true);//弹出Dialog
D.setSize(80,80);
D.add(new Label("面积= "+String.valueOf(area()))) ;
D.show();
}
else {
if(e.getSource() == button2){
D1=new Dialog(win,"四方形",true);//弹出另一个Dialog
D1.setSize(70,80);
D1.add(new Label("面积= "+String.valueOf(area1()))) ;
D1.show();
}
}
}
public static void main(String[] args){
Frame1 f=new Frame1();
}
}
package A;import java.awt.*;
import java.applet.*;import java.awt.Frame;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Frame1 extends Frame {
private static Frame win;
private Button button1;
private Button button2;
Dialog D;
Dialog D1;
public double area()
{
int r=5;
return r*r*3.14;
}
public double area1()
{
int r=5;
return r*r;
}
public Frame1() {
win=new Frame("My window");//设置窗体
win.setBackground(Color.white ) ;
win.resize(200,300) ;
win.setVisible(true); button1 = new Button("计算圆形面积");
button2 = new Button("计算四方形面积");
add(button1);
add(button2);
button1.getMouseListeners();
button2.getMouseListeners();//设置弹出对话框}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
D = new Dialog(win,"圆形",true);//弹出Dialog
D.setSize(80,80);
D.add(new Label("面积= "+String.valueOf(area()))) ;
D.show();
}
else {
if(e.getSource() == button2){
D1=new Dialog(win,"四方形",true);//弹出另一个Dialog
D1.setSize(70,80);
D1.add(new Label("面积= "+String.valueOf(area1()))) ;
D1.show();
}
}
}
public static void main(String[] args){
Frame1 f=new Frame1();
}
}
我觉得这样的话简洁点。
Frame1 f=new Frame1();
//少了这句
f.setVisible(true);
}
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame implements ActionListener{
private static JFrame win;
private JButton button1;
private JButton button2;
public double area(){
int r=5;
return r*r*3.14;
}
public double area1(){
int r=5;
return r*r;
}
public Frame1() {
win=new JFrame("My window");//ÉèÖô°Ìå
setBackground(Color.white ) ;
setSize(200,300) ;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JPanel jp=new JPanel();
button1 = new JButton("¼ÆËãÔ²ÐÎÃæ»ý");
button2 = new JButton("¼ÆËãËÄ·½ÐÎÃæ»ý");
jp.add(button1);
jp.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);//ÉèÖõ¯³ö¶Ô»°
Container c=getContentPane();
c.add(jp);
setContentPane(c);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
JOptionPane.showMessageDialog
(null,new JLabel("Ô²Ãæ»ý= "+String.valueOf(area())) );//µ¯³öDialog
}
else {
if(e.getSource() == button2){
JOptionPane.showMessageDialog
(null,new JLabel("Õý·½ÐÎÃæ»ý= "+String.valueOf(area())));//µ¯³öÁíÒ»¸öDialog
}
}
}
public static void main(String[] args){
Frame1 f=new Frame1();
}
}
*************************
呵呵,只会swing编程。
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame implements ActionListener{
private static JFrame win;
private JButton button1;
private JButton button2;
public double area(){
int r=5;
return r*r*3.14;
}
public double area1(){
int r=5;
return r*r;
}
public Frame1() {
win=new JFrame("My window");
setBackground(Color.white ) ;
setSize(200,300) ;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JPanel jp=new JPanel();
button1 = new JButton("area of circle");
button2 = new JButton("area of squarel");
jp.add(button1);
jp.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
Container c=getContentPane();
c.add(jp);
setContentPane(c);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
JOptionPane.showMessageDialog
(null,new JLabel("area of circle= "+String.valueOf(area())) );
}
else {
if(e.getSource() == button2){
JOptionPane.showMessageDialog
(null,new JLabel("area of square = "+String.valueOf(area())));
}
}
}
public static void main(String[] args){
Frame1 f=new Frame1();
}
}
**********************************************************
出现乱码,实在不好意思。
button2.getMouseListeners();
我觉得是getMouseListeners() 这个方法不对
应该用addActionListener()
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame implements ActionListener{
private static JFrame win;
private JButton button1;
private JButton button2;
public double area(){
int r=5;
return r*r*3.14;
}
public double area1(){
int r=5;
return r*r;
}
public Frame1() {
win=new JFrame("My window");
setBackground(Color.white ) ;
setSize(200,300) ;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JPanel jp=new JPanel();
button1 = new JButton("area of circle");
button2 = new JButton("area of squarel");
jp.add(button1);
jp.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
Container c=getContentPane();
c.add(jp);
setContentPane(c);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
JOptionPane.showMessageDialog
(null,new JLabel("area of circle= "+String.valueOf(area())) );
}
else {
if(e.getSource() == button2){
JOptionPane.showMessageDialog
(null,new JLabel("area of square = "+String.valueOf(area())));
}
}
}
public static void main(String[] args){
Frame1 f=new Frame1();
}
}
**********************************************************
这个代码符合你的要求的啊,你运行一下就知道了。
我如果用addActionListener()这个会出现错误的,说什么不能Frame 里面运用.