私は2つのリストと3つのボタンを持っています。リストには、基本的な形状の要素が含まれています。ex "Rect"を選択すると、キャンバスに長方形が描画されます。今、私は2番目のリスト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);
}
}
}
}
送信をクリックする前に図 2http://img152.imageshack.us/img152/4594/capture1ypd.png
送信ボタンをクリックした後 http://img46.imageshack.us/img46/854/capture2bk.png