| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1021 人关注过本帖
标题:求助 java
只看楼主 加入收藏
ft5032839
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2004-11-30
收藏
 问题点数:0 回复次数:3 
求助 java

要求:

在一个窗口中有两个Button,一个为Circle一个为Rectangle,点击CirckeButton,在窗口上出现

一个圆,点击RectangleButton出现一个矩形.以下是小弟写的程序,不知道那里错了,就是画不出东西...

小弟刚学,求教各位大虾!!

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*;

public class Cf { public static void main(String[] args) { Drawframe frame=new Drawframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class Drawframe extends JFrame { private static final int DW=400; private static final int DH=400; public Drawframe() { setTitle("CF"); setSize(DW,DH); Buttonpanel panel=new Buttonpanel(); Container content=getContentPane(); content.add(panel); } } class Drawpanel extends JPanel { Rectangle2D rect; Ellipse2D circle; Graphics2D g2; public void paintComponent(Graphics g) { super.paintComponent(g); g2=(Graphics2D)g; rect=new Rectangle.Double(100,100,200,150); double x=rect.getCenterX(); double y=rect.getCenterY(); double r=100; circle=new Ellipse2D.Double(); circle.setFrameFromCenter(x,y,x+r,y+r); } } class Buttonpanel extends JPanel { JButton CircleButton=new JButton("Circle"); JButton RectButton=new JButton("Rectangle"); public Buttonpanel () { add(CircleButton); add(RectButton); DrawAction draw=new DrawAction(); CircleButton.addActionListener(draw); RectButton.addActionListener(draw); }

private class DrawAction implements ActionListener { public void actionPerformed(ActionEvent event) { Drawpanel s=new Drawpanel(); Object source=event.getSource(); if(source==CircleButton) {repaint();s.g2.draw(s.circle);} else if (source==RectButton) {repaint();s.g2.draw(s.rect);} } } }

搜索更多相关主题的帖子: java import awt frame public 
2004-12-06 16:24
xiayongjie
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2004-12-3
收藏
得分:0 

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*;

public class Cfx { public static void main(String[] args) { Drawframe frame=new Drawframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.show(); } } class Drawframe extends JFrame { private static final int DW=400; private static final int DH=400; public Drawframe() { setTitle("CF"); Buttonpanel panel=new Buttonpanel(); Container content=getContentPane(); content.add(panel); setSize(DW,DH); show(); setVisible(true); } }

class Drawpanel extends JPanel { public final static int CIRCLE = 1,SQUARE = 2; private int shape;

public void paintComponent (Graphics g ) { super.paintComponent (g);

if ( shape == CIRCLE) g.fillOval(50,10,60,60); else if (shape == SQUARE ) g.fillRect(50,10,60,60); }

public void draw (int shapeToDraw) { shape = shapeToDraw; repaint(); } }

class Buttonpanel extends JPanel { JButton CircleButton=new JButton("Circle"); JButton RectButton=new JButton("Rectangle"); Drawpanel s; public Buttonpanel () { s = new Drawpanel(); setLayout(new BorderLayout ()); add(CircleButton,BorderLayout.EAST); add(RectButton,BorderLayout.WEST); add(s,BorderLayout.CENTER); DrawAction draw=new DrawAction(); CircleButton.addActionListener(draw); RectButton.addActionListener(draw); }

private class DrawAction implements ActionListener { public void actionPerformed(ActionEvent event) {

Object source = event.getSource();

if(source.equals(CircleButton)) { s.draw(Drawpanel.CIRCLE); } else if (source.equals(RectButton)) { s.draw(Drawpanel.SQUARE); } } } }

改成这样可以了。

2004-12-06 19:21
xiayongjie
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2004-12-3
收藏
得分:0 
s 在private class中是无法被外面承认的。
2004-12-06 19:22
ft5032839
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2004-11-30
收藏
得分:0 

那只要把我 Drawpanel s;定义在外部类就可以了吗?我试过程序还是性,还出现很多异常..

你的程序我看懂..谢谢.

加我QQ.25365514

2004-12-07 11:02
快速回复:求助 java
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013480 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved