Layout Probleme.

Status
Nicht offen für weitere Antworten.

dR.LoL

Bekanntes Mitglied
Hallo Leute,
Ich habe ein ARGHES Problem mit diesen Layouts von Java, hoffe ihr könnt mir helfen.

Ich möchte und finde ich habe es so implementiert, Wird allerdings aber NICHT so angezeigt wie ich das mir überlegt habe.

Es sollen 3 Panels sein. Der eine davon ist der Oben, der Oben sollte nochmals 3 Panels haben, das Erste dieser drei Panels (Um es Kurz zu sagen, von den 6 bisher erstellten, das ganz oben links) wird nun mit einem GridLayout(9,2) geteilt. Es soll dann so aussehen:

Links:
Calltype:
National
Call MO
Call MT
SMS MO
....

Rechts
Empty (Abstandhalter)
International
Call MO
Call MT
SMS MO
...

Es wird allerdings mit meinem Code so dargestellt:
Calltype: Empty National
International Call MO Call MO
Call MT Call MT SMS MO
....

Code:
//JPanel
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(3,1));
    myPanel.setLocation(0, 0);
    myPanel.setSize(800,600);
    JPanel myPanelTop = new JPanel();
    myPanelTop.setLayout(new GridLayout(1,3));
    myPanelTop.setLocation(0, 0);
    myPanelTop.setSize(800, 200);
    JPanel myPanelTopLeft = new JPanel();
    myPanelTopLeft.setLayout(new GridLayout(9,2));
    myPanelTopLeft.setLocation(0, 0);
    myPanelTopLeft.setSize(200, 200);
    JPanel myPanelTopCenter = new JPanel();
    myPanelTopCenter.setLayout(new FlowLayout(FlowLayout.LEFT));
    myPanelTopCenter.setLocation(200,0);
    myPanelTopCenter.setSize(200, 200);
    JPanel myPanelTopRight = new JPanel();
    myPanelTopRight.setLayout(new FlowLayout(FlowLayout.LEFT));
    myPanelTopRight.setLocation(400, 0);
    myPanelTopRight.setSize(300, 200);
    JPanel myPanelCenter = new JPanel();
    myPanelCenter.setLayout(new FlowLayout(FlowLayout.LEFT));
    myPanelCenter.setLocation(0, 200);
    myPanelCenter.setSize(800, 200);
    JPanel myPanelBottom = new JPanel();
    myPanelBottom.setLayout(new FlowLayout(FlowLayout.LEFT));
    myPanelBottom.setLocation(0, 400);
    myPanelBottom.setSize(800, 200);
    //JLabel
    JLabel lblEmpty = new JLabel("");
    JLabel lblCalltype = new JLabel("Calltype:");
    JLabel lblNational = new JLabel("National");
    JLabel lblInternational = new JLabel("International");
    //JRadioButton
    JRadioButton rdbNatCallMO = new JRadioButton("Call MO");
    JRadioButton rdbNatCallMT = new JRadioButton("Call MT");
    JRadioButton rdbNatSMSMO = new JRadioButton("SMS MO");
    JRadioButton rdbNatSMSMT = new JRadioButton("SMS MT");
    JRadioButton rdbNatMMSMO = new JRadioButton("MMS MO");
    JRadioButton rdbNatMMSMT = new JRadioButton("MMS MT");
    JRadioButton rdbNatGPRSMO = new JRadioButton("GPRS MO");
    JRadioButton rdbNatGPRSMT = new JRadioButton("GPRS MT");
    JRadioButton rdbIntCallMO = new JRadioButton("Call MO");
    JRadioButton rdbIntCallMT = new JRadioButton("Call MT");
    JRadioButton rdbIntSMSMO = new JRadioButton("SMS MO");
    JRadioButton rdbIntSMSMT = new JRadioButton("SMS MT");
    JRadioButton rdbIntMMSMO = new JRadioButton("MMS MO");
    JRadioButton rdbIntMMSMT = new JRadioButton("MMS MT");
    JRadioButton rdbIntGPRSMO = new JRadioButton("GPRS MO");
    JRadioButton rdbIntGPRSMT = new JRadioButton("GPRS MT");
    //JButton
    JButton myRateButton = new JButton("Rate it!");
    myRateButton.addActionListener(
      new ActionListener() { 
        public void actionPerformed(ActionEvent event) {
          onClickmyRateButton();
        }
      }
    );
    //ButtonGroup
    ButtonGroup grpNat = new ButtonGroup();
    grpNat.add(rdbNatCallMO);
    grpNat.add(rdbNatCallMT);
    grpNat.add(rdbNatSMSMO);
    grpNat.add(rdbNatSMSMT);
    grpNat.add(rdbNatMMSMO);
    grpNat.add(rdbNatMMSMT);
    grpNat.add(rdbNatGPRSMO);
    grpNat.add(rdbNatGPRSMT);
    ButtonGroup grpInt = new ButtonGroup();
    grpInt.add(rdbIntCallMO);
    grpInt.add(rdbIntCallMT);
    grpInt.add(rdbIntSMSMO);
    grpInt.add(rdbIntSMSMT);
    grpInt.add(rdbIntMMSMO);
    grpInt.add(rdbIntMMSMT);
    grpInt.add(rdbIntGPRSMO);
    grpInt.add(rdbIntGPRSMT);
    
    //Assembly GUI components
    //myPanelTopLeft.add(myButton, FlowLayout.LEFT);
    myPanelTopLeft.add(lblCalltype);
    myPanelTopLeft.add(lblEmpty);
    myPanelTopLeft.add(lblNational);
    myPanelTopLeft.add(lblInternational);
    myPanelTopLeft.add(rdbNatCallMO);
    myPanelTopLeft.add(rdbIntCallMO);
    myPanelTopLeft.add(rdbNatCallMT);
    myPanelTopLeft.add(rdbIntCallMT);
    myPanelTopLeft.add(rdbNatSMSMO);
    myPanelTopLeft.add(rdbIntSMSMO);
    myPanelTopLeft.add(rdbNatSMSMT);
    myPanelTopLeft.add(rdbIntSMSMT);
    myPanelTopLeft.add(rdbNatMMSMO);
    myPanelTopLeft.add(rdbIntMMSMO);
    myPanelTopLeft.add(rdbNatMMSMT);
    myPanelTopLeft.add(rdbIntMMSMT);
    myPanelTopLeft.add(rdbNatGPRSMO);
    myPanelTopLeft.add(rdbIntGPRSMO);
    myPanelTopLeft.add(rdbNatGPRSMT);
    myPanelTopLeft.add(rdbIntGPRSMT);
    myPanelTopCenter.add(new JLabel("myPanelTopCenter"));
    myPanelTopRight.add(new JLabel("myPanelTopRight"));
    myPanelTop.add(myPanelTopLeft);
    myPanelTop.add(myPanelTopCenter);
    myPanelTop.add(myPanelTopRight);
    myPanelCenter.add(new JLabel("myPanelCenter"));
    myPanelBottom.add(new JLabel("myPanelBottom"));
    myPanel.add(myPanelTop);
    myPanel.add(myPanelCenter);
    myPanel.add(myPanelBottom);
    
    //Assembly GUI components into JFrame
    this.setJMenuBar(myMenuBar);
    this.getContentPane().add(myPanel);
    this.pack();



Ich bin euch wirklich dankbar für eure Hilfe!
Ich finde es einfach nicht raus und möchte nicht noch mehr Zeit unnötig dabei verbrauchen!

Danke und Gruss
vom LoL
 
Ich bin einfach noch nicht vertraut mit Java Swing, deshalb hier nochmal etwas Code.

Code:
...
    JPanel myPanelBottom = new JPanel();
    myPanelBottom.setLayout(new GridLayout(2,1));
    myPanelBottom.setLocation(0, 400);
    myPanelBottom.setSize(800, 200);
...
    myPanelBottom.add(lblStatus);
    myPanelBottom.add(new JScrollPane(txtStatus));
...

Weshalb ist zwischen dem lblStatus und der Textarea nun ein ca. 50 Pixel grosser Abstand? :S

Habe das Gefühl, Swing macht immer einfach mal was...
Oder implementiere ich so katastrophal?
Wäre wirklich froh, wenn mir mal wer helfen kann....
 
Ich konnte bisher bereits das Erste Problem lösen, nur das mit dem Status text:

...
JPanel myPanelBottom = new JPanel();
myPanelBottom.setLayout(new GridLayout(2,1));
myPanelBottom.setLocation(0, 400);
myPanelBottom.setSize(800, 200);
...
myPanelBottom.add(lblStatus);
myPanelBottom.add(new JScrollPane(txtStatus));
...

Ist noch unklar!


Guckt in diesem Forum eigendlich keiner rein?
Oder sind meine Fragen zu blöde?
Oder soll ich so eine Newb Frage das nächste mal einfach im Beginner Forum posten?
 
Mir ist das alles ein wenig konfus. Kannst du mal eine Zeichnung posten, dann kann man sich eventuell etwas mehr von deinem Problem vorstellen.
 
Erstmal danke, dass du dich bereit erklärt hast, mir versuchen zu helfen :]><

Okay, Printscreen des GUI's (Neuling, hab extrem Probleme mich an das Layout zeuch zu gewöhnen (Okk, bin noch nicht lange an Java)

Screenshot:
shot1.jpg



Problem:
Das Problem sollte klar sein auf dem Screen, atm habe ich noch probleme das oben rechts korrekt darzustellen, sollte ich aber bald hinkriegen... unten ist der Fehler klar ersichtlich, ca 1 cm abstand von "Status:" bis dann das JTextfield folgt....
Das ist das Problem

Code:
Code:
    //JPanel
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new GridLayout(3,1));
    myPanel.setLocation(0, 0);
    myPanel.setSize(800,600);
    JPanel myPanelTop = new JPanel();
    myPanelTop.setLayout(new GridLayout(1,2));
    myPanelTop.setLocation(0, 0);
    myPanelTop.setSize(800, 300);
    JPanel myPanelTopLeft = new JPanel();
    myPanelTopLeft.setLayout(new GridLayout(9,2));
    myPanelTopLeft.setLocation(0, 0);
    myPanelTopLeft.setSize(400, 300);
    JPanel myPanelTopRight = new JPanel();
    myPanelTopRight.setLayout(new GridLayout(10,2));
    myPanelTopRight.setLocation(400, 0);
    myPanelTopRight.setSize(400, 300);
    JPanel myPanelCenter = new JPanel();
    myPanelCenter.setLayout(new GridLayout(2,1));
    myPanelCenter.setLocation(0, 200);
    myPanelCenter.setSize(800, 200);
    JPanel myPanelBottom = new JPanel();
    myPanelBottom.setLayout(new GridLayout(2,1));
    myPanelBottom.setLocation(0, 400);
    myPanelBottom.setSize(800, 100);
...
    JTextArea txtStatus = new JTextArea();
    txtStatus.setEditable(false);
    txtStatus.setLineWrap(true);
    txtStatus.setWrapStyleWord(true);
    txtStatus.setForeground(Color.GREEN);
    txtStatus.setText(getTimestamp()+"\tSRETT is started!");
...
myPanelBottom.add(lblStatus);
myPanelBottom.add(new JScrollPane(txtStatus));
...
myPanel.add(myPanelBottom);
...
this.getContentPane().add(myPanel);
...



THX!
 
Hey ho,
Ich würde dir vorschlagen auf die vorgefertigten Layouts zu verzichten und selbst ein Layout zu diesem Zwecke zu schreiben! Das ganze ist leichter als gedacht! Jedes Component enthält die Methode setBounds(int, int, int,int) (glaub ich zumindest das es immer int ist, kannste ja in der api nachschauen). Mit dieser Methode kannst du deine Komponente auf dem Bildschirm anordnen und entweder durch gute Einschätzung oder durch testen zum Ziel kommen.
Tipp: Um ein RICHTIG eigenes Layout zu schreiben implementierst du in einer Klasse das Interface LayoutManager.

Hier ein kurzes Beispiel:

Code:
public class MyOwnLayout implements LayoutManager
{
	    public void addLayoutComponent(String name, Component comp) {
	    }

	    public void removeLayoutComponent(Component comp) {
	    }

	    public Dimension preferredLayoutSize(Container parent)
	    {
	        Dimension dim = new Dimension(0, 0);

	        Insets insets = parent.getInsets();
	        dim.width = 320 + insets.left + insets.right;
	        dim.height = 168 + insets.top + insets.bottom;

	        return dim;
	    }
	    public Dimension minimumLayoutSize(Container parent)
	    {
	        Dimension dim = new Dimension(0, 0);
	        return dim;
	    }

	    public void layoutContainer(Container parent)
	    {
	        Insets insets = parent.getInsets();
	        Component c;
	        
	        c = parent.getComponent(0);
	        if (c.isVisible())
	        {
	        	c.setBounds(insets.left+12,insets.top+10,40,20);
	        }
	        
	        c = parent.getComponent(1);
	        if (c.isVisible())
	        {
	        	c.setBounds(insets.left+62,insets.top+10,112,20);
	        }
	        
	        c = parent.getComponent(2);
	        if (c.isVisible())
	        {
	        	c.setBounds(insets.left+12,insets.top+50,80,20);
	        }
	        
	        c = parent.getComponent(3);
	        if (c.isVisible())
	        {
	        	c.setBounds(insets.left+120,insets.top+50,152,20);
	        }
	        
	  }
}

Theoretisch kannst du es so übernehmen und nur den layoutContainer anpassen. Eine if-Verzweigung steht für eine Komponente!
Tipp für die Übersichtlichkeit: Packe z.b. die Checkboxen in ein JPanel, adde das JPanel in DIESES Layout und mache ein Extra Layout für das JPanel oder benutze dort dann ein GridLayout oder so 😉

Wegen setBounds musste nochmal in der API schaun, hoffe aber dir doch ein wenig geholfen zu haben 😉
Machs gut,
Deception
 
@Dr LoL

Also dein Problem besteht darin, dass die Komponenten in einem GridLayout ja immer gleich groß sind. Das heisst über deinem JLabel "Status" ist auch 1 cm Platz, was du bisher aber noch nicht gemerkt hast.

Tip: Verwende statt dem GridLayout das GridBagLayout. Das ist zwar etwas komplizierter, lässt dir aber absolut freie Hand. Ich benutze das fast nur noch. Und selbst einen LayoutManager schreiben halte ich für übertrieben. Und ich würde auch nicht so viel mit dem setLocation Methoden arbeiten. Sowas kann immer merkwürdige Folgen haben, wenn man das Fenster mal resized usw.

Gruß,
VipViper2000
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben