62,614
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}
private String shape;
中把shape赋值为Line,编译后,就没有出现第一个问题的情况,却会在没有按确定就自动生成Line图形,那应该如何修改才不这样呢?