我有两个列表和三个按钮。该列表具有基本形状的元素。当我选择前“矩形”时,它应该在画布中绘制矩形。现在我已经为第二个列表 Rectangle 实现了。矩形绘制得很完美,但我的列表又被重复了,为什么?以及如何很好地适应它们?. 我在哪里做错了
这是以下代码:
package src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class Main implements ActionListener{
public static void main(String[] args){
Main gui = new Main();
gui.go();
}
public void go(){
frame = new JFrame();
panel = new JPanel();
String figures[] = {"Rectangle","Rounded Rectangle", "Arc", "Line","Cubic curve"};
drawing1 = new JList<String>(figures);
drawing2 = new JList<String>(figures);
connect_button = new JButton("connect them");
submit1 = new JButton("Submit figure 1");
submit2 = new JButton("Submit figure 2");
connect_button.addActionListener(this);
submit1.addActionListener(this);
submit2.addActionListener(this);
panel.add(drawing2);
panel.add(drawing1);
panel.add(connect_button);
panel.add(submit1);
panel.add(submit2);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setBackground(Color.gray);
frame.getContentPane().add(BorderLayout.NORTH,panel);
frame.getContentPane().add(BorderLayout.CENTER,draw);
frame.setSize(5000,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()== submit1){
}
else if(event.getSource()== submit2){
type = drawing1.getSelectedValue();
draw.repaint();
}
else if(event.getSource()== connect_button){
}
}
DrawingPanel draw = new DrawingPanel();
JPanel panel;
JFrame frame;
JButton connect_button;
JButton submit1;
JButton submit2;
JList<String> drawing1;
JList<String> drawing2;
String type;
public class DrawingPanel extends JPanel{
public void paintComponent(Graphics G){
Graphics2D g2d = (Graphics2D) G;
if(type=="Rectangle"){
Rectangle2D r2d = new Rectangle2D.Float(10f, 10f, 130f, 130f);
g2d.draw(r2d);
}
}
}
}
点击提交图2之前 http://img152.imageshack.us/img152/4594/capture1ypd.png