Liebe Community,
mehr oder weniger hilf- und ratlos darf ich mich an euch wenden. Folgendes Code-Beispiel demonstriert mein Problem sehr deutlich. Ich habe 3 Panels, welche mittels BorderLayout im Hauptfenster bzw. auf dessen Container (main_container) links (West), mittig (Center) und rechts (East) angeordnet werden. Jedem dieser Panels wird mit .setLayout(null) mitgeteilt, dass dessen zukünftige Komponenten (in diesem Fall Buttons) pixelgenau angeordnet werden sollen. Leider funktioniert das jedoch nur für das mittige Panel, nicht jedoch für das linke und das rechte Panel. Nun meine - wahrscheinlich sehr dumme - Frage: Warum funktioniert das nicht für alle Panels gleichermaßen?
Vielen Dank für eure Mühe und Hilfe!
mehr oder weniger hilf- und ratlos darf ich mich an euch wenden. Folgendes Code-Beispiel demonstriert mein Problem sehr deutlich. Ich habe 3 Panels, welche mittels BorderLayout im Hauptfenster bzw. auf dessen Container (main_container) links (West), mittig (Center) und rechts (East) angeordnet werden. Jedem dieser Panels wird mit .setLayout(null) mitgeteilt, dass dessen zukünftige Komponenten (in diesem Fall Buttons) pixelgenau angeordnet werden sollen. Leider funktioniert das jedoch nur für das mittige Panel, nicht jedoch für das linke und das rechte Panel. Nun meine - wahrscheinlich sehr dumme - Frage: Warum funktioniert das nicht für alle Panels gleichermaßen?
Vielen Dank für eure Mühe und Hilfe!
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo
{
JFrame programWindow; // Main window
JButton menu_button1; // One
JButton menu_button2; // Two
JButton menu_button3; // Three
JPanel panel1; // West
JPanel panel2; // Center
JPanel panel3; // East
Container main_container;
public static void main(String[] args)
{
Demo demo = new Demo();
demo.setGui();
}
public void setGui()
{
// Create Frame
this.programWindow = new JFrame("Multiple Layout-Demo");
this.programWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.programWindow.setSize(800,600);
// Create three Panels
this.panel1 = new JPanel();
this.panel2 = new JPanel();
this.panel3 = new JPanel();
// Switch Panel Layouts to "null"
this.panel1.setLayout(null);
this.panel2.setLayout(null);
this.panel3.setLayout(null);
// Switch Main Layout to "BorderLayout"
this.main_container = new Container();
this.main_container = this.programWindow.getContentPane();
this.main_container.setLayout(new BorderLayout());
// Create three Buttons
this.menu_button1 = new JButton("One");
this.menu_button1.setSize(130,35);
this.menu_button1.setLocation(10,10);
this.menu_button2 = new JButton("Two");
this.menu_button2.setSize(130,35);
this.menu_button2.setLocation(10,50);
this.menu_button3 = new JButton("Three");
this.menu_button3.setSize(130,35);
this.menu_button3.setLocation(10,90);
// Add Buttons to Panels
this.panel1.add(this.menu_button1);
this.panel2.add(this.menu_button2);
this.panel3.add(this.menu_button3);
this.panel1.setBackground(Color.red);
this.panel2.setBackground(Color.green);
this.panel3.setBackground(Color.blue);
// add Panels to main Container
this.main_container.add(this.panel1,BorderLayout.WEST);
this.main_container.add(this.panel2,BorderLayout.CENTER);
this.main_container.add(this.panel3,BorderLayout.EAST);
this.programWindow.setVisible(true);
}
}