0

I created my first GUI and I shared it with my friend, but everything is messy on his screen because the GUI is designed for 2k. how do I make it so it works on 2k and HD?

I want it to work on full screen.

If there are non-related problems in the code please tell me, because I'm new to this.

(the class is activated by another class)

package me.NadDeMan.Bank;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;


public class Bank implements ActionListener {
    JFrame frame = new JFrame();
    JLabel moneyLabel = new JLabel();
    JLabel messageLabel = new JLabel();
    JButton Donut = new JButton("Buy a donut for 1.00$");
    JButton Pizza = new JButton("Buy a pizza for 10.00$");
    JButton Cake = new JButton("Buy a cake for 100.00$");
    String usernameMoney;
    double amount = 10000;
    JButton workMin = new JButton("Work 1 minute for 1.00$");
    JButton workHour = new JButton("Work 1 hour for 10.00$");
    JButton workDay = new JButton("Work 1 day for 100.00$");
    Bank(String username){
        usernameMoney = username;
        //frame.setSize(2560,1440);
        workMin.setBounds(400, 300, 200, 100);
        workMin.setFont(new Font(null, Font.BOLD, 15));
        workMin.setFocusable(false);
        workMin.addActionListener(this);

        workHour.setBounds(400, 640, 200, 100);
        workHour.setFont(new Font(null, Font.BOLD, 15));
        workHour.setFocusable(false);
        workHour.addActionListener(this);

        workDay.setBounds(400,980, 200, 100);
        workDay.setFont(new Font(null, Font.BOLD, 15));
        workDay.setFocusable(false);
        workDay.addActionListener(this);

        Donut.setBounds(1950, 300, 200, 100);
        Donut.setFont(new Font(null, Font.BOLD, 15));
        Donut.setFocusable(false);
        Donut.addActionListener(this);

        Pizza.setBounds(1950, 640, 200, 100);
        Pizza.setFont(new Font(null, Font.BOLD, 15));
        Pizza.setFocusable(false);
        Pizza.addActionListener(this);

        Cake.setBounds(1960, 980, 200, 100);
        Cake.setFont(new Font(null,Font.BOLD, 15));
        Cake.setFocusable(false);
        Cake.addActionListener(this);

        moneyLabel.setText("Hello " + username + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
        moneyLabel.setSize( 1000, 50);
        moneyLabel.setFont(new Font("Bison", Font.BOLD, 30));
        moneyLabel.setHorizontalAlignment(JLabel.CENTER);
        moneyLabel.setVerticalAlignment(JLabel.CENTER);
        messageLabel.setHorizontalTextPosition(JLabel.CENTER);
        messageLabel.setSize(1000, 100);
        messageLabel.setLocation(1000, 1000);
       
        messageLabel.setFont(new Font("Bison", Font.ITALIC, 50));
        frame.setTitle("Bank");
        frame.add(messageLabel);
        frame.add(Pizza);
        frame.add(Cake);
        frame.add(Donut);
        frame.add(workMin);
        frame.add(workHour);
        frame.add(workDay);
        frame.add(moneyLabel);
        frame.setLocationRelativeTo(null);
        frame.setSize(1500, 1000);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==workMin){
            amount = amount + 1;
            moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
            messageLabel.setText("You worked 1 minute and got 1.00$!");
            Timer timer = new Timer(5000, event ->{
                messageLabel.setText("");
            });
            timer.start();

        }else if(e.getSource()==workHour){
            amount = amount + 10;
            moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
            messageLabel.setText("You worked 1 hour and got 10.00$!");
            Timer timer = new Timer(5000, event ->{
                messageLabel.setText("");
            });
            timer.start();

        }else if(e.getSource()==workDay){
            amount = amount + 100;
            moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
            messageLabel.setText("You worked 1 day and got 100.00$!");
            Timer timer = new Timer(5000, event ->{
                messageLabel.setText("");
            });
            timer.start();
        } else if(e.getSource()==Donut){
            if(amount<1) {
                messageLabel.setText("You don't have enough money!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }else{
                amount = amount - 1;
                moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
                messageLabel.setText("You spent 1.00$ on a donut!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }
        }else if(e.getSource()==Pizza){
            if(amount<10) {
                messageLabel.setText("You don't have enough money!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }else{
                amount = amount - 10;
                moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
                messageLabel.setText("You spent 10.00$ on a pizza!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }
        }else if(e.getSource()==Cake){
            if(amount<100){
                messageLabel.setText("You don't have enough money!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }else {
                amount = amount - 100;
                moneyLabel.setText("Hello " + usernameMoney + ", you have " + NumberFormat.getCurrencyInstance().format(amount) + "!");
                messageLabel.setText("You spent 100.00$ on a cake!");
                Timer timer = new Timer(5000, event ->{
                    messageLabel.setText("");
                });
                timer.start();
            }

        }
    }
}
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • 9
    You need to use LayoutManager to lay out your components for you instead of hard-coding the sizes. [See the tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html). What you're doing right now is absolute positioning and has exactly the problem you describe: it'll work for one size and that size only. – Joachim Sauer Aug 25 '21 at 10:38

0 Answers0