Java Practice App Example

playerHandPanel.java

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JButton;  // Temporary, to be replaced with card image JLabels
 
public class playerHandPanel extends JPanel
{
   private JButton cards[];
   private final String card_names[] =
      { "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH", "AS" };
 
   public playerHandPanel()
   {
      super();
      FlowLayout layout = new FlowLayout();
      layout.setAlignment( FlowLayout.LEFT );
      setLayout(layout);
      setBackground(new Color(0, 160, 0));
 
      cards = new JButton[ card+names.length ];
 
      for ( int i=0; i < card_names.length; i++ )
      {
         cards[i] = new JButton(card_names[i]);
         cards[i].setBackground(Color.white);
         add(cards[i]);
      }
   }
}

practiceFrame.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
 
public class practiceFrame extends JFrame
{
   private playerHandPanel playerCards;
   private JScrollPane scrollPane;
 
   public practiceFrame()
   {
      super( "The frame is working" );
      Container me = getContentPane();
      me.setLayout( new BorderLayout() );
 
      playerCards = new playerHandPanel();
 
      scrollPane = new JScrollPane(playerCards);
      scrollPane.getViewport().setBackground( new Color(0, 160, 0) );
      Dimension newPreferredSize = new Dimension( scrollPane.getPreferredSize() );
      newPreferredSize.height += 60;  // Customize the size
      scrollPane.setPreferredSize( newPreferredSize );
      me.add( scrollPane, BorderLayout.SOUTH );
   }
}

practiceApp.java

import java.awt.Color;
import javax.swing.JFrame; // Needed for EXIT_ON_CLOSE
 
public class practiceApp
{
   public static void main( String args[] )
   {
      practiceFrame mainwin = new practiceFrame();
      mainwin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      mainwin.setSize(800, 640);
      mainwin.getContentPane().setBackground( new Color(0, 160, 0) );
      mainwin.setVisible(true);
   }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License