Hi, I'm really, really new to Java, but I've created a game, and in a separate file the GUI, but I don't know how to attach the functions of the program to the GUI.

This is my program:

import java .util.*;

class bust {
  
  public static void main (String args[]){
    int userChoice;
    int computerChoice;
    int result;
    int total;
    
    System.out.println("This is a game of twenty. The first player to reach 20 wins.");
    
    total = 0;
    result = 0;

    
    while (total <21){
      
      userChoice = getUserChoice();
      computerChoice = getComputerChoice();
      total = total + userChoice;
      
      if (total >=21){
        System.out.println("You win!");
        break;
      }
      total = total + computerChoice;
      System.out.println("The total is: " + total);
      
      if (total >=21){
        System.out.println("The computer wins.");
        break;
        
      }
    }
  }
  
  public static int getUserChoice() {
    
    System.out.println("Please enter either 1 or 2.");
    
    int userChoice = Integer.parseInt(get.input());
    
    return userChoice;
    
  }
  
  public static int getComputerChoice() {
    
    Random rand = new Random();
    
    int computerChoice = rand.nextInt(2) + 1;
    
    if (computerChoice ==1) {
      System.out.println("The computer chose 1.");
    }
    else {
      System.out.println("The computer chose 2.");
    }
    
    return computerChoice;
  }
}

and this is my GUI:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class bustGUI extends JFrame implements ActionListener {
  JLabel heading;
  JLabel Select;
  JLabel computerChoice;
  JLabel result;
  JLabel total;
  JLabel or;
  JTextField firstTextField;
  JTextField secondTextField;
  JTextField resultTextField;
  JButton oneButton;
  JButton twoButton;
  
  
  
  public bustGUI() {
    heading = new JLabel("                  Bust                  ");
    or = new JLabel(" or ");
    or.setFont(new Font("Tahoma", Font.PLAIN, 13));
    heading.setFont(new Font("Papyrus", Font.BOLD, 18));
    Select = new JLabel("         Select: ");
    Select.setFont(new Font("Tahoma", Font.PLAIN, 13));
    computerChoice = new JLabel("Computer Choice: ");
    computerChoice.setFont(new Font("Tahoma", Font.PLAIN, 13));
    result = new JLabel("                 Result");
    result.setFont(new Font("Tahoma", Font.PLAIN, 13));
    firstTextField = new JTextField(10);
    resultTextField = new JTextField(10);
    oneButton = new JButton(new ImageIcon("number1.jpg"));
    oneButton.setPreferredSize(new Dimension (35, 35));
    twoButton = new JButton(new ImageIcon("number2.jpg"));
    twoButton.setPreferredSize(new Dimension (35, 35));
    total = new JLabel("                 Total: ");
    total.setFont(new Font("Tahoma", Font.PLAIN, 13));
    secondTextField = new JTextField(10);
    
    getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));
    getContentPane().add(heading);
    getContentPane().add(Select);
    getContentPane().add(oneButton);
    getContentPane().add(or);
    getContentPane().add(twoButton);
    getContentPane().add(computerChoice);
    getContentPane().add(firstTextField);
    getContentPane().add(total);
    getContentPane().add(secondTextField);
    getContentPane().add(result);
    getContentPane().add(resultTextField);
    
    oneButton.addActionListener(this);
    twoButton.addActionListener(this);
    
  }
  
  public static void main(String args[]) {
    bustGUI 
      mywindow = new bustGUI();
    mywindow.setSize(250,250);
    mywindow.setTitle("Bust");
    mywindow.setVisible(true);
    
  }
  public void actionPerformed(ActionEvent evt) {

    }
 }

You can't have a main functions in both classes if you intend to make a single program.

I would change the main function in your bust class into a constructor:

public bust() {
...
}

and create an instance of bust in bustGUI's and define it in its constructor:

bust bust;
public bustGUI() {
this.bust = new bust();
.....
}

and then, when you want to call a function in bust, you would do bust.function();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.