我需要创建一个程序来绘制形状(用户用单选按钮选择),以及形状是否被填充(用户用复选框选择)。这是我到目前为止的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SelectShape extends JFrame implements ItemListener{
private JRadioButton line = new JRadioButton("Line");
private JRadioButton rect = new JRadioButton("Rectangle");
private JRadioButton oval = new JRadioButton("Oval");
private JCheckBox fill = new JCheckBox("Filled");
private FigurePanel fp;
public static void main(String[] args) {
SelectShape frame = new SelectShape();
frame.setTitle("Select Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400,400);
frame.setVisible(true);
}
public SelectShape() {
JPanel p1 = new JPanel();
p1.add(fp);
fp.setBackground(Color.WHITE);
p1.setSize(200,400);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(line);
p2.add(rect);
p2.add(oval);
p2.add(fill);
add(p2, "South");
line.addItemListener(this);
rect.addItemListener(this);
oval.addItemListener(this);
fill.addItemListener(this);
}
public void ItemStateChanged(ItemEvent e) {
if(rect.isSelected()) {
FigurePanel.dRect();
repaint();
}
else if(oval.isSelected()) {
FigurePanel.dOval();
repaint();
}
else if(line.isSelected()) {
FigurePanel.dLine();
repaint();
}
if(fill.isSelected()) {
FigurePanel.fill();
repaint();
}
else {
FigurePanel.erase();
repaint();
}
}
}
class FigurePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
public void dLine(Graphics g) {
g.drawLine(10,10,160,10);
}
public void dRect(Graphics g) {
g.drawRect(10,10,150,50);
}
public void dOval(Graphics g) {
g.drawOval(10,10,150,50);
}
public void fill(Graphics g) {
g.setColor(Color.GREEN);
if(rect.isSelected()) {
g.fillRect(10,10,150,50);
}
else if(oval.isSelected()) {
g.fillOval(10,10,150,50);
}
}
public void erase(Graphics g) {
g.setColor(Color.WHITE);
if(rect.isSelected()) {
g.fillRect(10,10,150,50);
}
else if(oval.isSelected()) {
g.fillOval(10,10,150,50);
}
}
}
}
我得到的错误是表达式和标识符的非法开头。如果我应该以另一种方式处理这个问题,请告诉我。