Componente mehrmals verwenden ?

  • Themenstarter Themenstarter !Stefan
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
S

!Stefan

Gast
Hallo,

ich erstelle ein Panel mit Gridbaglayout und möchte darauf ein Label mehrmals (roter Kreis) verwenden, allerdings stellt das Gridbaglayout das Label nur an der zuletzt eingefügten Position dar, warum ?

swing.png


Code:
public class Frame extends JFrame {
	private JFrame frame;
	
	public static void main(String[] args) {
		new Frame();
	}
	
	public Frame() {
		super();
		initilisieren();
		
		
	}
	
	private void initilisieren() {
		frame = new JFrame();
		frame.setSize(800, 600);		
		JButton button = new JButton("drück mich");
		frame.setLayout(new GridBagLayout());
		frame.add(erstelleGebuehrenPanel(),  new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0));
		frame.add(button,     			 new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0));
		frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
	}
	
	private JPanel erstelleGebuehrenPanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setLayout(new GridBagLayout());
		JLabel waehrungslbl=new JLabel();
		waehrungslbl.setText("€");
		JLabel stdAlbl=new JLabel("Klasse A");
		JLabel stdBlbl=new JLabel("Klasse B");
		JLabel stdClbl=new JLabel("Klasse C");
		JSpinner standardPreisASpinner=new JSpinner(new SpinnerNumberModel(0.00,0.00,10000.00,0.5));
		JSpinner standardPreisBSpinner=new JSpinner(new SpinnerNumberModel(0.00,0.00,10000.00,0.5));
		JSpinner standardPreisCSpinner=new JSpinner(new SpinnerNumberModel(0.00,0.00,10000.00,0.5));
		JPanel stdGebuehrenPanel=new JPanel(new GridBagLayout());
		stdGebuehrenPanel.setBorder(BorderFactory.createTitledBorder("Standard gebühren"));
		stdGebuehrenPanel.add(stdAlbl,     				new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(standardPreisASpinner,    new GridBagConstraints(2, 1, 1, 1, 0.1, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(waehrungslbl,     		new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(stdBlbl,     				new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(standardPreisBSpinner,    new GridBagConstraints(2, 2, 1, 1, 0.1, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(waehrungslbl,     		new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(stdClbl,     				new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(standardPreisCSpinner,    new GridBagConstraints(2, 3, 1, 1, 0.1, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		stdGebuehrenPanel.add(waehrungslbl,     		new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
		panel.add(stdGebuehrenPanel,     		new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0));
		return panel;
	}
}
 
Welchen Teil der Kausalkette man da jetzt nennen soll? .... Ein Label hat die methode "getBounds" - wenn das Label drei mal erscheinen würde, welches ergebnis sollte diese Methode dann zurückgeben?

Oder anders: Man kann nicht dieselbe Component mehrfach "instantiieren"....
 
Stimmt, daran hatte ich garnicht gedacht. Bleibt wohl nichts anderes übrig als entsprechend viele Componenten zu erzeugen ...
 
Bei eine Label ist das ja wurscht. Wenn es eine "komplexere" Component ist (ein Jpanel mit vielen unterkomponenten drauf) macht man sich halt eine Methode dafür....
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben