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:
Bilder: (siehe Anhang)
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)