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]
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: