I have this blackjack layout and every panel and textfield already on the box except I don't know how to properly alligned them. I've been reading about layout manager but I get confused. I hope anybody can help me.
package BlackJack; /*Program Name: BlackJackGUI.java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class BlackJackGUI extends JPanel { //declare all variables private ImageIcon image; private JLabel Photo; private JLabel betsLabel; private JTextField betsField; private JLabel trackerLabel; private JLabel winLabel; private JTextField winField; private JLabel loseLabel; private JTextField loseField; private JLabel winloseLabel; private JTextField winloseField; private JLabel playerLabel; private JTextField playerField; private JTextArea playerArea; private JLabel dealerLabel; private JTextField dealerField; private JTextArea dealerArea; private JButton dealButton; private JButton hitButton; private JButton stayButton; private JButton resetButton; private JButton highButton; private JButton lowButton; //private JFrame frame; //contructs the screen public BlackJackGUI() { //create layout manager setBackground(new Color(0,122,0)); setPreferredSize(new Dimension(700, 400)); //create panel components betsLabel = new JLabel ("Bets"); betsField = new JTextField(5); trackerLabel = new JLabel ("TRACKER"); winLabel = new JLabel ("Win"); winField = new JTextField(5); loseLabel = new JLabel ("lose"); loseField = new JTextField(5); playerField = new JTextField(10); playerArea = new JTextArea(5,10); playerLabel = new JLabel("Player"); dealerField = new JTextField(10); dealerArea = new JTextArea(5,10); dealerLabel = new JLabel("Dealer"); winloseField = new JTextField(10); winloseLabel = new JLabel("Win or Lose"); //components/objects dealButton = new JButton("DEAL"); hitButton = new JButton("Hit"); stayButton = new JButton("Stay"); resetButton = new JButton("Reset"); highButton = new JButton("Ace High"); lowButton = new JButton("Ace Low"); //add components to the panel (left side) add(betsLabel); add(betsField); add(trackerLabel); add(winLabel); add(winField); add(loseLabel); add(loseField); add(resetButton); //add compnents to the panel (player and dealer) add(playerLabel); add(playerArea); add(dealButton); add(hitButton); add(stayButton); add(highButton); add(lowButton); add(dealerLabel); add(dealerArea); //add components to the panel (bottom side) add(winloseLabel); add(winloseField); //connect event handler to event source dealButton.addActionListener(new ButtonListener()); hitButton.addActionListener(new ButtonListener()); stayButton.addActionListener(new ButtonListener()); //add(Photo,BorderLayout.WEST); image = new ImageIcon("src/BlackJack/blackjack60.jpg"); Photo = new JLabel (image); add(Photo); } //display the screen public void display() { JFrame myFrame = new JFrame("BlackJack"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setContentPane(this); myFrame.pack(); myFrame.setVisible(true); } private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { double amt = Double.parseDouble(winloseField.getText()); } } }
and here is my Panel so I can run it.
package BlackJack; import javax.swing.JFrame; public class Cards { public static void main(String[] args) { //Create frame and panel JFrame frame = new JFrame("BlackJack"); BlackJackGUI panel = new BlackJackGUI(); //set frame behavior frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //place panel in frame frame.getContentPane().add(panel); //format frame and make available/visible frame.pack(); frame.setVisible(true); } }
blackjackGUI.jpg