2016-07-01 3 views
0

でスイング。最初のJPanelには簡単なメッセージが含まれています。もう一つは、いくつかの(最大15)JButton S、異なるString(都市の名前)を含むそれぞれを含有する必要があります。私はJButtonを押すと、私は(私は)(GUI.getStringセッターを持っている)GUIという名前のクラスでStringJButton(都市の名前)にStringを設定する必要があります。これまでの私のコードですが、私はそれをどのように仕上げるのか分かりません。動のは、私が<code>JFrame</code><code>JPanel</code> 2とSを作成する必要があるアクションリスナー

public class AskCityPermit extends JFrame implements Runnable{ 
    String string; 
    GUI gui; 
    public static final long serialVersionUID = 1L; 

    public AskCityPermit(GUI gui, ArrayList<String> cities){ 

     this.gui=gui; 
     int i=0; 
     JPanel textPanel = new JPanel(); 
     JPanel buttonsPanel = new JPanel(); 
     JButton[] buttons = new JButton[15]; 
     for (String s:cities){ 
      buttons[i]=new JButton(s); 
      buttonsPanel.add(buttons[i]); 
     } 


     this.setLayout(new BorderLayout()); 
     textPanel.setLayout(new FlowLayout()); 
     buttonsPanel.setLayout(new FlowLayout()); 

     textPanel.add(new JLabel("Choose a city")); 

     this.add(textPanel,BorderLayout.NORTH); 
     this.add(buttonsPanel,BorderLayout.SOUTH); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setAlwaysOnTop(true); 
     this.setResizable(false); 
     this.setVisible(true); 
    } 

    @Override 
    public void run() { 
     SwingUtilities.invokeLater(new Runnable(){ 

      @Override 
      public void run() { 

       } 


     }); 
    } 
} 
+0

答えは質問のタイトルです。ボタンのテキストを取得し、gui.setString()を呼び出すActionListenerを各ボタンに追加する必要があります。 –

+0

[OK]を、どのように私は、各ボタンのアクションリスナーを追加することができますか? Javaの8と –

+0

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html –

答えて

0

ActionListenerのすべてのボタンをバインドすることを検討してください。ここに例があります:

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

class GUI 
{ 
    public String that_one; // the string you want to change 
} 

public class AskCityPermit extends JFrame implements Runnable{ 
    String string; 
    GUI gui; 
    public static final long serialVersionUID = 1L; 

    public AskCityPermit(GUI gui, ArrayList<String> cities){ 
     this.gui=gui; 
     int i=0; 
     JPanel textPanel = new JPanel(); 
     JPanel buttonsPanel = new JPanel(); 
     // JButton[] buttons = new JButton[15]; 
     // create action listener object: 
     ActionListener btnaction = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       gui.that_one = ((JButton) ae.getSource()).getText(); 
      } 
     }; 
     for (String s:cities){ 
      JButton button =new JButton(s); 
      buttonsPanel.add(button); 
      // bind action listener to each button: 
      button.addActionListener(btnaction); 
     } 


     this.setLayout(new BorderLayout()); 
     textPanel.setLayout(new FlowLayout()); 
     buttonsPanel.setLayout(new FlowLayout()); 

     textPanel.add(new JLabel("Choose a city")); 

     this.add(textPanel,BorderLayout.NORTH); 
     this.add(buttonsPanel,BorderLayout.SOUTH); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setAlwaysOnTop(true); 
     this.setResizable(false); 
     this.setVisible(true); 
    } 

    @Override 
    public void run() { 
    } 

    public static void main(String [] args) 
    { 
     ArrayList<String> cities = new ArrayList<String>(); 
     cities.add("City A"); 
     cities.add("City B"); 
     cities.add("City C"); 
     GUI gui = new GUI(); 
     AskCityPermit frm = new AskCityPermit(gui, cities); 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       frm.setVisible(true); 
      } 
     }); 
    } 
} 
+0

ありがとう!できます!神のお恵みがありますように! –

+0

@SauloTammariは、あなたが答えを受け入れるならば、それは素晴らしいことだ、歓迎しています。ダニをクリックします。 –

関連する問題