개발바닥곰발바닥
728x90
<java />
//줄바꿈시 가운데 정렬 기능을 위해 JTextPane 사용 JTextPane tpName = new JTextPane(); tpName.setEditable(false); tpName.setText(goodsName); //tpName의 styleDocument를 가져와 가운데 정렬 설정 StyledDocument doc = tpName.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); add(tpName);

처음에는 텍스트를 표시할 때 JLabel을 사용하려 했는데 텍스트가 길어질시 자동으로 줄바꿈 해주는 기능이 필요해

JTextArea를 사용했는데 setLineWrap 메소드를 이용하면 줄바꿈은 잘 되지만 줄바꿈된 텍스트가 가운데 정렬이 안되서 결국 JTextPane을 사용하게 됐다. JTextPane은 따로 설정없이 자동으로 줄바꿈이 가능하고, StyleDocument를 이용해서

텍스트를 가운데 정렬하니 잘 작동한다.

 

728x90

'JAVA > Swing' 카테고리의 다른 글

JTable에 JCheckBox 넣기  (0) 2021.05.18
profile

개발바닥곰발바닥

@bestinu

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!