| | |
Problems with GUI.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 1
Reputation:
Solved Threads: 0
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:
and this is my GUI:
This is my program:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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) { } }
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
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();
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();
![]() |
Similar Threads
- Problems with GUI package Header compliance (C++)
- jComboBox getSelectedIndex (Java)
- python & guis (Python)
- Threads (Java)
- Ubuntu 64 bits and INTEL processors (Getting Started and Choosing a Distro)
- Too many questions. (Python)
- Help with booting up, Windows 98 and XP issue (Windows NT / 2000 / XP)
- Mandrake 9.2 Screen Res problem (Window and Desktop Managers)
Other Threads in the Java Forum
- Previous Thread: Convert Video into FLV format
- Next Thread: Anyone got any ideas?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui html ide image input integer intersect iphone j2me java java.xls javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





