Problems with GUI.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 1
Reputation: violet_blossom is an unknown quantity at this point 
Solved Threads: 0
violet_blossom violet_blossom is offline Offline
Newbie Poster

Problems with GUI.

 
0
  #1
Aug 12th, 2009
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:

  1. import java .util.*;
  2.  
  3. class bust {
  4.  
  5. public static void main (String args[]){
  6. int userChoice;
  7. int computerChoice;
  8. int result;
  9. int total;
  10.  
  11. System.out.println("This is a game of twenty. The first player to reach 20 wins.");
  12.  
  13. total = 0;
  14. result = 0;
  15.  
  16.  
  17. while (total <21){
  18.  
  19. userChoice = getUserChoice();
  20. computerChoice = getComputerChoice();
  21. total = total + userChoice;
  22.  
  23. if (total >=21){
  24. System.out.println("You win!");
  25. break;
  26. }
  27. total = total + computerChoice;
  28. System.out.println("The total is: " + total);
  29.  
  30. if (total >=21){
  31. System.out.println("The computer wins.");
  32. break;
  33.  
  34. }
  35. }
  36. }
  37.  
  38. public static int getUserChoice() {
  39.  
  40. System.out.println("Please enter either 1 or 2.");
  41.  
  42. int userChoice = Integer.parseInt(get.input());
  43.  
  44. return userChoice;
  45.  
  46. }
  47.  
  48. public static int getComputerChoice() {
  49.  
  50. Random rand = new Random();
  51.  
  52. int computerChoice = rand.nextInt(2) + 1;
  53.  
  54. if (computerChoice ==1) {
  55. System.out.println("The computer chose 1.");
  56. }
  57. else {
  58. System.out.println("The computer chose 2.");
  59. }
  60.  
  61. return computerChoice;
  62. }
  63. }

and this is my GUI:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class bustGUI extends JFrame implements ActionListener {
  6. JLabel heading;
  7. JLabel Select;
  8. JLabel computerChoice;
  9. JLabel result;
  10. JLabel total;
  11. JLabel or;
  12. JTextField firstTextField;
  13. JTextField secondTextField;
  14. JTextField resultTextField;
  15. JButton oneButton;
  16. JButton twoButton;
  17.  
  18.  
  19.  
  20. public bustGUI() {
  21. heading = new JLabel(" Bust ");
  22. or = new JLabel(" or ");
  23. or.setFont(new Font("Tahoma", Font.PLAIN, 13));
  24. heading.setFont(new Font("Papyrus", Font.BOLD, 18));
  25. Select = new JLabel(" Select: ");
  26. Select.setFont(new Font("Tahoma", Font.PLAIN, 13));
  27. computerChoice = new JLabel("Computer Choice: ");
  28. computerChoice.setFont(new Font("Tahoma", Font.PLAIN, 13));
  29. result = new JLabel(" Result");
  30. result.setFont(new Font("Tahoma", Font.PLAIN, 13));
  31. firstTextField = new JTextField(10);
  32. resultTextField = new JTextField(10);
  33. oneButton = new JButton(new ImageIcon("number1.jpg"));
  34. oneButton.setPreferredSize(new Dimension (35, 35));
  35. twoButton = new JButton(new ImageIcon("number2.jpg"));
  36. twoButton.setPreferredSize(new Dimension (35, 35));
  37. total = new JLabel(" Total: ");
  38. total.setFont(new Font("Tahoma", Font.PLAIN, 13));
  39. secondTextField = new JTextField(10);
  40.  
  41. getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));
  42. getContentPane().add(heading);
  43. getContentPane().add(Select);
  44. getContentPane().add(oneButton);
  45. getContentPane().add(or);
  46. getContentPane().add(twoButton);
  47. getContentPane().add(computerChoice);
  48. getContentPane().add(firstTextField);
  49. getContentPane().add(total);
  50. getContentPane().add(secondTextField);
  51. getContentPane().add(result);
  52. getContentPane().add(resultTextField);
  53.  
  54. oneButton.addActionListener(this);
  55. twoButton.addActionListener(this);
  56.  
  57. }
  58.  
  59. public static void main(String args[]) {
  60. bustGUI
  61. mywindow = new bustGUI();
  62. mywindow.setSize(250,250);
  63. mywindow.setTitle("Bust");
  64. mywindow.setVisible(true);
  65.  
  66. }
  67. public void actionPerformed(ActionEvent evt) {
  68.  
  69. }
  70. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: Problems with GUI.

 
0
  #2
Aug 12th, 2009
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();
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC