Java position festlegen

2hawk

Newbie
Registriert
Jan. 2019
Beiträge
1
package Main;
import javax.swing.JFrame;
import javax.swing.JRadioButton;


public class Frame {

public static void main(String[] args) {

JFrame window = new JFrame();
JRadioButton wahl2 = new JRadioButton("hier");
JRadioButton wahl = new JRadioButton("da");
window.setVisible(true);
window.setSize(500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(wahl);
window.add(wahl2);
}



}






hallo,

und zwar hätte ich mal eine frage und hoffe, dass mir jemand diese beantworten könnte.
Ich versuch momentan verzweifelt diese RadioButtons untereinander anzuordnen, jedoch ist bei der ausführung des programmes immer nur einer zu sehen. Ich bin ein totaler anfänger und habe kaum ahnung davon was ich hier machen könnte, voe allem, da setLocation mir auch gar nicht weiterhilft (wenn ich das nutze bleibt alles wie davor und verändert sich nicht)
 
Stichwort: Flowlayout, Borderlayout, Gridlayout -> die 3 reichen locker für dein vorhaben ;=)
 
  • Gefällt mir
Reaktionen: psYcho-edgE
Und das nächste mal bitte den Code anständig formatieren:

Java:
package Main;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class Frame {
    public static void main(String[] args) {
        JFrame window = new JFrame();
        JRadioButton wahl2 = new JRadioButton("hier");
        JRadioButton wahl = new JRadioButton("da");
        window.setVisible(true);
        window.setSize(500, 500);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(wahl);
        window.add(wahl2);
    }
}

Danke!

greetz
hroessler
 
Ich empfehle immer MigLayout. Bedarf etwas Einarbeitung, aber damit kann man praktisch alles erschlagen. Gibt es auch für JavaFX.

Swing Code sollte man übrigens immer im EDT ausführen!

Java:
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class Frame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JRadioButton wahl2 = new JRadioButton("hier");
            JRadioButton wahl = new JRadioButton("da");
            JFrame window = new JFrame();
            window.setLayout(new MigLayout());
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.add(wahl);
            window.add(wahl2, "newline");
            window.setSize(500, 500);
            window.setLocationRelativeTo(null);
            window.setVisible(true);
        });
    }
}
 
  • Gefällt mir
Reaktionen: psYcho-edgE
Zurück
Oben