でスイング。最初のJPanel
には簡単なメッセージが含まれています。もう一つは、いくつかの(最大15)JButton
S、異なるString
(都市の名前)を含むそれぞれを含有する必要があります。私はJButton
を押すと、私は(私は)(GUI.getStringセッターを持っている)GUIという名前のクラスでString
にJButton
(都市の名前)に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() {
}
});
}
}
答えは質問のタイトルです。ボタンのテキストを取得し、gui.setString()を呼び出すActionListenerを各ボタンに追加する必要があります。 –
[OK]を、どのように私は、各ボタンのアクションリスナーを追加することができますか? Javaの8と –
https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html –