2012-09-29 3 views
2

많은 고된 작업을 수행 한 후 제 클릭 클릭이 올바르게 작동합니다. 슬프게도, JTextPane의 형식을 "text/html"으로 변경하고 JTextPane에 텍스트를 추가하면 버튼이 사라집니다. 나는이 거친 여주인과 거의 다 끝났어. 아무도 도와 줄 수 있습니까?"text/html"로 설정된 경우 jtextpane에 구성 요소를 포함 할 수 없습니다.

코드 다음은 ...

import java.awt.*; 
import javax.swing.*; 
import java.awt.Color; 
import javax.swing.JTextPane; 
import javax.swing.JButton; 
import java.applet.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

public class jlabeltest extends Applet { 

    public void init() { 

     jlabeltest textPaneExample = new jlabeltest(); 
     textPaneExample.setSize(550, 300); 
     textPaneExample.setVisible(true); 
    } 


    public jlabeltest() { 

     JTextPane textPane = new JTextPane(); 
     textPane.setContentType("text/html"); 
     InlineB button = new InlineB("Button");  
     textPane.setText("<p color='#FF0000'>Cool!</p>"); 
     button.setAlignmentY(0.85f); 

     button.addMouseListener(new MouseAdapter() { 

     public void mouseReleased(MouseEvent e) { 

     if (e.isPopupTrigger()) { 

      JOptionPane.showMessageDialog(null,"Hello!"); 
      // Right Click 
     } 

     if (SwingUtilities.isLeftMouseButton(e)) { 

      JOptionPane.showMessageDialog(null,"Click!"); 
      // Left Click 
     } 
    } 
    }); 
     textPane.insertComponent(button); 
     this.add(textPane); 
    } 
} 
+0

HTML을 사용하지 않고 기본 모드로 유지해야합니까? – Confident

+0

'JTextPane' /'JEditorPane '에 대해 [camickr] (http://stackoverflow.com/users/131872/camickr) 및 [StanislavL] (http://stackoverflow.com/users/301607/stanislavl)의 게시물을 확인하시기 바랍니다. ' – mKorbel

+0

http://java-sl.com/custom_tag_html_kit.html은 버튼 – StanislavL

답변

3

추가 구성 요소 (this 게시물을 참조)이 콘텐츠 형식에 일부 문제가있는 것 같다,하지만 당신은 다음과 같은 것을 시도 할 수 :

JTextPane textPane = new JTextPane(); 
    JButton button = new JButton("Button");  
    button.setAlignmentY(0.85f); 

    HTMLEditorKit kit = new HTMLEditorKit(); 
    HTMLDocument doc = new HTMLDocument(); 
    textPane.setEditorKit(kit); 
    textPane.setDocument(doc); 

    try { 
     kit.insertHTML(doc, doc.getLength(), "<p color='#FF0000'>Cool!", 0, 0, HTML.Tag.P); 
     kit.insertHTML(doc, doc.getLength(), "<p></p>", 0, 0, null); 
    } catch (BadLocationException ex) { 
    } catch (IOException ex) { 
    } 
+0

덕분에 맞춤 태그를 추가하는 방법을 보여줍니다! 나는 이것이 그것을 할 것이라고 생각한다. – Confident

2

JEditorPane에 텍스트를 추가 한 다음 JEditorPane의 HTML 내에 포함 된 구성 요소를 추가 할 때 문제가 발생합니다.

우리가이 실행되는 버튼을 클릭하면

import java.awt.Color; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.*; 

public class Test { 

    public static void main(String[] args) throws Exception { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test().createAndShowUI(); 
      } 
     }); 
    } 

    private void createAndShowUI() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     initComponents(frame); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private void initComponents(JFrame frame) { 
     final JTextPane editorPane = new JTextPane(); 
     editorPane.setSelectedTextColor(Color.red); 

     //set content as html 
     editorPane.setContentType("text/html"); 
     editorPane.setText("<p color='#FF0000'>Cool!</p>"); 

     //added <u></u> to underlone button 
     final InlineB label = new InlineB("<html><u>JLabel</u></html>"); 

     label.setAlignmentY(0.85f); 

     label.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       //added check for MouseEvent.BUTTON1 which is left click 
       if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) { 
        JOptionPane.showMessageDialog(null, "Hello!"); 
        String s = editorPane.getText(); 
        System.out.println(s); 
       } 
      } 
     }); 

     editorPane.insertComponent(label); 
     frame.getContentPane().add(editorPane); 
    } 
} 

class InlineB extends JButton { 

    public InlineB(String caption) { 
     super(caption); 
     setBorder(null);//set border to nothing 
    } 
} 

: println 메소드 (들)의

//added check for MouseEvent.BUTTON1 which is left click 
       if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) { 
        JOptionPane.showMessageDialog(null, "Hello!"); 
        String s = editorPane.getText(); 
        System.out.println(s); 
       } 

결과입니다 :

<html> 
    <head> 

    </head> 
    <body> 
    <p color="#FF0000"> 
     Cool! 
     <p $ename="component"> 

    </p> 
    </body> 
</html> 

당신이 할 수 0 여기

은 예입니다 구성 요소가 HTML에 포함되어 있으면 setText()을 호출하면이 내용이 지워집니다.

유일한 다른 가능한 솔루션 (Rempelos 외에 그에게 +1)이 너무 :

 @Override 
     public void mouseReleased(MouseEvent e) { 
      //added check for MouseEvent.BUTTON1 which is left click 
      if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) { 

       JOptionPane.showMessageDialog(null, "Hello!"); 
       String s = editorPane.getText(); 
       System.out.println(s); 

       editorPane.setText("<html><u>Hello</u></html>"); 
       //re add after call to setText 
       editorPane.insertComponent(label); 
      } 
     } 

을 더 나은 방법이지만 : 당신과 같이 setText()를 호출 한 후

JEditorPane에 구성 요소를 다시 추가 JEditorPane 클래스를 확장하여 setText() 메서드가 해당 구성 요소/버튼을 자동으로 추가하도록 할 수 있습니다.