Java Menü-Problem

AmsananKING

Mitglied
Ich arbeite schon seit längerem an verschiedenen Problemen:
Die erste ist die, dass ich die Labels auf einer bestimmten Art und Weise anordnen will so dass sie immer den selben Abstand untereinander haben .
Zweitens sollen die Labels im Panel sichtbar sein.(Problem: Anmeldung.class)
Das letzte Problem ist eher, dass ich in der Klasse Gui über einen Knopfdruck auf die Klasse Anmeldung springen kann, bzw das JFrame vom panel Menü auf das panel Anmeldung wechselt.
Ich hoffe ihr könnt mir helfen, denn alleine finde ich leider keine effiziente Lösung.
P.S: Ich arbeite leider mit BlueJ weshalb ich keinen Window-Builder zu Verfügung habe.
Mit freundlichen Grüßen Amsanan
[CODE lang="java" title="Anmeldung.class"]

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Anmeldung extends JPanel
{ String[] labelnamen ={"Vorname","Nachname","Gamenr"};
Referenz ref= new Referenz();
public Anmeldung(int pWidth,int pHeight){

int width =pWidth;
int height=pHeight;
setSize(new Dimension(width,height));
setLayout(null);
JLabel label =new JLabel("Hallo");
label.setBorder(BorderFactory.createLineBorder(Color.black));

setBackground(Color.BLUE);

add(label);
setVisible(true);
}

private JLabel[] labels(){

JLabel[] labels =new JLabel[labelnamen.length];
for(int i=0;i<labels.length;i++){
labels=temp2(labelnamen,20,20);

}

return labels;
}

private JLabel[] LabelOrdnen(){
JLabel[] temp =labels();
for(int i=0;i<temp.length;i++)
{
if(temp.getName().contains("Vorname")){
temp.setLocation(100,10);
}else if(temp.getName().contains("Nachname")){
temp.setLocation(300,10);
} else if(temp.getName().contains("Gamenr")){
temp.setLocation(ref.getWidth()/2,ref.getHeight()/2);
}
System.out.println(temp.getName()+","
+temp.getLocation());
}
return temp;
}

private JLabel temp2(String pName,int pX,int pY){
JLabel temp =new JLabel();
temp.setName(pName);
Font font =new Font("Arial",Font.PLAIN,30);
temp.setFont(font);
temp.setLocation(pX,pY);
return temp;
}
}
[/CODE]
[CODE lang="java" title="Gui.class und subclass menue"]import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
public class Gui extends JFrame implements ActionListener
{ Referenz ref =new Referenz();
Menue menue =new Menue(ref.getWidth(),ref.getHeight());
public Gui(){

setTitle("Pong");
setSize(ref.getWidth(),ref.getHeight());
setLayout(new CardLayout());
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setVisible(true);
add(menue);
}

public void actionPerformed(ActionEvent e){
if(e.getSource().toString().contains("Play"))
{System.out.println("Play");
remove(menue);
add(new Anmeldung(ref.getWidth(),ref.getHeight()));
}
if(e.getSource().toString().contains("Exit"))
{System.out.println("Exit");
System.exit(0);}
if(e.getSource().toString().contains("Options"))
{System.out.println("Options");}
}
private class Menue extends JPanel {
private JButton[] buttons;

public Menue(int pWidth,int pHeight){

setLayout(null);
int width =pWidth;
int height =pHeight;
setBounds(0,0,width,height);
setBackground(Color.BLACK);
for(int i=0;i<Buttons().length;i++){
add(Buttons());
}
add(getTitle());

}

public JLabel getTitle(){
URL url =this.getClass().getResource("titel.gif");
ImageIcon icon =new ImageIcon(url);

JLabel titel =new JLabel(icon);
titel.setBounds(200,10,400,100);
//titel.setBorder(BorderFactory.createLineBorder(Color.white));

return titel;
}

public JButton[] Buttons(){

JButton Play =new JButton("Play");
JButton Exit = new JButton("Exit");
JButton Options =new JButton("Options");
JButton[] buttons={Play,Options,Exit};

for(int i=0;i<buttons.length;i++){
int y=200;

buttons=button(buttons.getLabel(),325,y,150,50);
buttons.addActionListener(Gui.this);
if(i>0){if(buttons[i-1].getY()==buttons.getY()||buttons.getY()<buttons[i-1].getY())
{buttons.setLocation(buttons[i-1].getX(),buttons[i-1].getY()+100);}}

}
return buttons;
}

public JButton button(String pButtonName,int pX,int pY,int pWidth,int pHeight){
JButton tempbutton =new JButton(pButtonName);
tempbutton.setBounds(pX,pY,pWidth,pHeight);
tempbutton.setBackground(Color.BLUE);
tempbutton.setOpaque(true);
tempbutton.setBorder(BorderFactory.createEmptyBorder());
tempbutton.setFont(new Font("Arial", Font.PLAIN, 30));
return tempbutton;
}

}
}[/CODE]
[CODE lang="java" title="Anmeldungsdiagramm"] Anrede Vorname Nachname Gamenr
Player1 (JCombobox) (JTextField) (JTextField) (JLabel)
Player2 (JCombobox) (JTextField) (JTextField) (JLabel)[/CODE]
 
Zuletzt bearbeitet:
Man kann einen JFrame erzeugen und in ihmmit CardLayout einen Panel erstellen und im Panel andere Panels einsetzen und tauschen.
Meine neue Gui.class
[CODE lang="java" title="Gui.class"]
/**
* Beschreiben Sie hier die Klasse Gui.
*
* @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum)
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
public class Gui extends JFrame implements ActionListener
{ Var var =new Var();
static CardLayout cl=new CardLayout();
Menue menue =new Menue();
Anmeldung anmeld=new Anmeldung();
static JPanel rootPanel=new JPanel();
JButton Play =new JButton();
JButton Options =new JButton();
JButton Exit =new JButton();

public Gui(){
setTitle("Pong");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(var.getWidth(),var.getHeight());
setLocationRelativeTo(null);
add(rootPanel());
setVisible(true);
}

public JPanel rootPanel(){
rootPanel.setPreferredSize(new Dimension(var.getWidth(),
var.getHeight()));
rootPanel.setLayout(cl);
rootPanel.add(menue,"menue");
rootPanel.add(anmeld,"anmeldung");
cl.show(rootPanel,"menue");
return rootPanel;
}

public void actionPerformed(ActionEvent e){
if(e.getSource().toString().contains("Play"))
{System.out.println("Play");
cl.show(rootPanel,"anmeldung");
}
if(e.getSource().toString().contains("Exit"))
{System.out.println("Exit");
System.exit(0);}
if(e.getSource().toString().contains("Options"))
{System.out.println("Options");
}

}

public static void change(JPanel pPanel,String pName){
rootPanel.add(pPanel,pName);
cl.show(rootPanel,pName);
}
private class Menue extends JPanel{

public Menue(){
setSize(var.getWidth(),var.getHeight());
setLayout(null);
setBackground(Color.BLACK);
for(JButton b:buttons()){
add(b);

}
add(titel());
}

public JButton button(String pText,int pX,int pY){
JButton temp =new JButton();
temp.setText(pText);
temp.setBounds(pX,pY,150,40);
Font font =new Font("Tahoma",Font.PLAIN,24);
temp.setFont(font);
return temp;
}

public JButton[] buttons(){
JButton[] buttons;
Play=button("Play",300,200);
Options=button("Options",300,300);
Exit=button("Exit",300,400);
buttons=new JButton[]{Play,Options,Exit};

for(JButton j:buttons){
j.addActionListener(Gui.this);
}
return buttons;
}

private JLabel titel(){
JLabel titel =new JLabel();
URL url =this.getClass().getResource("titel.gif");
ImageIcon icon =new ImageIcon(url);
titel.setIcon(icon);
titel.setBounds(200,10,300,100);
return titel;
}
}
}
[/CODE]
 

Zurück
Oben