2013-01-01 5 views
8

가능한 중복 :
What components should I use for building a Java WYSIWYG HTML editor자바 스윙 텍스트 편집기

나는 자바 프로그래밍에서 총 초보자입니다. 스윙/AWT에서 텍스트 편집기를 써야하고 그것에 대해 하나의 질문이 있습니다. 선택한 단어를 편집하려면 어떻게합니까? 예를 들어 색상을 변경하려면 어떻게해야합니까? 어떤 구성 요소와 어떤 기능을 사용해야합니까?

+2

입니다 : 자바에서 http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html – aly

+0

참조 [* WYSIWYG 텍스트 편집기 *] (http://stackoverflow.com/q/853071/230513). – trashgod

+0

AWT는 서식 첨부 텍스트를 지원하는 컴퍼넌트를 제공하지 않기 때문에, 「Swing」으로 간주 될 가능성이 있습니다. –

답변

10

Java Swing의 초보자는이 프로젝트를 간단하게 유지하십시오. 동일한 문서에서 여러 색상과 여러 크기를 표시하려면 복잡한 코딩 및 렌더링 html이 많이 필요합니다.

기본 복사, 잘라 내기, 붙여 넣기 기능을 구현하기가 쉽기 때문에 제공하십시오.

이러한 기능을 제공하려면 JTextArea이면 충분합니다.

시도해보십시오. 그것은이 튜토리얼을 살펴보십시오 매우 간단한 텍스트 편집기

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Document extends JFrame implements ActionListener 
{ 
private JTextArea ta; 
private int count; 
private JMenuBar menuBar; 
private JMenu fileM,editM,viewM; 
private JScrollPane scpane; 
private JMenuItem exitI,cutI,copyI,pasteI,selectI,saveI,loadI,statusI; 
private String pad; 
private JToolBar toolBar; 
public Document() 
{ 
    super("Document"); 
    setSize(600, 600); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container pane = getContentPane(); 
    pane.setLayout(new BorderLayout()); 

    count = 0; 
    pad = " "; 
    ta = new JTextArea(); //textarea 
    menuBar = new JMenuBar(); //menubar 
    fileM = new JMenu("File"); //file menu 
    editM = new JMenu("Edit"); //edit menu 
    viewM = new JMenu("View"); //edit menu 
    scpane = new JScrollPane(ta); //scrollpane and add textarea to scrollpane 
    exitI = new JMenuItem("Exit"); 
    cutI = new JMenuItem("Cut"); 
    copyI = new JMenuItem("Copy"); 
    pasteI = new JMenuItem("Paste"); 
    selectI = new JMenuItem("Select All"); //menuitems 
    saveI = new JMenuItem("Save"); //menuitems 
    loadI = new JMenuItem("Load"); //menuitems 
    statusI = new JMenuItem("Status"); //menuitems 
    toolBar = new JToolBar(); 

    ta.setLineWrap(true); 
    ta.setWrapStyleWord(true); 

    setJMenuBar(menuBar); 
    menuBar.add(fileM); 
    menuBar.add(editM); 
    menuBar.add(viewM); 

    fileM.add(saveI); 
    fileM.add(loadI); 
    fileM.add(exitI); 

    editM.add(cutI); 
    editM.add(copyI); 
    editM.add(pasteI);   
    editM.add(selectI); 

    viewM.add(statusI); 

    saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); 
    loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK)); 
    cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); 
    copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); 
    pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); 
    selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); 

    pane.add(scpane,BorderLayout.CENTER); 
    pane.add(toolBar,BorderLayout.SOUTH); 

    saveI.addActionListener(this); 
    loadI.addActionListener(this); 
    exitI.addActionListener(this); 
    cutI.addActionListener(this); 
    copyI.addActionListener(this); 
    pasteI.addActionListener(this); 
    selectI.addActionListener(this); 
    statusI.addActionListener(this); 

    setVisible(true); 
} 
public void actionPerformed(ActionEvent e) 
{ 
    JMenuItem choice = (JMenuItem) e.getSource(); 
    if (choice == saveI) 
    { 
     //not yet implmented 
    } 
    else if (choice == exitI) 
     System.exit(0); 
    else if (choice == cutI) 
    { 
     pad = ta.getSelectedText(); 
     ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd()); 
    } 
    else if (choice == copyI) 
     pad = ta.getSelectedText(); 
    else if (choice == pasteI) 
     ta.insert(pad, ta.getCaretPosition()); 
    else if (choice == selectI) 
     ta.selectAll(); 
    else if (e.getSource() == statusI) 
    { 
     //not yet implmented 
    } 
} 
public static void main(String[] args) 
{ 
    new Document(); 
}