I have a Jframe that contains a JPanel which has a label that I use as a background image and a second JPanel that I want to add my controls into. I am attempting to add and position labels dynamically. I can add the labels but they all appear at the top of the second JLabel eventhough I have set location to be towards the bottom. Here is my code.
package Game;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Henry Sanders
*/
public class BJTable extends javax.swing.JFrame {
Image img;
/**
* Creates new form BJTable
*/
public BJTable() {
this.setPreferredSize(new Dimension(800, 600)); // **** note change
this.setTitle("Henderson Games - Blackjack");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel TableLayout = new PicturePanel();
Toolkit kit = Toolkit.getDefaultToolkit();
img = kit.getImage("C:\\Users\\Henry Sanders\\Pictures\\cards_jpg\\Blackjack Table.jpg");
img = img.getScaledInstance(785, -1, Image.SCALE_SMOOTH);
//Construct Componts to add to panel
//Construct Labels
JPanel Controls = new JPanel();
Controls.setPreferredSize(new Dimension(800, 600));
Controls.setOpaque(false);
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Chip_$1.jpg")));
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Chip_$5.jpg")));
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Chip_$25.jpg")));
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Chip_$100.jpg")));
jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Chip_$500.jpg")));
//Add Componts to panel
//Add Labels
Controls.add(jLabel4);
jLabel4.setLocation(166, 488);
Controls.add(jLabel5);
Controls.add(jLabel6);
Controls.add(jLabel7);
Controls.add(jLabel8);
//Add JPanels to frame
TableLayout.add(Controls);
add(TableLayout);
pack(); // **** added
setVisible(true);
SetCenter();
initComponents();
}
private void SetCenter() {
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
// Move the window
this.setLocation(x, y);
}
private class PicturePanel extends JPanel {
// **** this method should be paintComponent, not paint
public void paintComponent(Graphics g) {
// **** don't forget to call the super method first
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(800, 600));
setPreferredSize(new java.awt.Dimension(800, 600));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 800, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 600, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BJTable.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BJTable.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BJTable.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BJTable.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BJTable().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
I have tried for several days to get this going so any and all help is appreciated.