2014-01-12 2 views
0

다음 스 니펫을 사용하여 대화방을 만듭니다.수정 된 JScrollPane의 JEditorPane 배경 이미지

 chatlog = new JEditorPane("text/html", "<html>") { 
     private static final long serialVersionUID = 1L; 

     protected void paintComponent(Graphics g) { 
      // set background green - but can draw image here too 
      g.setColor(new Color(0,0,0,0)); 
      g.fillRect(0, 0, getWidth(), getHeight()); 

      // uncomment the following to draw an image 
      Image img; 
      try { 
       img = ImageIO.read(new File("images/chatlog.png")); 
       g.drawImage(img, 0, 0, this); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      super.paintComponent(g); 
     } 
    }; 

    chatlog.setFont(new Font("Segoe UI", Font.PLAIN, 14)); 
    chatlog.setEditable(false); 
    chatlog.setOpaque(false); 

    chatScroll = new JScrollPane(chatlog); 
    chatScroll.setBounds(4, 454, 620, 100); 
    chatScroll.setOpaque(false); 
    chatScroll.setBackground(new Color(0,0,0,0)); 
    chatScroll.setBorder(BorderFactory.createEmptyBorder()); 
    chatScroll.setVisible(false); 

JEditorPane에서 배경 이미지를 사용합니다. 그러나 많은 내용을 채팅 로그에 쓰면 배경 이미지가 텍스트와 함께 위로 이동합니다.

도와 주시겠습니까?

+0

관련이없는

enter image description here는 : 적, 구성 요소의 수동 크기/위치 결정을하지 않는다 - 그 LayoutManager에의 독점적 인 책임입니다. – kleopatra

+0

관련 : 예, 편집기의 맨 위에 그릴 때 다른 작업을 수행해야합니다 :-) 전체 배율을 채우기 위해 배율을 조정하거나 (약간 이상하게 보일 수도 있음) viewPort의 배경으로 칠하십시오. – kleopatra

답변

2

표준 HTML에서 우리는 background-attachment: scroll; CSS 속성을 사용하여 사용자가 페이지를 스크롤하면서 BG가 페이지로 스크롤되도록합니다. 노란색 도트는 Swing이 그 속성을 어떻게 구현하지 않는지 보여주기 위해 배경으로 스타일을 넣습니다.

반면에 넓은 검은 색 이미지는 스크롤 창 자체에서 그려 지므로 어디에서나 그려야합니다. enter image description here enter image description here

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class BackgroundImageInHTML { 

    // the GUI as seen by the user (without frame) 
    private JPanel gui; 
    private final static String HTML = "<html>" 
      + "<head>" 
      + "<style type=text/css>" 
      + "body {" 
      + " background-image: http://i.stack.imgur.com/IHARa.png;" 
      + " background-repeat:no-repeat;" 
      + " background-position:left top;" 
      + " background-attachment: scroll;" 
      + " color: #BBBBBB;" 
      + "}" 
      + "</style>" 
      + "</head>" 
      + "<body>" 
      + "<h1>Heading 1</h1>"; 
    private final String PARAGRAPH = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus."; 

    BackgroundImageInHTML() { 
     initializGui(); 
    } 

    public void initializGui() { 
     gui = new JPanel(new BorderLayout()); 
     gui.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     // TODO - not ideal 
     gui.setPreferredSize(new Dimension(400, 100)); 

     StringBuilder sb = new StringBuilder(); 
     sb.append(HTML); 
     for (int ii = 0; ii < 10; ii++) { 
      sb.append("<h2>Header 2</h2>"); 
      sb.append(PARAGRAPH); 
     } 
     JEditorPane jep = new JEditorPane(); 
     jep.setOpaque(false); 
     jep.setContentType("text/html"); 
     jep.setText(sb.toString()); 
     JScrollPane jsp = new JScrollPane(jep) { 
      BufferedImage bg = new BufferedImage(350,50,BufferedImage.TYPE_INT_RGB); 
      @Override 
      public void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.drawImage(bg, 0, 0, this); 
      } 
     }; 
     jsp.getViewport().setOpaque(false); 
     gui.add(jsp); 
    } 

    public JComponent getGui() { 
     return gui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 

       BackgroundImageInHTML bih = new BackgroundImageInHTML(); 

       JFrame f = new JFrame("Demo"); 
       f.add(bih.getGui()); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
}