rührnössel
Grünschnabel
Wie macht man einen Text in einer JTextArea rechtsbünding, linksbünding, zentriert?
Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
rührnössel hat gesagt.:Wie macht man einen Text in einer JTextArea rechtsbünding, linksbünding, zentriert?
public void setComponentOrientation(ComponentOrientation o)
/*
* Created on 31.01.2005@19:15:41
*
* TODO Licence info
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
/**
* @author Administrator
*
* TODO Explain me
*/
public class AlignmentExamle extends JFrame {
private JTextPane textPane;
private StyledDocument doc = new DefaultStyledDocument();
private JPanel controlPanel = new JPanel() {
{
final JButton btnLeftAlignment = new JButton("left");
final JButton btnRightAlignment = new JButton("right");
final JButton btnCenterAlignment = new JButton("center");
final SimpleAttributeSet as = new SimpleAttributeSet();
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
int start = textPane.getSelectionStart();
int len = textPane.getSelectionEnd() - start;
if (src == btnLeftAlignment) {
StyleConstants.setAlignment(as,
StyleConstants.ALIGN_LEFT);
System.out.println("left");
} else if (src == btnRightAlignment) {
StyleConstants.setAlignment(as,
StyleConstants.ALIGN_RIGHT);
System.out.println("right");
} else if (src == btnCenterAlignment) {
StyleConstants.setAlignment(as,
StyleConstants.ALIGN_CENTER);
System.out.println("center");
}
doc.setParagraphAttributes(start, len, as, false);
}
};
btnLeftAlignment.addActionListener(action);
btnRightAlignment.addActionListener(action);
btnCenterAlignment.addActionListener(action);
add(btnLeftAlignment, BorderLayout.WEST);
add(btnCenterAlignment, BorderLayout.CENTER);
add(btnRightAlignment, BorderLayout.EAST);
}
};
public AlignmentExamle() {
super("AlignmentExamle");
setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
doc.insertString(0, "tutorials.de", SimpleAttributeSet.EMPTY);
} catch (BadLocationException e) {
e.printStackTrace();
}
textPane = new JTextPane(doc);
textPane.setEditable(false);
Container c = getContentPane();
c.add(controlPanel, BorderLayout.SOUTH);
c.add(textPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
new AlignmentExamle();
}
}
rührnössel hat gesagt.:danke, wisst ihr wie man die größe des JTextPane ändern kann?
so gehts nicht JTextPane(30,20)
rührnössel hat gesagt.:danke! aber wenn ich das jetzt so machen
text.setPreferredSize(300,300);
text.setMinimumSize(300,300);
text.setMaximumSize(10,10);
dann bekomme ich drei mal diese fehlermeldung:
setPreferredSize(java.awt.Dimension) in javax.swing.JComponent cannot be applied to (int,int)
text.setPreferredSize(300,300);
text.setPreferredSize(300,300);
text.setMinimumSize(300,300);
text.setMaximumSize(300,300);
text.setPreferredSize(300,300);
text.setMinimumSize(10,10);
text.setMaximumSize(300,300);