السلام عليكم
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.
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 Mitglieder sichtbar. Bitte einloggen oder neu 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");
}
}
}