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'm not quite sure i understand the problem.
I don't understand what components your trying position. The labels? The cards? I can't even see the positioning for cards.

Anyway is this was me i would do this using the Java 2D API, and just draw rectangles, fancy effects and boxes and text using x,y cords instead of using gui controls.

But if you want to do it this way, and you want absolute control without no funky layout manager stuff use absolute positioning, although it looses resolution independence since you moving everything with x,y cords. Not the best practice although.

http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html

Layout managers are a pain to get right.
 
Last edited:
I agree that layout managers are a pain but probs are the best bet because absolute position really will look a bit rubbish when you can scale anything up or down.

Might I recommend netbeans IDE for making GUI. If you use it properly it can make GUI design a fair bit easier.
 
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. :)
 
I agree with mattgaunt - when doing anything with a null layout manager you'll want to be using some sort of visual editor - either Netbeans as suggested or the visual editor addon for Eclipse (the latter can be a bit of ass though)
 
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