myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   GroupLayout - Alignment (https://mygully.com/showthread.php?t=2770201)

d03jo4n 05.12.12 17:01

GroupLayout - Alignment
 
السلام عليكم

ich entwickle zur Zeit eine Anwendung in Java mit Hilfe von u.A. Swing.
In einem Fenster soll im oberen Bereich soll eine Statusanzeige eingefügt werden, die auf der linken Seite die Anzahl der noch abzuarbeitenden Verbindungen und rechts den Status des aktuellen Servers mit einem Button zum Starten/Stoppen angezeigt werden.
Ich habe einen Screenshot angefügt und die anderen Kontrollen entfernt.

http://i45.tinypic.com/2dw6r61.jpg

Die Statusanzeige wurde mit einem GroupLayout in das Fenster eingefügt. Es selber richtet den Content ebenfalls über ein solches aus.
Doch irgendwie klappt das nicht so, wie ich das gerne möchte, denn die Komponenten werden jeweils zentriert dargestellt und ich komme nicht drauf, wieso das so ist.

Vielleicht findet hier ja jemand meinen Fehler ;)

[Link nur für registrierte und freigeschaltete Mitglieder sichtbar. Jetzt registrieren...]

Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class StatusPane extends JPanel implements ServerStateListener, ActionListener
{
        protected Server server;
        protected JButton action;
        protected JLabel label;
        protected StatusPane.CounterLabel counter;
        protected ServerStateListener.States currentState;
       
        protected final static String RUNNING = "Server is running";
        protected final static String NOT_RUNNING = "Server is not running";
        protected final static String START = "Start server";
        protected final static String STOP = "Stop server";
       
        public StatusPane(Server server)
        {
                super();
               
                this.server = server;
               
                this.action = new JButton(StatusPane.START);
                this.label  = new JLabel(StatusPane.NOT_RUNNING);
                this.currentState = ServerStateListener.States.SERVER_STOPPED;
                this.counter = new StatusPane.CounterLabel();
               
                this.initComponents();
        }
       
        private void initComponents()
        {
                this.server.addServerStateListener(this);
               
                GroupLayout layout = new GroupLayout(this);
               
                layout.setHorizontalGroup(
                        layout.createSequentialGroup()
                                .addComponent(this.counter)
                                .addGap(0,0,Short.MAX_VALUE)
                                .addComponent(this.label)
                                .addComponent(this.action)
                        );
               
                layout.setVerticalGroup(
                        layout.createParallelGroup()
                                .addComponent(this.counter)
                                .addGap(0,0,Short.MAX_VALUE)
                                .addComponent(this.label)
                                .addComponent(this.action)
                        );
        }

        @Override
        public void stateChanged(ServerStateListener.States newState)
        {
                this.currentState = newState;

                switch( newState )
                {
                        case SERVER_STARTED:
                                this.label.setText(StatusPane.RUNNING);
                                this.action.setText(StatusPane.STOP);
                                break;
                        case SERVER_STOPPED:
                                this.label.setText(StatusPane.NOT_RUNNING);
                                this.action.setText(StatusPane.START);
                                break;
                        case CONNECTION_OPENED:
                                this.counter.increase();
                                break;
                        case CONNECTION_CLOSED:
                                this.counter.decrease();
                                break;
                        case REQUEST_SEND:
                                // TODO:        Implement
                                break;
                        case REQUEST_RECEIVED:
                                // TODO:        Implement
                                break;
                }
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
                // TODO:        Implement
        }
   
       
        protected static class CounterLabel extends JLabel
        {
                protected int count;
               
                public CounterLabel()
                {
                        super("0 queued connections");
                        this.count = 0;
                }
               
                public void increase()
                {
                        this.count++;
                        this.setText(this.count+" queued connections");
                }
               
                public void decrease()
                {
                        this.count--;
                        this.setText(this.count+" queued connections");
                }
        }
}


Your_Conscience 05.12.12 17:29

Das liegt am [Link nur für registrierte und freigeschaltete Mitglieder sichtbar. Jetzt registrieren...].

d03jo4n 05.12.12 17:36

Zitat:

Zitat von Your_Conscience (Beitrag 24084444)
Das liegt am [Link nur für registrierte und freigeschaltete Mitglieder sichtbar. Jetzt registrieren...].

Dachte ich ja auch.
Aber es bleibt zentriert, wenn ich das Alignment des StatusPanes im Fenster ändere ...

Code:

mwl.setHorizontalGroup(
                        mwl.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(this.statusPane)
                                .addComponent(pane)
                        );

... oder wenn ich das Alignment im StatusPane ändere.


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:58 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.