2016-09-01 9 views
1

配列リストから選択する際に少し問題があります。私は10個程度のJButtonをサークルで修正できるようにいくつかのコードを書いていますが、それは得意ですが、.....それぞれのボタンにactionListenerを設定したいのですが、ボタンは、ボタンに必要なアクションを継承します。どうすればそれを具体的にすることができますか...私のコードはここにあります....事前に感謝!特定の配列リストを取得する方法

private JButton quest; 

public Beginner() { 

    int n = 10; // no of JButtons 
    int radius = 200; 
    Point center = new Point(250, 250); 
    double angle = Math.toRadians(360/n); 

    List<Point> points = new ArrayList<Point>(); 

    points.add(center); 

    for (int i = 0; i < n; i++) { 
     double theta = i * angle; 
     int dx = (int) (radius * Math.sin(theta)); 
     int dy = (int) (radius * Math.cos(theta)); 
     Point p = new Point(center.x + dx, center.y + dy); 
     points.add(p); 
    } 

    draw(points); 

} 

public void draw(List<Point> points) { 

    JPanel panels = new JPanel(); 

    SpringLayout spring = new SpringLayout(); 

    // Layout used 
    int count = 1; 
    for (Point point : points) { 

     quest = new JButton("Question " + count); 
     quest.setForeground(Color.BLUE); 
     Font fonte = new Font("Script MT Bold", Font.PLAIN, 20); 
     quest.setFont(fonte); 

     add(quest); 
     count++; 

     spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels); 

     spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels); 

     setLayout(spring); 

     panels.setOpaque(false); 
     panels.setVisible(true); 
     panels.setLocation(10, 10); 

     add(panels); 

     // action Listener to be set on individual buttons 
     quest.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent a) { 

       if (quest.equals(points.get(5))) 
        ; 
       String c = "Hello!"; 
       JOptionPane.showMessageDialog(null, c); 
      } 
     }); 
    } 
} 
+0

クリックすると、それぞれのボタンに独自の独自のアクションを設定しますか? –

答えて

2

問題は表現

if (quest.equals(points.get(5))); 

は何もしないということです。私はそれがこの

if (quest.equals(points.get(5))) { 
    String c = "Hello!"; 
    JOptionPane.showMessageDialog(null, c); 
} 
+0

My!私の頭を爆発させる!ありがとう...それはちょうど働いた!! –

+0

@ Presh_K7問題はありません:) – vsminkov

0

私は質問を理解しています方法のように書き直すべきであると思いますが、複数のボタンがあり、それに関連付けられた独自のアクションだ持つように各ボタンをたいということです。これを行うにはいくつかの方法があります。作成するボタンに応じて、各JButtonの新しいActionListenerを作成します。

ActionListener内で、大文字/小文字のスイッチまたはif/elseを作成して、どのボタンが選択されたかによって決定されるようにすることもできます。これを行うには、ActionEventオブジェクトのgetActionCommand()関数を呼び出すことができます。

quest.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent a) { 

      if (a.getActionCommand().equals("Question 1")) 
      { 
       String c = "Hello!"; 
       JOptionPane.showMessageDialog(null, c); 
      } 
      else if(a.getActionCommand().equals("Question 2")) 
      { 
       //have it do something else 
      } 
      //and so on so forth 

     } 
    }); 
0

デザイン全体を再考する必要があります。あなたはこのラインを持っている:

if (quest.equals(points.get(5))) { 

しかしpointsは、Pointオブジェクトを含むリストです。 points.get(5)はポイントを返します。 クエストはJButtonです。 JButtonインスタンスとPointインスタンスの関係

+0

ポイントのものは、配列リストに接続されています....そこには、JButtonが組み込まれています! –

関連する問題