2016-12-26, 23:44
  #1
Medlem
min JLabel text flyttar sig inte efter att jag angett koordinater?
den hamnar i toppen av rutan, varfr?


package ssp;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.Color;
import java.awt.FlowLayout;

public class StenSaxPseTest implements ActionListener{

private JLabel L1; /* It's a good practice to make your variables private or protected */
private JLabel L2;
private JLabel L3;
private JLabel L4;
private JPanel p1;
private JPanel p2;
private String s1;
private String s2;
private String s3;
private JButton j4;
private ActionListener anotherListener;

public StenSaxPseTest(){



JButton j1=new JButton("STEN");
j1.setActionCommand("Du har valt sten" );

j1.setBounds(0, 180, 100, 100);
j1.addActionListener(this );


JButton j2=new JButton("SAX");
j2.setActionCommand("Du har valt sax");
j2.setBounds(100,180, 100, 100);
j2.addActionListener(this );


JButton j3=new JButton("PSE");
j3.setActionCommand("Du har valt pse");
j3.setBounds(200, 180, 100, 100);;
j3.addActionListener(this );

JButton j4=new JButton("Nytt Spel");
j4.setActionCommand("Rematch");
j4.setBounds(200, 200, 200, 100);;
j4.addActionListener(this );

JButton j5=new JButton("Avsluta");
j5.setActionCommand("Stng");
j5.setBounds(200, 200, 200, 100);;
j5.addActionListener(Close -> System.exit(0) );/*Behvs bara denna raden fr att
gra knappen till en stng! */

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setBounds(0, 180, 310, 100);
p2.setBounds(0, 0, 300, 190);

L1=new JLabel("Vlj vapen!");
L1.setBounds(50,50,100,50);
L2=new JLabel("Vlj vapen!");
L2.setBounds(100,50,100,50);
L3=new JLabel("Spelare!");
L3.setLocation(30,100);
L4=new JLabel("Dator!");
L4.setBounds(100,150,100,50);


p1.add(j1);
p1.add(j2);
p1.add(j3);
p1.add(j4);
p1.add(j5);
p2.add(L1);
p2.add(L2);
p2.add(L3);
p2.add(L4);
p1.setBackground(Color.BLACK);
p2.setBackground(Color.RED);

JFrame f = new JFrame("StenSaxPse Death battle -.- !!!");




f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE);
// f.setResizable(false);
f.setResizable(true);
f.setSize(315,310);
f.setVisible(true);

f.add(p1);
f.add(p2);
}
// j1.addActionListener(new ActionListener() {







public void actionPerformed(ActionEvent Ae){
Random rand = new Random();
L1.setText(Ae.getActionCommand());
String s1 = new String("Datorn har valt sten");
String s2 = new String("Datorn har valt sax");
String s3 = new String("Datorn har valt pse");
int n = rand.nextInt(3) + 1; // 3an r vrt max och 1an vrt min vrde!!
if(n==1){

// j1.setActionCommand("Du har valt sten" );

L2.setText(s1);
}
if(n==2){
L2.setText(s2);
}
if(n==3){
L2.setText(s3);
}

}
public void action2(ActionEvent Rema){
}



public static void main(String args[]){
StenSaxPseTest stp = new StenSaxPseTest();
//stp.data();

}
}
Citera
2016-12-27, 01:24
  #2
Medlem
Sitkins avatar
Vilken label syftar du p?
http://imgur.com/a/xetMb

Nu ser jag att du har inte valt ngon layout. Detta gr att alla komponenter lgger sig i en och samma rad. Mer om Layouthantering hittar du hr: https://docs.oracle.com/javase/tutor...ut/visual.html

EDIT: Jag rekommenderar att du anvnder ngon GUI-builder istllet fr att manuellt sitta och koda interfacet utan att riktigt se resultatet direkt p skrmen.

Beroende p vilken IDE du anvnder finns det olika GUI-builder man kan anvnda sig utav.
Eclipse: WindowBuilder. Har sjlv anvnt mig utav det.
NetBeans: Tror NetBeans har en GUI-builer inbyggd i IDEn men jag r inte helt sker.
IntellijIdea: Inbyggd GUI-builder s du behver inte installera ngon plugin.


Jag rekommenderar att du frst och frmst lgger fokus p problemlsningen och att lra dig programmera istllet fr att lgga stor vikt p hur man manuellt bygger upp ett interface.
Iofs kanske uppgiften gr ut p att skapa en GUI "manuellt" utan att anvnda drag & drop.
__________________
Senast redigerad av Sitkin 2016-12-27 kl. 01:39.
Citera
2016-12-27, 21:03
  #3
Medlem
Hr r en LayoutManager jag har byggt fr just absoluta positioner:

Du kan anvnda den hur du vill. Skippa licenserna om du vill det. Den r egentligen LGPL. Koden kan hittas hr: https://github.com/macroing/CIT-Java...uteLayout.java

Kod:
/**
 * Copyright 2009 - 2016 Jrgen Lundgren
 * 
 * This file is part of org.macroing.cit.java.
 * 
 * org.macroing.cit.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * org.macroing.cit.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with org.macroing.cit.java. If not, see <http://www.gnu.org/licenses/>.
 */
package org.macroing.cit.java.awt;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.Serializable;

/**
 * An {@code AbsoluteLayout} lays out its {@code Component}s using their locations. But it can also take into account their preferred sizes. Only visible {@code Component}s will be taken into account.
 * <p>
 * Using this {@code LayoutManager}, you don't have to call {@code setLayout(null)} on a given {@code Container} instance. Not only is that discouraged, but it may also cause unintended side-effects.
 * 
 * @since 1.0.0
 * @author Jrgen Lundgren
 */
public final class AbsoluteLayout implements LayoutManager, Serializable {
	private static final long serialVersionUID = 1L;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private boolean isUsingPreferredSize;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Constructs a new {@code AbsoluteLayout}.
	 * <p>
	 * Calling this constructor is equivalent to calling {@code new AbsoluteLayout(true)}.
	 */
	public AbsoluteLayout() {
		this(true);
	}
	
	/**
	 * Constructs a new {@code AbsoluteLayout}.
	 * <p>
	 * If {@code isUsingPreferredSize} is {@code true}, this {@code AbsoluteLayout} instance will take the preferred size into account when laying out its {@code Component}s.
	 * 
	 * @param isUsingPreferredSize {@code true} if, and only if, this {@code AbsoluteLayout} instance should take the preferred size into account when laying out its {@code Component}s
	 */
	public AbsoluteLayout(final boolean isUsingPreferredSize) {
		setUsingPreferredSize(isUsingPreferredSize);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Returns {@code true} if, and only if, this {@code AbsoluteLayout} instance will take the preferred size into account when laying out its {@code Component}s, {@code false} otherwise.
	 * 
	 * @return {@code true} if, and only if, this {@code AbsoluteLayout} instance will take the preferred size into account when laying out its {@code Component}s, {@code false} otherwise
	 */
	public boolean isUsingPreferredSize() {
		return this.isUsingPreferredSize;
	}
	
	/**
	 * Calculates the minimum size for the specified {@code Container}, given the {@code Component}s it contains.
	 * <p>
	 * At this time, the minimum size is the same as the preferred size. So this method simply delegates to {@code preferredLayoutSize(Container)}.
	 * 
	 * @param parent the {@code Container} to be laid out
	 */
	@Override
	public Dimension minimumLayoutSize(final Container parent) {
		synchronized(parent.getTreeLock()) {
			return preferredLayoutSize(parent);
		}
	}
	
	/**
	 * Calculates the preferred size for the specified {@code Container}, given the {@code Component}s it contains.
	 * 
	 * @param parent the {@code Container} to be laid out
	 */
	@Override
	public Dimension preferredLayoutSize(final Container parent) {
		final Insets parentInsets = parent.getInsets();
		
		int x = parentInsets.left;
		int y = parentInsets.top;
		int width = 0;
		int height = 0;
		
		synchronized(parent.getTreeLock()) {
			for(final Component component : parent.getComponents()) {
				if(component.isVisible()) {
					final Point location = component.getLocation();
					
					final Dimension componentSize = doGetComponentSize(component);
					
					x = Math.min(x, location.x);
					y = Math.min(y, location.y);
					width = Math.max(width, location.x + componentSize.width);
					height = Math.max(height, location.y + componentSize.height);
				}
			}
		}
		
		if(x < parentInsets.left) {
			width += parentInsets.left - x;
		}
		
		if(y < parentInsets.top) {
			height += parentInsets.top - y;
		}
		
		width += parentInsets.right;
		height += parentInsets.bottom;
		
		return new Dimension(width, height);
	}
	
	/**
	 * Returns a {@code String} representation of this {@code AbsoluteLayout} instance.
	 * 
	 * @return a {@code String} representation of this {@code AbsoluteLayout} instance
	 */
	@Override
	public String toString() {
		return String.format("[%s]", getClass().getName());
	}
	
	/**
	 * This method is not supported by this class. So calling it will do nothing.
	 * 
	 * @param name the name to be associated with {@code component}
	 * @param component the {@code Component} to be added
	 */
	@Override
	public void addLayoutComponent(final String name, final Component component) {
//		Do nothing.
	}
	
	/**
	 * Lays out the specified {@code Container}.
	 * 
	 * @param parent the {@code Container} to be laid out
	 */
	@Override
	public void layoutContainer(final Container parent) {
		synchronized(parent.getTreeLock()) {
			final Insets parentInsets = parent.getInsets();
			
			int x = parentInsets.left;
			int y = parentInsets.top;
			
			for(final Component component : parent.getComponents()) {
				if(component.isVisible()) {
					final Point location = component.getLocation();
					
					x = Math.min(x, location.x);
					y = Math.min(y, location.y);
				}
			}
			
			x = x < parentInsets.left ? parentInsets.left - x : 0;
			y = y < parentInsets.top ? parentInsets.top - y : 0;
			
			for(final Component component : parent.getComponents()) {
				if(component.isVisible()) {
					final Point location = component.getLocation();
					
					final Dimension componentSize = doGetComponentSize(component);
					
					component.setBounds(location.x + x, location.y + y, componentSize.width, componentSize.height);
				}
			}
		}
	}
	
	/**
	 * This method is not supported by this class. So calling it will do nothing.
	 * 
	 * @param component the {@code Component} to be removed
	 */
	@Override
	public void removeLayoutComponent(final Component component) {
//		Do nothing.
	}
	
	/**
	 * Sets the preferred size usage for this {@code AbsoluteLayout}.
	 * <p>
	 * If {@code true} is specified, this {@code AbsoluteLayout} instance will take the preferred size into account when laying out its {@code Component}s.
	 * 
	 * @param isUsingPreferredSize {@code true} if, and only if, this {@code AbsoluteLayout} instance should take the preferred size into account when laying out its {@code Component}s
	 */
	public void setUsingPreferredSize(final boolean isUsingPreferredSize) {
		this.isUsingPreferredSize = isUsingPreferredSize;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	private Dimension doGetComponentSize(final Component component) {
		final Dimension preferredSize = component.getPreferredSize();
		final Dimension size = component.getSize();
		
		return !this.isUsingPreferredSize && size.width > 0 && size.height > 0 ? size : preferredSize;
	}
}
__________________
Senast redigerad av 0xCAFEBABE 2016-12-27 kl. 21:06.
Citera

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in