为什么老弹不出对话框?????大家帮忙啊!!

fyj35030 2006-11-30 09:37:18
我的题目是:"有一个窗口,该窗口有两个依赖于它的对话框,一个对话框负责求圆的面积,另一个负责求三角形的面积"
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();
}
}
...全文
244 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fyj35030 2006-12-03
  • 打赏
  • 举报
回复
lulin_19870207() 你好
我如果用addActionListener()这个会出现错误的,说什么不能Frame 里面运用.
lulin_19870207 2006-12-02
  • 打赏
  • 举报
回复
button1.getMouseListeners();
button2.getMouseListeners();
我觉得是getMouseListeners() 这个方法不对
应该用addActionListener()
fyj35030 2006-12-02
  • 打赏
  • 举报
回复
大家能不能伸出援助之手啊,不然我就挂掉了,问了好多人, But nobody know it.
yztommyhc 2006-12-02
  • 打赏
  • 举报
回复
import java.awt.*;
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();
}
}
**********************************************************
这个代码符合你的要求的啊,你运行一下就知道了。
honeyego 2006-12-02
  • 打赏
  • 举报
回复
推荐用VE,方便多了
还不会出错
上面的代码看着头晕
sxnucseven 2006-12-01
  • 打赏
  • 举报
回复
public static void main(String[] args){
Frame1 f=new Frame1();
//少了这句
f.setVisible(true);
}
yztommyhc 2006-12-01
  • 打赏
  • 举报
回复
import java.awt.*;
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();
}
}
**********************************************************
出现乱码,实在不好意思。
yztommyhc 2006-12-01
  • 打赏
  • 举报
回复
import java.awt.*;
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编程。
fyj35030 2006-12-01
  • 打赏
  • 举报
回复
sxnucseven(No.7)你好,我在程序里是有定义Frame的,可能程序太乱不好看.

那我的这个程序是那里不问题大家有没有发现出来啊!
wangdaoyong 2006-12-01
  • 打赏
  • 举报
回复
使用javax.swing.JOptionPane.showMessage()简单很多,而且直接给button加mouseclick事件更直接明了
yztommyhc 2006-11-30
  • 打赏
  • 举报
回复
如果可以的话,为什么不用javax.swing的包呢?
我觉得这样的话简洁点。

62,615

社区成员

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

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