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

题目要求: 在一个JComboBox选中其中的Rect,在面板内画个矩形,选中Circle 则画个圆.

以下是小弟写的程序:

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class DrawCombo { public static void main(String[] args) { Drawframe frame=new Drawframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class Drawframe extends JFrame { Drawpanel s; private JComboBox drawCombo; public Drawframe() { setTitle("DrawFrame"); setSize(300,200); drawCombo=new JComboBox(); drawCombo.setEditable(false); drawCombo.addItem("Circle"); drawCombo.addItem("Rect"); drawCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String s2=(String)drawCombo.getSelectedItem(); if(s2=="Circle"){ s.drawf(Drawpanel.CIRCLE);} else if (s2=="Rect"){s.drawf(Drawpanel.SQUARE);} } }); Drawpanel panel=new Drawpanel(); Container contentPane=getContentPane(); JPanel comboPanel=new JPanel(); comboPanel.add(drawCombo); contentPane.add(panel); contentPane.add(comboPanel,BorderLayout.SOUTH); } }

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

public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D f2=(Graphics2D)g; Rectangle2D rect=new Rectangle.Double(100,100,200,150); double x=rect.getCenterX(); double y=rect.getCenterY(); double r=50; Ellipse2D circle=new Ellipse2D.Double(); circle.setFrameFromCenter(x,y,x+r,y+r); if ( shape == CIRCLE) {f2.draw(circle);} else if (shape == SQUARE ) {f2.draw(rect);} } public void drawf (int shapeToDraw) { shape = shapeToDraw; repaint(); } } 不知道那里错了.请哪位大侠帮帮忙.

搜索更多相关主题的帖子: java 习题 
2004-12-20 16:17
快速回复:[求助]求助 一道java习题
数据加载中...
 
   



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

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