Java Swing.. surely it can't be this difficult?

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
Just messing around with Swing trying to render a playing card, but I can't find a way to position my Components.

I've already done this with CSS on PHP and Smalltalk, so the logic of placing what where is no problemo.. it's just getting them to actually place where I want is the problem.

I have a JFrame.. (this will look hideous at the mo.. refactoring to come :p)
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Cards extends JFrame {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Cards();
			}
		});
	}

	public Cards() {
		super("Cards");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(600, 480));
		JPanel bg = backgroundFrame();
		bg.add(new Card(Suit.Diamonds, 13));
		getContentPane().add(bg);
		pack();
		setLocation();
		setVisible(true);
	}

	public void setLocation() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getPreferredSize();
		screenSize.height = screenSize.height / 2;
		screenSize.width = screenSize.width / 2;
		frameSize.height = frameSize.height / 2;
		frameSize.width = frameSize.width / 2;
		int x = screenSize.width - frameSize.width;
		int y = screenSize.height - frameSize.height;
		setLocation(x, y);
	}

	public JPanel backgroundFrame() {
		JPanel bg = new JPanel();
		bg.setBackground(new Color(0, 150, 0));
		bg.setBorder(BorderFactory.createLineBorder(new Color(100, 50, 0)));
		return bg;
	}
}
And a JPanel for my card, with JLabels for displaying suit/rank..
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Card extends JPanel {

	private static final long serialVersionUID = 1L;

	public int rank;
	public Suit suit;

	public Card(Suit suit, int rank) {
		super();
		this.rank = rank;
		this.suit = suit;
		setPreferredSize(new Dimension(100, 150));
		setBackground(Color.white);
		add(topRankSuit());
		add(bottomRankSuit());
		doLayout();
	}
	
	public JLabel topRankSuit() {
		JLabel rankSuit = newLabel("topRankSuit", toString());
		rankSuit.setLocation(0, 0);
		return rankSuit;
	}
	
	public JLabel bottomRankSuit() {
		JLabel rankSuit = newLabel("bottomRankSuit", toString());
		rankSuit.setLocation(70, 120);
		return rankSuit;
	}
	
	public JLabel newLabel(String name, String content) {
		JLabel newLabel = new JLabel(name, SwingConstants.LEFT);
		newLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
		newLabel.setText(content);
		newLabel.setForeground(suit.getColor());
		newLabel.setBorder(BorderFactory.createEtchedBorder());
		return newLabel;
	}
	
	public String toString() {
		return String.valueOf(rank) + suit;
	}
}

Yet the bounds/location setting seems to not make any diff:



Where have I gone wrong? :)

P.S. Suit is just an enum.
 
Last edited:
I'll look into implementing my own LayoutManager, ta. :)

This is more an experiment with Swing than anything. I will be positioning the cards (Panels) as well as the Labels, I was just starting with the labels.

What I'd like to achieve is to relatively position the cards, but absolutely position the labels within them.
 
Getting there now.. the Card has setLayout(null) and the labels use setLocation(); the content pane still using FlowLayout. :)
 
Got the nack of this now I think :p



Need to see if I can get some anti-aliasing done, the spades look like someone took a bite out of them :p
 
Back
Top Bottom