Problem mit Box bzw BorderLayout

Ruderer1993

Aktives Mitglied
Hey, ich habe einen Frame der bei einem kleinen Programm die Optionen darstellen soll.
Ich habe dabei eine Überschrift (als Label), eine EditorPane in der eine HTML Datei angezeigt wird und dann zwei JRadioButtons mit denen man Musik an/ausschalten soll und einen Zurück Button der zum StartFrame leiten soll.

Ich wollte mein Programm nun wie folgt aufbauen:

Überschrift - Label -> North(Border Layout)
EditorPane && JRadioButtons(Border Layout Center, und im Panel selber nochmal BoxLayout damit die JRadioButtons eben unter der JEditorPane sind)
Zurück Button -> South BorderLayout

Das klappt auch alles fast ;)
Das Problem ist das die JRadioButtons zwar unter der JEditorPane angezeigt werden, aber nicht links "orientiert" sind. Außerdem stehen jene untereinander und nicht nebeneinander.
Ich poste hier einfach mal zwei Bilder so wie ich es im Moment habe und so wie es sein soll.

Hier auch mal der Code:

Java:
public class OptionFrame implements ActionListener {

	JButton backButton;
	JFrame f;
	JLabel headline;
	JEditorPane editorPane;
	JPanel centerPanel;
	JPanel northPanel;
	JPanel southPanel;
	
	public OptionFrame(int w, int h) {
		
		f = new JFrame("Optionen");
		f.setPreferredSize(new Dimension(w,h));
		f.setLocation(100,100);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());
		
		centerPanel = new JPanel();
		northPanel = new JPanel();
		southPanel = new JPanel();
		
		southPanel.setLayout(new BorderLayout());
		northPanel.setLayout(new BorderLayout());
		centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));
		
		JRadioButton switchMusicOn = new JRadioButton("Sound an");
		switchMusicOn.setActionCommand("on");
		
		JRadioButton switchMusicOff = new JRadioButton("Sound aus");
		switchMusicOff.setActionCommand("off");
		switchMusicOff.setForeground(Color.WHITE);
		switchMusicOn.setForeground(Color.WHITE);
		
		ButtonGroup group = new ButtonGroup();
		group.add(switchMusicOn);
		group.add(switchMusicOff);

		switchMusicOff.addActionListener(this);
		switchMusicOn.addActionListener(this);
		
				
		headline = new JLabel("Optionen");
		headline.setForeground(Color.WHITE);
		headline.setFont(new Font("Arial",Font.BOLD,60));
		northPanel.add(headline,BorderLayout.NORTH);
		
		String url = ""+getClass().getResource("option.html");
		
		try {
			editorPane = new JEditorPane(url);
			
			EditorKit editorKit = editorPane.getEditorKitForContentType ("text/html");
			editorPane.setEditorKit (editorKit);
			editorPane.setPage(url);
			editorPane.setEditable(false);
			
			editorPane.setAlignmentX(Component.CENTER_ALIGNMENT);
			editorPane.setAlignmentY(Component.CENTER_ALIGNMENT);
			editorPane.setBackground(Color.BLUE);			
			centerPanel.add (editorPane);
		} catch (IOException e) {
			e.printStackTrace();
		}
		switchMusicOff.setAlignmentX(Component.LEFT_ALIGNMENT);
		switchMusicOff.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		switchMusicOn.setAlignmentX(Component.LEFT_ALIGNMENT);
		switchMusicOn.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		
		centerPanel.add(switchMusicOff);
		centerPanel.add(switchMusicOn);
		
		
		
	    
		backButton = new JButton("Zurück");
		backButton.setActionCommand("back");
		backButton.addActionListener(this);
		southPanel.add(backButton,BorderLayout.SOUTH);
		

	    Container cp = f.getContentPane();
	    Container cp1 = northPanel;
	    Container cp2 = southPanel;
	    Container cp3 = centerPanel;
	    cp2.setBackground(Color.BLUE);
	    cp3.setBackground(Color.BLUE);
	    cp1.setBackground(Color.BLUE);
	    cp.setBackground(Color.BLUE);	
	    f.add(northPanel,BorderLayout.NORTH);
	    f.add(centerPanel,BorderLayout.CENTER);	
	    f.add(southPanel,BorderLayout.SOUTH);
		
	    f.pack();
		f.setVisible(true);
	}
	
	...ActionListener....


Bilder: (siehe Anhang)
 

Anhänge

  • Momentan.jpg
    Momentan.jpg
    72 KB · Aufrufe: 37
  • Richtig.jpg
    Richtig.jpg
    72 KB · Aufrufe: 31
C

Camino

Gast
Na ja, wenn du die Kompomenten im BoxLayout untereinander anordnest und einfügst, dann erscheinen die natürlich auch untereinander. Vielleicht musst du nochmal verschachteln und für die RadioButtons ein eigenes Panel anlegen, auf denen du dann die Ausrichtung bestimmen kannst.
 

Xeonkryptos

Bekanntes Mitglied
Es ist klar, dass die RadioButtons sich unterhalb anordnen, weil du in deinem Code den centerPanel mit der Konstanten BoxLayout.Y_AXIS initialisiert, das bedeutet, dass sich alles in dem Layout von oben nach unten ausrichtet!

Für dein Problem bräuchtest du ein weiteres Layout, damit du die RadioButtons von links nach rechts anordnen kannst mit der BoxLayout.X_AXIS-Konstanten.

so meine ich das:
Java:
JPanel centerPanel, radioCenterPanel;
centerPanel = new JPanel()
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS);

radioCenterPanel = new JPanel();
radioCenterPanel.setLayout(newBoxLayout(radioCenterPanel, BoxLayout.X_AXIS);

centerPanel.add(deinEditorPane);

radioCenterPanel.add(radioButtons);

centerPanel.add(radioCenterPanel);

So in etwa müsste es hinhauen, wenn es an die richtigen Stellen packst
 

jgh

Top Contributor
trotz der schon vorhanden Antworten...so sieht dein Code unverändert bei mir aus, und nur durch Veränderung der
[java=25] centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));[/code]
auf X_AXIS ist dein gewünschtes Aussehen erreichbar. + das Opaque für die JLabels...

Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.text.EditorKit;

public class OptionFrame {

	JButton backButton;
	JFrame f;
	JLabel headline;
	JEditorPane editorPane;
	JPanel centerPanel;
	JPanel northPanel;
	JPanel southPanel;

	public OptionFrame(int w, int h) {

		f = new JFrame("Optionen");
		f.setPreferredSize(new Dimension(w, h));
		f.setLocation(100, 100);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());

		centerPanel = new JPanel();
		northPanel = new JPanel();
		southPanel = new JPanel();

		southPanel.setLayout(new BorderLayout());
		northPanel.setLayout(new BorderLayout());
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));

		JRadioButton switchMusicOn = new JRadioButton("Sound an");
		switchMusicOn.setActionCommand("on");

		JRadioButton switchMusicOff = new JRadioButton("Sound aus");
		switchMusicOff.setActionCommand("off");
		switchMusicOff.setForeground(Color.WHITE);
		switchMusicOn.setForeground(Color.WHITE);
		switchMusicOff.setOpaque(false);
		switchMusicOn.setOpaque(false);

		ButtonGroup group = new ButtonGroup();
		group.add(switchMusicOn);
		group.add(switchMusicOff);

		// switchMusicOff.addActionListener(this);
		// switchMusicOn.addActionListener(this);

		headline = new JLabel("Optionen");
		headline.setForeground(Color.WHITE);
		headline.setFont(new Font("Arial", Font.BOLD, 60));
		northPanel.add(headline, BorderLayout.NORTH);

		String url = "" + getClass().getResource("option.html");

		try {
			editorPane = new JEditorPane(url);

			EditorKit editorKit = editorPane
					.getEditorKitForContentType("text/html");
			editorPane.setEditorKit(editorKit);
			editorPane.setPage(url);
			editorPane.setEditable(false);

			editorPane.setAlignmentX(Component.CENTER_ALIGNMENT);
			editorPane.setAlignmentY(Component.CENTER_ALIGNMENT);
			editorPane.setBackground(Color.BLUE);
			centerPanel.add(editorPane);
		} catch (IOException e) {
			e.printStackTrace();
		}
		switchMusicOff.setAlignmentX(Component.LEFT_ALIGNMENT);
		switchMusicOff.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		switchMusicOn.setAlignmentX(Component.LEFT_ALIGNMENT);
		switchMusicOn.setAlignmentY(Component.BOTTOM_ALIGNMENT);

		centerPanel.add(switchMusicOff);
		centerPanel.add(switchMusicOn);

		backButton = new JButton("Zurück");
		backButton.setActionCommand("back");
		// backButton.addActionListener(this);
		southPanel.add(backButton, BorderLayout.SOUTH);

		Container cp = f.getContentPane();
		Container cp1 = northPanel;
		Container cp2 = southPanel;
		Container cp3 = centerPanel;
		cp2.setBackground(Color.BLUE);
		cp3.setBackground(Color.BLUE);
		cp1.setBackground(Color.BLUE);
		cp.setBackground(Color.BLUE);
		f.add(northPanel, BorderLayout.NORTH);
		f.add(centerPanel, BorderLayout.CENTER);
		f.add(southPanel, BorderLayout.SOUTH);

		f.pack();
		f.setVisible(true);
	}

	public static void main(String[] args) {
		OptionFrame of = new OptionFrame(500, 200);

	}
}
 

Anhänge

  • deinCode.PNG
    deinCode.PNG
    39,3 KB · Aufrufe: 22
  • mitXAxis.PNG
    mitXAxis.PNG
    32,6 KB · Aufrufe: 26

Ruderer1993

Aktives Mitglied
hm ja das macht er dann bei mir auch aber das Problem ist das er das dann wie folgt anzeigt bei mir (weil ich ja noch die EditorPane habe)
 

Anhänge

  • Bildschirmfoto 2011-10-09 um 14.03.19.png
    Bildschirmfoto 2011-10-09 um 14.03.19.png
    56 KB · Aufrufe: 24

Xeonkryptos

Bekanntes Mitglied
hm ja das macht er dann bei mir auch aber das Problem ist das er das dann wie folgt anzeigt bei mir (weil ich ja noch die EditorPane habe)

Genau deshalb hab ich die Verschachtelung vorgeschlagen! Du packst die RadioButtons in ein eigenes Panel, welches sie seitlich anordnet und packst dieses Panel dann wieder in das centerPanel. Und schon ordnet er das richtig an.
 

Ruderer1993

Aktives Mitglied
ok habe es nun genau so wie du es vorgeschlagen hast gemacht.. es klappt auch fast. Die Radio Buttons werden nebeneinander und unter der Editor Panne angezeigt.
Aber sie werden nicht Links vom Frame angezeigt obwohl ich Component.LEFT_ALIGNMENT geschrieben habe ... dazu vllt noch eine Idee ? Danke schonmal bis hierhin !

Edit: Habs nun hinbekommen... einfach bei dem RadioCenterPanel ein FlowLayout genommen und schon gings... merkwürdig aber nun gut.. ;)
Danke !
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
K JavaFX Resizing-Problem beim BorderLayout (Center Component) beim Arbeiten mit mehreren FXMLs AWT, Swing, JavaFX & SWT 2
S Problem mit BorderLayout NORTH und SOUTH AWT, Swing, JavaFX & SWT 2
Strahlungsleck Problem mit dem Anzeigen von Elementen im BorderLayout AWT, Swing, JavaFX & SWT 6
M Layout Problem (GrudBag- + BorderLayout) AWT, Swing, JavaFX & SWT 6
B BorderLayout/JSplitPane Problem beim Ausrichten AWT, Swing, JavaFX & SWT 5
G Problem mit der Anzeige von jLabel. Unlesbar wenn der Text geändert wird. AWT, Swing, JavaFX & SWT 28
H 2D-Grafik Problem mit Paint AWT, Swing, JavaFX & SWT 1
S Layout - Problem AWT, Swing, JavaFX & SWT 1
Tassos JavaFX/Problem mit der Maussteuerung in Stackpane AWT, Swing, JavaFX & SWT 7
sserio Java Fx - Problem AWT, Swing, JavaFX & SWT 3
A Problem Spiel auf Panel der GUI zu bringen AWT, Swing, JavaFX & SWT 1
A JavaFX Controller Problem AWT, Swing, JavaFX & SWT 1
TheWhiteShadow JavaFX ListView Problem beim Entfernen von Elementen AWT, Swing, JavaFX & SWT 1
E LayoutManager Welcher Layout-Mix löst mein Problem? AWT, Swing, JavaFX & SWT 3
Umb3rus JavaFX Problem mit PropertyValueFactory: can not read from unreadable property AWT, Swing, JavaFX & SWT 1
T Problem mit paintComponent() AWT, Swing, JavaFX & SWT 17
AmsananKING Java Menü-Problem AWT, Swing, JavaFX & SWT 1
G Instance OF Problem AWT, Swing, JavaFX & SWT 9
FrittenFritze Ein Problem mit der CSSBox, die Größe wird nicht angepasst AWT, Swing, JavaFX & SWT 5
M Problem mit dem Anzeigen von Frames im Vordergrund AWT, Swing, JavaFX & SWT 5
Badebay Problem mit JButton AWT, Swing, JavaFX & SWT 2
newJavaGeek Grid-Layout problem AWT, Swing, JavaFX & SWT 7
J JavaFX Löschen im Tabelview macht Problem AWT, Swing, JavaFX & SWT 15
JavaTalksToMe JavaFx ExekutorService Problem AWT, Swing, JavaFX & SWT 2
Zrebna Problem bei Eventhandling (Value soll nach jedem erneutem Klick gelöscht werden) AWT, Swing, JavaFX & SWT 4
B Problem mit JavaFX AWT, Swing, JavaFX & SWT 5
J css Problem AWT, Swing, JavaFX & SWT 5
B JavaFX habe mein Problem fett markiert AWT, Swing, JavaFX & SWT 2
A Swing Filter-Problem AWT, Swing, JavaFX & SWT 1
temi JavaFX Problem mit IntelliJ und JavaFx 11 unter XUbuntu AWT, Swing, JavaFX & SWT 3
L Java FX Problem mit Ubuntu 18 und JavaFx AWT, Swing, JavaFX & SWT 27
H JTable TableCellEditor-Problem AWT, Swing, JavaFX & SWT 0
kodela Swing Problem mit Warten-Dialog AWT, Swing, JavaFX & SWT 16
B JavaFx Scene Builder Problem AWT, Swing, JavaFX & SWT 2
B [Problem] Java öffnet Word-Datein nicht AWT, Swing, JavaFX & SWT 14
T DataBinding Problem AWT, Swing, JavaFX & SWT 5
Blender3D Problem mit € Symbol Font Gotham Windows 10 Swing AWT, Swing, JavaFX & SWT 11
T Problem mit JTable Sortierung AWT, Swing, JavaFX & SWT 2
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 15
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 0
D Swing SwingUtils / Thread Problem AWT, Swing, JavaFX & SWT 3
L JavaFX Problem beim Aufrufen einer Methode AWT, Swing, JavaFX & SWT 5
T Swing Problem mit Datum und FormattedTextField AWT, Swing, JavaFX & SWT 2
S AWT Java print dialog Problem AWT, Swing, JavaFX & SWT 0
olfibits JavaFX Problem mit HTMLEditor AWT, Swing, JavaFX & SWT 0
W SWT hover-background-problem with first column in TreeViewer AWT, Swing, JavaFX & SWT 0
M Problem mit Add JScrollPane AWT, Swing, JavaFX & SWT 25
Mario1409 Swing JTextArea scroll Problem AWT, Swing, JavaFX & SWT 0
N Swing Problem mit loop AWT, Swing, JavaFX & SWT 2
S Swing Problem mit Button und ActionListener AWT, Swing, JavaFX & SWT 5
S Swing & Clean und build Problem AWT, Swing, JavaFX & SWT 12
S JLabel setText() Problem AWT, Swing, JavaFX & SWT 6
I 2D-Grafik Problem beim Ändern der Farbe eine 2d Objekts AWT, Swing, JavaFX & SWT 3
G Swing Splitpane Problem AWT, Swing, JavaFX & SWT 1
F Problem mit der FXML Rectangle Shape AWT, Swing, JavaFX & SWT 2
N JavaFX Stranges Problem mit der Autoscroll-Eigenschaft von Textareas AWT, Swing, JavaFX & SWT 0
E Java FX FXML Problem mit html Scriptausführung AWT, Swing, JavaFX & SWT 2
J JavaFX Intersect Problem mit Shapes AWT, Swing, JavaFX & SWT 10
R JavaFX MediaPlayer AVI-Problem AWT, Swing, JavaFX & SWT 1
M Swing Problem mit ListCellRenderer AWT, Swing, JavaFX & SWT 7
D Problem mit JTable AWT, Swing, JavaFX & SWT 1
F GUI Auflösung ändern - Koordianten und Proportions Problem AWT, Swing, JavaFX & SWT 21
J Problem mit Button darstellung AWT, Swing, JavaFX & SWT 23
M Problem mit Layoutmanagern... Hilfe wäre sehr nett. AWT, Swing, JavaFX & SWT 2
S 2D-Grafik Problem mit Variablen AWT, Swing, JavaFX & SWT 4
7 JavaFX Problem beim Zeichnen eines Dreiecks in einem GUI AWT, Swing, JavaFX & SWT 6
M Swing AttributiveCellTableModel addRow() Problem AWT, Swing, JavaFX & SWT 1
J Swing Problem mit Graphics Methode AWT, Swing, JavaFX & SWT 4
N JavaFX Problem mit table multiple selection AWT, Swing, JavaFX & SWT 5
K CheckBox Problem AWT, Swing, JavaFX & SWT 5
Grevak DisplayMode Problem seit Windows 10 AWT, Swing, JavaFX & SWT 2
S Swing Eigene JComboBox Problem! AWT, Swing, JavaFX & SWT 1
B Swing Problem mit Bildpfad AWT, Swing, JavaFX & SWT 4
N Swing Problem beim Scrollen mit JScrollPane AWT, Swing, JavaFX & SWT 6
V Graphics g - drawOval problem mit background AWT, Swing, JavaFX & SWT 1
C AWT Problem mit Protokol Fenster AWT, Swing, JavaFX & SWT 0
M Swing pack() Problem mit Taskleiste AWT, Swing, JavaFX & SWT 4
N Swing Choice- Problem! AWT, Swing, JavaFX & SWT 8
Q "AWT-EventQueue-0" Exception Problem AWT, Swing, JavaFX & SWT 4
D jButton Problem, ein Rieser Button bedeckt das ganze frame AWT, Swing, JavaFX & SWT 1
A Problem: repaint() - Schleife AWT, Swing, JavaFX & SWT 3
J Anfänger GUI Problem bei der Ausführung eines sehr einfachen Programms AWT, Swing, JavaFX & SWT 2
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
N Swing JTree Problem beim erstellen der Knoten AWT, Swing, JavaFX & SWT 0
N Swing CardLayout: Problem beim Wechsel zwischen den JPanels AWT, Swing, JavaFX & SWT 3
A Mini-Menu-Schriften. Ein Problem bei hohen DPI Zahlen AWT, Swing, JavaFX & SWT 2
Z Canvas in Frame einfügen. Problem mit 4-Gewinnt AWT, Swing, JavaFX & SWT 1
C Thread-/ Simulations- Problem AWT, Swing, JavaFX & SWT 18
G Swing Setvisible problem AWT, Swing, JavaFX & SWT 1
J JTabbedPane: close Button Problem AWT, Swing, JavaFX & SWT 2
Tom299 JavaFX -> fxmlLoader -> getResourceAsStream Problem AWT, Swing, JavaFX & SWT 1
T Problem: ComboBox und addItem AWT, Swing, JavaFX & SWT 5
M JTextArea wird nicht aktualisiert (ActionListener-Problem) AWT, Swing, JavaFX & SWT 1
T LayoutManager LookAndFeel-Problem AWT, Swing, JavaFX & SWT 4
F Problem mit Implementierung von Kollisionsabfrage AWT, Swing, JavaFX & SWT 5
vodkaz (javafx) Image Problem AWT, Swing, JavaFX & SWT 2
T Problem beim Zeichnen von Rechteck AWT, Swing, JavaFX & SWT 3
B JavaFX Problem bei Kamera / Group, gesamte Scene bewegt sich mit AWT, Swing, JavaFX & SWT 0
L Swing Vier Gewinnt Problem AWT, Swing, JavaFX & SWT 2
Z GUI-Problem, finde meinen Fehler nicht! AWT, Swing, JavaFX & SWT 11

Ähnliche Java Themen

Neue Themen


Oben