Есть ли способ добавить слушатель действия после того, как все создано? - PullRequest
0 голосов
/ 13 апреля 2020

Я уже давно учусь программировать java. Я слышал, если у меня есть какие-либо проблемы с моим кодом, я могу спросить здесь, поэтому я делаю это сейчас, я думаю. Сейчас я учусь использовать аддон Windowbuilder в Eclipse. Я слышал, что добавление слушателя действий решит мою проблему, но есть другая проблема. Я не знаю, возможно ли добавить слушателя действия и связать все с ним после того, как все спроектировано. Я хочу слушать эти поля, потому что мне нужны предопределенные переменные для расчета. Мне нужно прослушать getEggAmount, getMilkAmount и getFlourAmount JTextFields. Мой класс mainmenu здесь:

public class main {

  private JFrame frmBlinmaker;
  private JTextField getMilkAmount;
  private JTextField getFlourAmount;
  private JLabel MilkAmount;
  private JLabel FlourAmount;
  private JLabel MakeableBlins;
  private JLabel setMakeableBlins;
  private JLabel EggPortions;
  private JLabel MilkPortions;
  private JLabel FlourPortions;
  private JLabel setEggPortions;
  private JLabel setMilkPortions;
  private JLabel setFlourPortions;
  private JLabel EggNeed;
  private JLabel setEggNeed;
  private JLabel MilkNeed;
  private JLabel setMilkNeed;
  private JLabel FlourNeed;
  private JLabel lblNewLabel;
  private JLabel eggs;
  private JLabel milliliter;
  private JLabel grams;
  private JTextField setEggAmount;
  public int setResults() {
    makeable k = new makeable();
    int getResults = k.setFinalResult();
    return getResults;
  }

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                main window = new main();
                window.frmBlinmaker.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public main() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmBlinmaker = new JFrame();
    frmBlinmaker.setTitle("BlinMaker");
    frmBlinmaker.setBounds(100, 100, 450, 300);
    frmBlinmaker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout springLayout = new SpringLayout();
    frmBlinmaker.getContentPane().setLayout(springLayout);

    JButton Calculate = new JButton("CALCULATE");
    Calculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            makeable k = new makeable();
            k.main();
            setMakeableBlins.setText(Integer.toString(setResults()));
        }
    });
    Calculate.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(Calculate);

    getMilkAmount = new JTextField();
    springLayout.putConstraint(SpringLayout.WEST, Calculate, 0, SpringLayout.WEST, getMilkAmount);
    springLayout.putConstraint(SpringLayout.EAST, Calculate, 0, SpringLayout.EAST, getMilkAmount);
    springLayout.putConstraint(SpringLayout.NORTH, getMilkAmount, 45, SpringLayout.NORTH, frmBlinmaker.getContentPane());
    getMilkAmount.setFont(new Font("Tahoma", Font.PLAIN, 10));
    getMilkAmount.setText("Type here in milliliter");
    springLayout.putConstraint(SpringLayout.WEST, getMilkAmount, 10, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, getMilkAmount, 122, SpringLayout.WEST, frmBlinmaker.getContentPane());
    frmBlinmaker.getContentPane().add(getMilkAmount);
    getMilkAmount.setColumns(10);

    getFlourAmount = new JTextField();
    springLayout.putConstraint(SpringLayout.NORTH, Calculate, 6, SpringLayout.SOUTH, getFlourAmount);
    springLayout.putConstraint(SpringLayout.NORTH, getFlourAmount, 80, SpringLayout.NORTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, getMilkAmount, -6, SpringLayout.NORTH, getFlourAmount);
    getFlourAmount.setFont(new Font("Tahoma", Font.PLAIN, 10));
    getFlourAmount.setText("Type here in grams");
    springLayout.putConstraint(SpringLayout.SOUTH, getFlourAmount, -153, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, getFlourAmount, 10, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, getFlourAmount, 122, SpringLayout.WEST, frmBlinmaker.getContentPane());
    frmBlinmaker.getContentPane().add(getFlourAmount);
    getFlourAmount.setColumns(10);

    JButton getHelp = new JButton("What is this program?");
    springLayout.putConstraint(SpringLayout.SOUTH, Calculate, -65, SpringLayout.NORTH, getHelp);
    getHelp.setFont(new Font("Tahoma", Font.PLAIN, 10));
    getHelp.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            mainhelp m = new mainhelp();
            m.setVisible(true);
        }
    });
    springLayout.putConstraint(SpringLayout.NORTH, getHelp, -46, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, getHelp, 10, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, getHelp, -10, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    frmBlinmaker.getContentPane().add(getHelp);

    JLabel EggAmount = new JLabel("Egg Amount");
    springLayout.putConstraint(SpringLayout.WEST, EggAmount, 128, SpringLayout.WEST, frmBlinmaker.getContentPane());
    EggAmount.setFont(new Font("Tahoma", Font.BOLD, 11));
    springLayout.putConstraint(SpringLayout.NORTH, EggAmount, 17, SpringLayout.NORTH, frmBlinmaker.getContentPane());
    frmBlinmaker.getContentPane().add(EggAmount);

    MilkAmount = new JLabel("Milk Amount");
    MilkAmount.setFont(new Font("Tahoma", Font.BOLD, 11));
    springLayout.putConstraint(SpringLayout.NORTH, MilkAmount, 7, SpringLayout.NORTH, getMilkAmount);
    springLayout.putConstraint(SpringLayout.WEST, MilkAmount, 6, SpringLayout.EAST, getMilkAmount);
    frmBlinmaker.getContentPane().add(MilkAmount);

    FlourAmount = new JLabel("Flour Amount");
    FlourAmount.setFont(new Font("Tahoma", Font.BOLD, 11));
    springLayout.putConstraint(SpringLayout.NORTH, FlourAmount, 7, SpringLayout.NORTH, getFlourAmount);
    springLayout.putConstraint(SpringLayout.WEST, FlourAmount, 6, SpringLayout.EAST, getFlourAmount);
    frmBlinmaker.getContentPane().add(FlourAmount);

    MakeableBlins = new JLabel("The amount of makeable blins is:");
    springLayout.putConstraint(SpringLayout.NORTH, MakeableBlins, 10, SpringLayout.NORTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, MakeableBlins, 218, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, EggAmount, -22, SpringLayout.WEST, MakeableBlins);
    MakeableBlins.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
    frmBlinmaker.getContentPane().add(MakeableBlins);

    setMakeableBlins = new JLabel("0");
    springLayout.putConstraint(SpringLayout.NORTH, setMakeableBlins, 22, SpringLayout.SOUTH, MakeableBlins);
    springLayout.putConstraint(SpringLayout.WEST, setMakeableBlins, 94, SpringLayout.EAST, FlourAmount);
    springLayout.putConstraint(SpringLayout.SOUTH, setMakeableBlins, 105, SpringLayout.SOUTH, MakeableBlins);
    springLayout.putConstraint(SpringLayout.EAST, setMakeableBlins, -90, SpringLayout.EAST, frmBlinmaker.getContentPane());
    setMakeableBlins.setFont(new Font("Tahoma", Font.BOLD, 73));
    frmBlinmaker.getContentPane().add(setMakeableBlins);

    EggPortions = new JLabel("Portions of egg:");
    EggPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(EggPortions);

    MilkPortions = new JLabel("Portions of milk:");
    springLayout.putConstraint(SpringLayout.WEST, MilkPortions, 153, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, getHelp, -6, SpringLayout.WEST, MilkPortions);
    springLayout.putConstraint(SpringLayout.WEST, EggPortions, 0, SpringLayout.WEST, MilkPortions);
    springLayout.putConstraint(SpringLayout.SOUTH, EggPortions, -6, SpringLayout.NORTH, MilkPortions);
    MilkPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(MilkPortions);

    FlourPortions = new JLabel("Portions of flour:");
    springLayout.putConstraint(SpringLayout.WEST, FlourPortions, 6, SpringLayout.EAST, getHelp);
    springLayout.putConstraint(SpringLayout.SOUTH, FlourPortions, -10, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, MilkPortions, -6, SpringLayout.NORTH, FlourPortions);
    FlourPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(FlourPortions);

    setEggPortions = new JLabel("0");
    setEggPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.NORTH, setEggPortions, 1, SpringLayout.NORTH, EggPortions);
    springLayout.putConstraint(SpringLayout.WEST, setEggPortions, 6, SpringLayout.EAST, EggPortions);
    frmBlinmaker.getContentPane().add(setEggPortions);

    setMilkPortions = new JLabel("0");
    springLayout.putConstraint(SpringLayout.NORTH, setMilkPortions, 5, SpringLayout.SOUTH, setEggPortions);
    setMilkPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.WEST, setMilkPortions, 6, SpringLayout.EAST, MilkPortions);
    frmBlinmaker.getContentPane().add(setMilkPortions);

    setFlourPortions = new JLabel("0");
    springLayout.putConstraint(SpringLayout.SOUTH, setFlourPortions, -10, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    setFlourPortions.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.WEST, setFlourPortions, 6, SpringLayout.EAST, FlourPortions);
    frmBlinmaker.getContentPane().add(setFlourPortions);

    EggNeed = new JLabel("You will need:");
    springLayout.putConstraint(SpringLayout.NORTH, EggNeed, 0, SpringLayout.NORTH, EggPortions);
    springLayout.putConstraint(SpringLayout.WEST, EggNeed, 15, SpringLayout.EAST, setEggPortions);
    EggNeed.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(EggNeed);

    setEggNeed = new JLabel("0");
    setEggNeed.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.NORTH, setEggNeed, 1, SpringLayout.NORTH, EggPortions);
    springLayout.putConstraint(SpringLayout.WEST, setEggNeed, 6, SpringLayout.EAST, EggNeed);
    frmBlinmaker.getContentPane().add(setEggNeed);

    MilkNeed = new JLabel("You will need:");
    springLayout.putConstraint(SpringLayout.NORTH, MilkNeed, 6, SpringLayout.SOUTH, EggNeed);
    springLayout.putConstraint(SpringLayout.EAST, MilkNeed, 0, SpringLayout.EAST, EggNeed);
    MilkNeed.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(MilkNeed);

    setMilkNeed = new JLabel("0");
    springLayout.putConstraint(SpringLayout.NORTH, setMilkNeed, 5, SpringLayout.SOUTH, setEggNeed);
    setMilkNeed.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.WEST, setMilkNeed, 0, SpringLayout.WEST, setEggNeed);
    frmBlinmaker.getContentPane().add(setMilkNeed);

    FlourNeed = new JLabel("You will need:");
    springLayout.putConstraint(SpringLayout.SOUTH, FlourNeed, -10, SpringLayout.SOUTH, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, FlourNeed, 0, SpringLayout.EAST, EggNeed);
    FlourNeed.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(FlourNeed);

    lblNewLabel = new JLabel("0");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 0, SpringLayout.NORTH, FlourPortions);
    springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 0, SpringLayout.WEST, setEggNeed);
    frmBlinmaker.getContentPane().add(lblNewLabel);

    eggs = new JLabel("eggs");
    springLayout.putConstraint(SpringLayout.NORTH, eggs, 0, SpringLayout.NORTH, EggPortions);
    springLayout.putConstraint(SpringLayout.WEST, eggs, 6, SpringLayout.EAST, setEggNeed);
    eggs.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(eggs);

    milliliter = new JLabel("ml");
    springLayout.putConstraint(SpringLayout.NORTH, milliliter, 6, SpringLayout.SOUTH, eggs);
    springLayout.putConstraint(SpringLayout.WEST, milliliter, 6, SpringLayout.EAST, setMilkNeed);
    springLayout.putConstraint(SpringLayout.EAST, milliliter, -18, SpringLayout.EAST, frmBlinmaker.getContentPane());
    milliliter.setFont(new Font("Tahoma", Font.BOLD, 12));
    frmBlinmaker.getContentPane().add(milliliter);

    grams = new JLabel("grams");
    grams.setFont(new Font("Tahoma", Font.BOLD, 12));
    springLayout.putConstraint(SpringLayout.NORTH, grams, 0, SpringLayout.NORTH, FlourPortions);
    springLayout.putConstraint(SpringLayout.WEST, grams, 6, SpringLayout.EAST, lblNewLabel);
    frmBlinmaker.getContentPane().add(grams);

    JButton getHelpForNeed = new JButton("What is this here?");
    getHelpForNeed.setFont(new Font("Tahoma", Font.PLAIN, 10));
    springLayout.putConstraint(SpringLayout.NORTH, getHelpForNeed, -32, SpringLayout.NORTH, EggNeed);
    springLayout.putConstraint(SpringLayout.WEST, getHelpForNeed, 0, SpringLayout.WEST, EggNeed);
    springLayout.putConstraint(SpringLayout.SOUTH, getHelpForNeed, -9, SpringLayout.NORTH, EggNeed);
    springLayout.putConstraint(SpringLayout.EAST, getHelpForNeed, 0, SpringLayout.EAST, eggs);
    frmBlinmaker.getContentPane().add(getHelpForNeed);

    JButton getHelpForPortions = new JButton("What is this here?");
    springLayout.putConstraint(SpringLayout.SOUTH, getHelpForPortions, 23, SpringLayout.NORTH, getHelpForNeed);
    getHelpForPortions.setFont(new Font("Tahoma", Font.PLAIN, 10));
    springLayout.putConstraint(SpringLayout.NORTH, getHelpForPortions, 0, SpringLayout.NORTH, getHelpForNeed);
    springLayout.putConstraint(SpringLayout.WEST, getHelpForPortions, 0, SpringLayout.WEST, EggPortions);
    springLayout.putConstraint(SpringLayout.EAST, getHelpForPortions, 0, SpringLayout.EAST, setFlourPortions);
    frmBlinmaker.getContentPane().add(getHelpForPortions);

    setEggAmount = new JTextField();
    springLayout.putConstraint(SpringLayout.NORTH, setEggAmount, -7, SpringLayout.NORTH, EggAmount);
    springLayout.putConstraint(SpringLayout.WEST, setEggAmount, 10, SpringLayout.WEST, frmBlinmaker.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, setEggAmount, -6, SpringLayout.NORTH, getMilkAmount);
    springLayout.putConstraint(SpringLayout.EAST, setEggAmount, -6, SpringLayout.WEST, EggAmount);
    frmBlinmaker.getContentPane().add(setEggAmount);
    setEggAmount.setColumns(10);
}
public int getEggAmount() {
    int getEggAmount;
    getEggAmount = Integer.parseInt(setEggAmount.getText());
    return getEggAmount;
}
public int getMilkAmount() {
    int setMilkAmount;
    setMilkAmount = Integer.parseInt(getMilkAmount.getText());
    return setMilkAmount;
}
public int getFlourAmount() {
    int setFlourAmount;
    setFlourAmount = Integer.parseInt(getFlourAmount.getText());
    return setFlourAmount;
}
}
...