简体   繁体   English

具有动作侦听器的JButtons Swing的动态

[英]Dinamic of JButtons Swing with action listener

I need to create a JFrame with 2 JPanel s. 我需要用2个JPanel创建一个JFrame The first JPanel contains a simple message. 第一个JPanel包含一条简单消息。 The second one needs to contain some (max 15) JButton s, each one containing a different String (name of a city). 第二个需要包含一些(最多15个) JButton ,每个JButton包含一个不同的String (城市名称)。 When I press a JButton , I need to set the String in the JButton (the name of the city) on a String in a class named GUI (I have the setter GUI.getString()). 当我按下JButton ,我需要在名为GUI的类中的String上设置JButtonString (城市名称)(我有setter GUI.getString())。 That's my code so far, I don't know how to finish it. 到目前为止,这是我的代码,我不知道如何完成它。

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() {

                }


        }); 
    }
}

Consider binding every button an ActionListener object. 考虑将每个按钮绑定到一个ActionListener对象。 Here is an example: 这是一个例子:

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);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM