-1
//===== импорты =====
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//===================

public class Okno extends JFrame {
    public static int Money = 0;
    public static int earn = 5;
    public static int price = 10;
    public static String label = null;
    public static String label2 = "price: " + price;
    public static String label3 = "earn: " + earn;
    public static JButton Upgrade = new JButton("Upgrade");
    public static JLabel MoneyDisplay = new JLabel(label);
    public static JLabel PriceDisplay = new JLabel(label2);
    public static JLabel EarnDisplay = new JLabel(label3);
        public Okno() {
        super("Earn money with Konstantin!");
        this.setBounds(100, 100, 220, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cont = this.getContentPane();
        Tread sle = new Tread();
        
        SpringLayout layout = new SpringLayout();

        Upgrade.setPreferredSize(new Dimension(100, 30));
        
        cont.setLayout(layout);
        cont.add(MoneyDisplay);
        cont.add(PriceDisplay);
        cont.add(EarnDisplay);
        Upgrade.addActionListener(new ButtonEventListener());
        cont.add(Upgrade);

        layout.putConstraint(SpringLayout.WEST, MoneyDisplay, 10, SpringLayout.WEST, cont);         
        layout.putConstraint(SpringLayout.NORTH, MoneyDisplay, 30, SpringLayout.NORTH, cont);

        layout.putConstraint(SpringLayout.WEST, EarnDisplay, 120, SpringLayout.WEST, cont);         
        layout.putConstraint(SpringLayout.NORTH, EarnDisplay, 30, SpringLayout.NORTH, cont);


        layout.putConstraint(SpringLayout.WEST, PriceDisplay, 120, SpringLayout.WEST, cont);
        layout.putConstraint(SpringLayout.NORTH, PriceDisplay, 225, SpringLayout.NORTH, cont);


        layout.putConstraint(SpringLayout.WEST, Upgrade, 10, SpringLayout.WEST, cont);
        layout.putConstraint(SpringLayout.NORTH, Upgrade, 220, SpringLayout.NORTH, cont);

        

    }
    class ButtonEventListener implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            if (Money >= price) {
                Money -= price;
                earn += 5;
                price *=2;
                label = "Nice";
                label2 = "Price: " + price;
                label3 = "Earn: " + earn;
                MoneyDisplay.setText(label);
                PriceDisplay.setText(label2);
                EarnDisplay.setText(label3);
                return;
            } else {  
                label = "No money";
                MoneyDisplay.setText(label);
                return;
            }
        }
    }

}

1 ответ 1

0

Проверьте это условие:

if (Money >= price)

Если я правильно понял у вас переменная money=0, а price=10.

1
  • У меня есть еще один метод public class Cycle extends Okno { public static void Cycl() { Boolean isTrue = true; while (isTrue) { Money += earn; MoneyDisplay.setText("Money: " + Money); Tread.toThread(); } } } А метод Tread.toTread() - остановка на 2 секунды
    – Konstantin
    14 июн 2021 в 16:47

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.