Ok, also wollte ich ein Box-Layout machen, um es zu testen, und auf der layout help page gibt es an, dass Sie ihm eine Ausrichtung geben können, was ich versuchte, aber es scheint überhaupt nicht zu funktionieren.Boxlayout wird nicht ausgerichtet
package gameflow;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LoginScreen extends JFrame
{
private static final Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize();
private static final double DEFAULTHEIGHT = (SCREENSIZE.getHeight() * 80)/100;
private static final double DEFAULTWIDTH = (SCREENSIZE.getWidth() * 80)/100;
private static final long serialVersionUID = -7245840869407664992L;
private JTextField userfield = new JTextField(), passfield = new JTextField();
private JLabel userlabel = new JLabel("Username"), passlabel = new JLabel("Password");
private static final Dimension fieldSize = new Dimension();
public LoginScreen()
{
super("Wraith: the game");
setBounds((SCREENSIZE.width * 10)/100, (SCREENSIZE.height * 10)/100,(int)DEFAULTWIDTH,(int)DEFAULTHEIGHT);
fieldSize.setSize((double)this.getWidth()/2,(double)this.getHeight()/30);
userfield.setMaximumSize(fieldSize);
userfield.setForeground(new Color(1f,1f,1f));
passfield.setMaximumSize(fieldSize);
passfield.setForeground(new Color(1f,1f,1f));
userlabel.setMaximumSize(fieldSize);
passlabel.setMaximumSize(fieldSize);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setBackground(new Color(0f,0f,0f));
panel.setAlignmentX(0.5f);
panel.add(userlabel);
panel.add(userfield);
panel.add(passlabel);
panel.add(passfield);
this.add(panel, BorderLayout.CENTER);
}
}
wie Sie sehen, Platte eine BoxLayout innerhalb des JFrame hat wem ein Border hat, richten sich die Komponenten werden nicht auf die Mitte. Warum?
wie es jetzt ist, haben alle Komponenten die gleiche linke Kante. Der Aufruf von setAlignment on panel wird die einzelnen Komponenten nicht beeinflussen. – MeBigFatGuy
Nun, wie mache ich das dann? – Delupara
Ich würde SpringLayout über BoxLayout nach Formularen suchen. oder sogar miglayout. https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html – MeBigFatGuy