我已经运行了这个并修复了这些错误但它们显示它们没有修复!基本上,GUI假设有一个JcomboBox,其中一个人可以选择一个形状,并且该形状被随机抽取多次。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Practice4 extends JFrame
{
private final int CIRCLE = 0;
private final int SQUARE = 1;
private final int OVAL = 2;
private final int RECTANGLE = 3;
private int shape;
private JComboBox comboBox;
private String [ ] names = { "Circle", "Square", "Oval", "Rectangle" };
public Practice4 ( )
{
super ( "Drawing Random Shapes" );
comboBox = new JComboBox ( names );
getContentPane ( ).add ( comboBox, BorderLayout.SOUTH );
comboBox.addItemListener (
new ItemListener ( )
{
public void itemStateChanged ( ItemEvent e )
{
shape = comboBox.getSelectedIndex ( );
repaint ( );
}
}
);
setSize ( 400, 400 );
setVisible ( true );
}
public void paint ( Graphics g )
{
super.paint ( g );
Random r = new Random ( );
for ( int k = 1; k {
int x = r.nextInt ( 390 );
int y = r.nextInt ( 370 ) + 25;
int w = r.nextInt ( 400 - x );
int h = r.nextInt ( 400 - y );
switch ( shape )
{
case CIRCLE:
g.drawOval ( x, y, w, w );
break;
case SQUARE:
g.drawRect ( x, y, w, w );
break;
case OVAL:
g.drawOval ( x, y, w, h );
break;
case RECTANGLE:
g.drawRect ( x, y, w, h );
break;
}
}
}
public static void main ( String args [ ] )
{
Practice4 application = new Practice4 ( );
application.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
}
}
Error ';' expected
for (int k = 1; k (
error illegal start of expression
for (int k = 1; k (
答案 0 :(得分:2)
您的错误消息告诉您确切的错误:您的for循环不是有效的for循环。这不是有效的Java:
for ( int k = 1; k {
我甚至不确定你在这里要做什么,所以除了摆脱它并开始使用适当的Java语法之外,我不能给你建议。
一些旁注: