Text Unterstreichen/Fett/Kursiv/Font ändern?

Status
Nicht offen für weitere Antworten.

venomlol

Mitglied
hi ho

hab nen neues problem ^^ und zwar will ich einen kleinen editor schreiben . nun will ich das man den text unterstreichen/fett/kursiv und dazu die font ändern kann .

aber ich komm absolut nich drauf wie ;( . hab schon gegoogelt bis zum erbrechen aber find nix gutes :shock: .

Java:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.StyledEditorKit.BoldAction;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLEditorKit.HTMLTextAction;


public class Stream extends JFrame implements ActionListener, WindowListener{
	
	JTextPane ta = new JTextPane();
	JMenuBar mb1;
	JMenu file;
	JMenu farbe;
	JMenu schrift;
	JMenu menu;
	JButton Bold,Italic,Underline,left,center,right,red,green,black;
	JTextPane tp = new JTextPane();

	JMenuItem load,save,exit,neu,rote,gruene,schwarze,serif,sanserif,monospaced;
	String hilf = new String ();

	public Stream(String s){
		super(s);
		this.setSize(600,650);
		this.addWindowListener(this);
		mb1 = new JMenuBar();		
		file = new JMenu("Datei");
		farbe = new JMenu("Farbe");
		schrift = new JMenu("Schriftart");
		ImageIcon icon1 = new ImageIcon("neu.gif");
		
		JMenuItem neu = new JMenuItem("Neu", icon1);
		neu.setBackground(Color.WHITE);
		ImageIcon icon2 = new ImageIcon("open.gif");
		load = new JMenuItem("Laden",icon2);
		load.setBackground(Color.WHITE);
		ImageIcon icon3 = new ImageIcon("save.gif");
		save = new JMenuItem("Speichern",icon3);
		save.setBackground(Color.WHITE);
		exit = new JMenuItem("Beenden");
		exit.setBackground(Color.WHITE);
		ImageIcon icon6 = new ImageIcon("bold.gif");
		Bold = new JButton("",icon6);
		ImageIcon icon4 = new ImageIcon("italic.gif");
		Italic = new JButton("",icon4);
		ImageIcon icon5 = new ImageIcon("underline.gif");
		Underline = new JButton("",icon5);
		ImageIcon icon7 = new ImageIcon("left.gif");
		left = new JButton("",icon7);
		ImageIcon icon8 = new ImageIcon("center.gif");
		center = new JButton("",icon8);
		ImageIcon icon9 = new ImageIcon("right.gif");
		right = new JButton("",icon9);
		ImageIcon icon10 = new ImageIcon("green.gif");
		green = new JButton("",icon10);
		ImageIcon icon11 = new ImageIcon("red.gif");
		red = new JButton("",icon11);
		ImageIcon icon12 = new ImageIcon("black.gif");
		black = new JButton("",icon12);
		ImageIcon icon15 = new ImageIcon("red.gif");
		rote = new JMenuItem("Rot",icon15);
		rote.setBackground(Color.WHITE);
		ImageIcon icon13 = new ImageIcon("black.gif");
		schwarze = new JMenuItem("Grün",icon13);
		schwarze.setBackground(Color.WHITE);
		ImageIcon icon14 = new ImageIcon("green.gif");
		gruene = new JMenuItem("Schwarz",icon14);
		gruene.setBackground(Color.WHITE);
		serif = new JMenuItem("Serif");
		serif.setBackground(Color.WHITE);
		sanserif = new JMenuItem("San Serif");
		sanserif.setBackground(Color.WHITE);
		monospaced = new JMenuItem("Monospaced");
		monospaced.setBackground(Color.WHITE);

		//styleBox = new StyleBox();
		
		 
		
		setJMenuBar(mb1);
		file.add(neu);
		file.add(load);
		file.add(save);
		file.addSeparator();
		file.add(exit);
		mb1.add(file);
		farbe.add(rote);
		farbe.add(gruene);
		farbe.add(schwarze);
		schrift.add(serif);
		schrift.add(sanserif);
		schrift.add(monospaced);

		mb1.add(file);
		mb1.add(file);
		mb1.add(farbe);
		mb1.add(schrift);
		mb1.add(Bold);
		mb1.add(Italic);
		mb1.add(Underline);
		mb1.add(left);
		mb1.add(center);
		mb1.add(right);
		mb1.add(file);



		
		neu.addActionListener(this);
		load.addActionListener(this);
		save.addActionListener(this);
		exit.addActionListener(this);
		
		center.addActionListener(new HTMLEditorKit.AlignmentAction("zentrieren",StyleConstants.ALIGN_CENTER));
		left.addActionListener(new HTMLEditorKit.AlignmentAction("linksbündig",StyleConstants.ALIGN_LEFT));
		right.addActionListener(new HTMLEditorKit.AlignmentAction("rechtsbündig",StyleConstants.ALIGN_RIGHT));

		//menu.add(getActionByName(DefaultEditorKit.cutAction));
		
	    rote.addActionListener(new HTMLEditorKit.ForegroundAction("Farbe", Color.red));
	    gruene.addActionListener(new HTMLEditorKit.ForegroundAction("Farbe", Color.green));
	    schwarze.addActionListener(new HTMLEditorKit.ForegroundAction("Farbe", Color.black));



	    
	    
		this.add(ta);
		this.setVisible(true);
		this.add(tp);
	}




	
	public static void main(String args[]){
		Stream g = new Stream("Editor");
	}

	
//	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==load){
			ta.setText("");
			readin();
		}
		
		if(e.getSource()==save){
			writeout();
		}
		
		if(e.getSource()==neu){
			ta.setText("");
		}
		
		if(e.getSource()==exit){
			hilf=ta.getText();
		}
		
		if(e.getSource()==Bold){
			hilf=ta.getText();
		}
		
		if(e.getSource()==serif){
			ta.setForeground(Color.GREEN);

		}
		
		if(e.getSource()==Underline){
			hilf=ta.getText();
		}
		
		
		
		if(e.getSource()==left){
			System.exit(0);
		}
		
		
		
		if(e.getSource()==right){
			tp = new JTextPane();
			
			SimpleAttributeSet attribs = new SimpleAttributeSet();
			StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_RIGHT);
			tp.setParagraphAttributes(attribs,true);
		}
		
	}

	
	public void writeout(){
		FileDialog fd2 = new FileDialog (this,"Speichern",FileDialog.SAVE);
		fd2.setVisible(true);
		File f = new File(fd2.getDirectory()+fd2.getFile());
		FileOutputStream f1 = null;
		try {  
			f1 = new FileOutputStream(f);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(f1));			
		String line = new String();
		line=ta.getText();
		try {
			out.append(line);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	public void readin(){
		FileDialog fd1 = new FileDialog (this,"Laden",FileDialog.LOAD);
		fd1.setVisible(true);
		File f = new File(fd1.getDirectory()+fd1.getFile());
		FileInputStream f1 = null;
		try {
			f1 = new FileInputStream(f);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(f1));			
		String line = new String();
		while(line!=null){
		try {
			line = in.readLine();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	//	ta.append(line);
	//	ta.append("\n");
		}
		try {
			in.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}


lg VenomLoL
 
Wie wärs mit setFont? Ich weiss nicht genau, ob du da Farbe und so einstellen kannst, aber auf jeden Fall den Font ansich.
 
Schlagwörter (Damit hab ichs gelöst): setDocument, StyledDocument, StyleContext, MutableAttributeSet, Style, StyleConstants-Klasse, JEditorPane (JTextPane erbt davon)

glaub das wars so grob ^^
 
sooo ich danke erstmal allen für die denk anstöße 😉 ich werd mich mal damit auseinandersetzen und falls probleme auftreten diese posten 🙂

lg venomlol
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben