Hi, I'm taking a computer science course in school, and for a project I need to write a game, and in my case blackjack.
I've only just started learning how to write in GUI, and I'm really just a beginner. Anyways my problem is that whenever I want to run my program as an application (be it in Eclipse or Dr. Java), it takes anywhere from 2-5 minutes to load, and during this time my computer lags to hell.
Also I noted that in task manager I have around 10 javaw.exe tasks running, although I have 8GBs of Ram.
I'm using a visual editor at the moment to write my GUI code, since my teacher didn't exactly 'teach' how to do this kind of code, and I'm learning it through trial and error. Anyways, here's the code, and if anyone can help me and tell me where the problem lies, I'd really appreciate it.
Thanks
import javax.swing.JFrame; import javax.swing.JPanel; //import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JButton; import java.awt.Rectangle; import java.awt.Font; import javax.swing.JLabel; import java.util.Random; public class Blackjack { int card = 0; private JFrame jFrame = null; private JPanel jContentPane = null; private JButton jButton = null; private JButton jButton1 = null; private JLabel jLabel = null; /** * This method initializes jFrame * * @return javax.swing.JFrame */ private JFrame getJFrame() { if (jFrame == null) { jFrame = new JFrame(); jFrame.setSize(new Dimension(593, 429)); jFrame.setTitle("Blackjack"); jFrame.setVisible(true); jFrame.setResizable(false); jFrame.setFont(new Font("Calibri", Font.PLAIN, 12)); jFrame.setContentPane(getJContentPane()); } return jFrame; } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jLabel = new JLabel(); jLabel.setBounds(new Rectangle(150, 75, 181, 31)); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJButton(), null); jContentPane.add(getJButton1(), null); jContentPane.add(jLabel, null); } return jContentPane; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new Rectangle(45, 345, 75, 28)); jButton.setFont(new Font("Calibri", Font.BOLD, 12)); jButton.setText("Hit Me"); jButton.addActionListener(new java.awt.event.ActionListener() { /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(java.awt.event.ActionEvent e) { } }); } return jButton; } /** * This method initializes jButton1 * * @return javax.swing.JButton */ private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setBounds(new Rectangle(135, 345, 71, 28)); jButton1.setFont(new Font("Calibri", Font.BOLD, 12)); jButton1.setText("Deal"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { deal(); } }); } return jButton1; } public int deal() { Random dealCard = new Random(); card = (dealCard.nextInt(10)+ 1); jLabel.setText("Your card total: " + card); return card; } private static void runGUI() { //JFrame.setDefaultLookAndFeelDecorated(true); Blackjack greeting = new Blackjack(); greeting.getJFrame(); } /** * @param args */ public static void main(String[] args) { runGUI(); } }
PS at the moment this program really does nothing, as I'm only trying to figure out how to get the buttons to work, and make my blackjack game.
Any tips on how to get this done would also be appreciated!
Thanks in advance.