All pastes #208568 Raw Edit

malmat

public text v1 · immutable
#208568 ·published 2006-10-18 18:24 UTC
rendered paste body
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Memory extends JFrame implements ActionListener {
	private JPanel plan = new JPanel();
	private JPanel meny = new JPanel();
	private JButton[] b = new JButton[2];
	private String[] m = {"Nytt spel", "Avsluta"};
	private JPanel spelare = new JPanel();
	private JButton[] sp = new JButton[2];
	private String[] poang = {"Spelare 1", "Spelare 2"};
	private String[] k;
	private int rader;
	private int kolumner;
	private int n = rader*kolumner;
	
	// metoder
	public void nyttSpel() {
		
		try {
        	rader = Integer.parseInt(JOptionPane.showInputDialog(null, "Hur mÂnga rader vill du ha?"));
        	kolumner = Integer.parseInt(JOptionPane.showInputDialog(null, "Hur mÂnga kolumner vill du ha?"));
		}
		catch(NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "Ogiltigt värde");
			System.exit(0);
			return;
		}
		
		if((rader*kolumner) > 53 || (rader*kolumner) < 4) {
			 JOptionPane.showMessageDialog(null, "Felaktigt antal kort!");
		}
		File bildmapp = new File("bildmapp");
                File[] bilder = bildmapp.listFiles();
                String[] k = new String[bilder.length];
		for(int i = 0; i < bilder.length; i++) {
			k[i] = bilder[i].getPath();
		}
		Kort[] antKort = new Kort[n];
		for(int i = 0; i < n; i += 2) {
			antKort[i] = new Kort(new ImageIcon(k[i]));
			antKort[i + 1] = antKort[i];
		}
		Util.slumpOrdning(antKort);
		
		for(int i = 0; i<antKort.length; i++) {
			plan.add(antKort[i]);
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == b[0])
			new Memory();
		else if(e.getSource() == b[1])
			System.exit(0);		
	}
	
	// konstuktor
	public Memory() {
		nyttSpel();
		
		setTitle("Memory");
		
		
		
			
		meny.setLayout(new FlowLayout());
		spelare.setLayout(new GridLayout(2,1));
		plan.setLayout(new GridLayout(rader, kolumner));
		for(int i =0; i<b.length; i++) {
			b[i] = new JButton();
			b[i].setText(m[i]);
			b[i].addActionListener(this);
			meny.add(b[i]);
		}	
		for(int i =0; i<sp.length; i++) {
			sp[i] = new JButton();
			sp[i].setText(poang[i]);
			sp[i].addActionListener(this);
			spelare.add(sp[i]);
		}
		
		
		add(meny, BorderLayout.SOUTH);
		add(spelare, BorderLayout.WEST);
		add(plan, BorderLayout.CENTER);
		
		pack();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	// main
	public static void main (String[] arg) {
	new Memory();
	}
}