JLabel mit mehreren Zeilen?

failedC

Grünschnabel
Hi,

ich lade Informationen aus einer Datenbank und möchte sie
nun einfach anzeigen, ein Label ist einzeilig. Würde gerne
ein Text setzten können und ein Größe des Labels angeben
und dann soll er automatisch ein Zeilenumbruch machen.

Gibts ide Komponente?

Danke
 
Hallo failedC,

schau mal hier:

Code:
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class WrappedLabelExample extends JFrame{

	JTextArea textArea = new JTextArea();
	JLabel label = new JLabel("<html>Erste Zeile<br>Zweite Zeile<br>Dritte Zeile<html>");
	
	
	public WrappedLabelExample(){
		super("WrappedText");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(300,400);
		this.setLayout(new FlowLayout());
		
		
		this.add(label);
		
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setFont(label.getFont());
		textArea.setSize(label.getWidth(),label.getHeight());
		textArea.setText("Dies ist ein umgebrochener Satz.");
		textArea.setOpaque(false);
		this.add(textArea);
	}
	
	public static void main (String[] args){
		new WrappedLabelExample().setVisible(true);
	}	
}


Vg Erdal
 
Hallo,

für die einfache Wiederverwendung, könntest du eine Innere Klasse implementieren!

Java:
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class WrappedLabelExample extends JFrame {
	WrappedLabel l1 = new WrappedLabel("Dies ist ein umgebrochener Satz.");

	WrappedLabel l2 = new WrappedLabel(
			"Dies ist ein umgebrochener Satz. Dies ist ein umgebrochener Satz.");

	WrappedLabel l3 = new WrappedLabel(
			"Dies ist ein umgebrochener Satz. Dies ist ein umgebrochener Satz. Dies ist ein umgebrochener Satz.");

	public WrappedLabelExample() {
		super("WrappedLabel");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(300, 400);
		this.setLayout(new FlowLayout());

		this.add(l1);
		this.add(l2);
		this.add(l3);
	}

	public static void main(String[] args) {
		new WrappedLabelExample().setVisible(true);
	}

	class WrappedLabel extends JTextArea {
		JLabel label = new JLabel();

		public WrappedLabel(String text) {
			super(text);
			this.setEditable(false);
			this.setLineWrap(true);
			this.setWrapStyleWord(true);
			this.setFont(label.getFont());
			this.setOpaque(false);
		}
	}
}


Vg Erdal
 
Zuletzt bearbeitet:
Zurück