新手低分求助~关于绘图

chidun123 2012-03-30 09:20:48
题目:编写一个程序,以允许用户可以从一个JComboBox中选择一个形状,然后用paint方法在随机的位置用随机的尺寸绘制该形状20次。在第一次调用paint方法时,应将JComboBox中的第一个列表项作为默认形状来显示。
下面是我写的代码。问题有3个:
1、第一次调用paint方法绘制Line时点确认按钮没反应,必须是第二次选择Line后才可以绘制;
2、绘制一次后,第二次按确认按钮无法将前一次的绘制结果清除。
3.JLabel标签没有显示,当注释掉paint()方法后才会出现。
我是初学者,望各位强人不吝赐教,拜谢!



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

public class Heyu3 extends JFrame{
private JComboBox shapeBox;
private JButton yesButton;
private JLabel shapeLabel, label;
private String shapeNames[] = { "Line", "Rectangle", "Oval"};
private String shape;


public Heyu3(){
super("绘制自选图形");

Container container = getContentPane();
container.setLayout( new FlowLayout() );

shapeLabel = new JLabel("请选择您要绘制的图形:");

shapeBox = new JComboBox( shapeNames );
shapeBox.setMaximumRowCount( 3 );
shapeBox.addItemListener(

new ItemListener(){

public void itemStateChanged( ItemEvent event ){
shape = shapeNames[ shapeBox.getSelectedIndex() ];

}
}

);

yesButton = new JButton("确定");
yesButton.addActionListener( new YesButtonHandler() );

container.add( shapeLabel );
container.add( shapeBox );
container.add( yesButton );

setSize( 500, 500 );
setVisible( true );

}

private class YesButtonHandler implements ActionListener{

public void actionPerformed( ActionEvent event ){

if( event.getSource() == yesButton)

repaint();

}
}

public void paint( Graphics g ){
for(int count=0;count<20;count++){

if(shape.equals("Line"))
g.drawLine((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);

else if(shape.equals("Rectangle"))
g.drawRect((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);

else
g.drawOval((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);

}

}
public static void main( String args[]){
Heyu3 application = new Heyu3();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

}
...全文
160 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chidun123 2012-04-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

Java code

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

public class Heyu3 extends JFrame {
private JComboBox shapeBox;
private JButton yesButton;
private JLabel sh……
[/Quote]
终于都可以了!!非常感谢!
sdojqy1122 2012-03-31
  • 打赏
  • 举报
回复

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

public class Heyu3 extends JFrame {
private JComboBox shapeBox;
private JButton yesButton;
private JLabel shapeLabel, label;
private String shapeNames[] = {"Line","Rectangle","Oval"};
private String shape="";//赋初值,以免报空指针异常
public Heyu3() {
super("绘制自选图形");
Container container = getContentPane();
container.setLayout(new FlowLayout());
shapeLabel = new JLabel("请选择您要绘制的图形:");
shapeBox = new JComboBox(shapeNames);
shapeBox.setMaximumRowCount(3);
// shapeBox.addItemListener(这是为那般??我感觉没用,除非你想根据这个触发.
// new ItemListener(){
// public void itemStateChanged(ItemEvent event) {
// shape = shapeNames[shapeBox.getSelectedIndex()];
// }
// });
yesButton = new JButton("确定");
yesButton.addActionListener(new YesButtonHandler());

container.add(shapeLabel);
container.add(shapeBox);
container.add(yesButton);

setSize(500, 500);
setVisible(true);

}

private class YesButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == yesButton)
shape = (String) shapeBox.getSelectedItem();//赋值整到这里来
repaint();
}
}
public void paint(Graphics g){
super.paint(g);
for (int count = 0; count < 20; count++) {
if (shape.equals("Line"))
g.drawLine((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);

else if (shape.equals("Rectangle"))
g.drawRect((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);

else if (shape.equals("Oval"))//是这里。。。,别整个else
g.drawOval((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);
}
}
public static void main(String args[]) {
Heyu3 application = new Heyu3();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
chidun123 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

Java code

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

public class Heyu3 extends JFrame {
private JComboBox shapeBox;
private JButton yesButton;
private JLabel sh……
[/Quote]
对喔,不过还是还没按确定键就画图了
chidun123 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 4 楼 的回复:

引用 2 楼 的回复:

Line 是JComboBox的三个选项的第一个,所以是默认显示的,你在绘制Line时应该没有对JComboBox的选项进行选择,由于你对JComboBox监听的是选项改变的事件,所以shapa没变还是null,点击确认按钮时,当然绘不出来了,第二次选择Line时,由于触发了itemStateChanged事件,所以shapa不为n……
[/Quote]
还是不行~
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 2 楼 的回复:

Line 是JComboBox的三个选项的第一个,所以是默认显示的,你在绘制Line时应该没有对JComboBox的选项进行选择,由于你对JComboBox监听的是选项改变的事件,所以shapa没变还是null,点击确认按钮时,当然绘不出来了,第二次选择Line时,由于触发了itemStateChanged事件,所以shapa不为null,可以绘出来


受……
[/Quote]
把这句话:
shape = shapeNames[ shapeBox.getSelectedIndex() ];
发在确认按钮的单击事件中试试……
private class YesButtonHandler implements ActionListener{

public void actionPerformed( ActionEvent event ){

if( event.getSource() == yesButton){

shape = shapeNames[ shapeBox.getSelectedIndex() ];
repaint();

}
}
}
sdojqy1122 2012-03-30
  • 打赏
  • 举报
回复

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

public class Heyu3 extends JFrame {
private JComboBox shapeBox;
private JButton yesButton;
private JLabel shapeLabel, label;
private String shapeNames[] = {"Line","Rectangle","Oval"};
private String shape="";//赋初值,以免报空指针遗产
public Heyu3() {
super("绘制自选图形");
Container container = getContentPane();
container.setLayout(new FlowLayout());
shapeLabel = new JLabel("请选择您要绘制的图形:");
shapeBox = new JComboBox(shapeNames);
shapeBox.setMaximumRowCount(3);
// shapeBox.addItemListener(这是为那般??我感觉没用,除非你想根据这个触发.
// new ItemListener(){
// public void itemStateChanged(ItemEvent event) {
// shape = shapeNames[shapeBox.getSelectedIndex()];
// }
// });
yesButton = new JButton("确定");
yesButton.addActionListener(new YesButtonHandler());

container.add(shapeLabel);
container.add(shapeBox);
container.add(yesButton);

setSize(500, 500);
setVisible(true);

}

private class YesButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == yesButton)
repaint();
}
}
public void paint(Graphics g){
super.paint(g);
for (int count = 0; count < 20; count++) {
shape = (String)shapeBox.getSelectedItem();
if (shape.equals("Line"))
g.drawLine((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);

else if (shape.equals("Rectangle"))
g.drawRect((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);

else
g.drawOval((int) (Math.random() * 100) + 50, (int) (Math
.random() * 100) + 150, (int) (Math.random() * 100)
* (int) (Math.random() * 10) + 20,
(int) (Math.random() * 10) * (int) (Math.random() * 10)
+ 20);
}
}
public static void main(String args[]) {
Heyu3 application = new Heyu3();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
chidun123 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

Line 是JComboBox的三个选项的第一个,所以是默认显示的,你在绘制Line时应该没有对JComboBox的选项进行选择,由于你对JComboBox监听的是选项改变的事件,所以shapa没变还是null,点击确认按钮时,当然绘不出来了,第二次选择Line时,由于触发了itemStateChanged事件,所以shapa不为null,可以绘出来
[/Quote]

受教了~但是当我在
private String shape;
中把shape赋值为Line,编译后,就没有出现第一个问题的情况,却会在没有按确定就自动生成Line图形,那应该如何修改才不这样呢?
chidun123 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

主要问题是:
public void paint(Graphics g ){
第一句话,请写:
super.paint(g);
[/Quote]

嗯,这个很好,解决了第二个和第三个问题了,非常感谢!但是第一个问题还是没有解决到,一运行程序就会有:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException的警告提示
  • 打赏
  • 举报
回复
Line 是JComboBox的三个选项的第一个,所以是默认显示的,你在绘制Line时应该没有对JComboBox的选项进行选择,由于你对JComboBox监听的是选项改变的事件,所以shapa没变还是null,点击确认按钮时,当然绘不出来了,第二次选择Line时,由于触发了itemStateChanged事件,所以shapa不为null,可以绘出来
MiceRice 2012-03-30
  • 打赏
  • 举报
回复
主要问题是:
public void paint(Graphics g ){
第一句话,请写:
super.paint(g);

62,614

社区成员

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

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