新手低分求助~关于绘图
题目:编写一个程序,以允许用户可以从一个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 );
}
}
------解决方案--------------------
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 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);
}
}