我有GridgabLayout JPanel。我在里面放了4个组件,3个JLabel和1个JTextArea。最后一行是JTextArea组件。这个GridegabLayout面板被添加到另一个JPanelfor中。 JTextArea中的文本存在问题:
1. Text in JTextArea cannot show it all.
2. If there are too many word, the first two row of labels cannot see.
在图片的右侧,我将文字改为多个单词。前两行消失了。
有我的GridgeabLayout JPanel代码: 包PDFAnnotationPackage;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class DivCommentJPane extends JPanel {
private BeanPanel thisBean;
private CommentItem thisComment;
private String pageErrorPrefix="Error On DivCommentJPane.java : ";
private PDFNotes bean;
private GridBagConstraints c;
public DivCommentJPane(CommentItem comment ,BeanPanel bean ){
//thisBean=bean;
super();
thisComment=comment;
thisBean=test;
setting();
}
public void setting(){
try{
bean=new PDFNotesBean();
this.setLayout( new GridBagLayout());
c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; // **** comment this line out to see effect ****
c.weighty = 1.0; // **** comment this line out to see effect ****
buildTopJPane();
setVisible(true);
}catch(Exception err){
Utility.DisplayErrorMsg(pageErrorPrefix+ err.getMessage().toString());
}
}
private JLabel setAttonIconCreator(){
Icon icon=getAnotIcon();
JLabel lblAnottIcon=new JLabel(thisComment.creator, icon, JLabel.CENTER);
return lblAnottIcon;
}
private JLabel setDeleteIcon(){
ImageIcon icon = new ImageIcon(getClass().getResource("Delete.gif"));
JLabel lblDeleteIcon=new JLabel("", icon, JLabel.CENTER);
lblDeleteIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
thisBean.annotRemove(thisComment.annotation, thisComment.pageNum);
}
});
return lblDeleteIcon;
}
private JLabel setPageDateLabel(){
JLabel lblDate=new JLabel(thisComment.toString());
return lblDate;
}
/*private JTextPane setContent(){
JTextPane pane = new JTextPane();
pane.setText(thisComment.content);
pane.setEditable(false);
return pane;
}*/
/*private JTextArea setContent(){
JTextArea t=new JTextArea();
t.setText(thisComment.content);
t.setLineWrap(true);
t.setEditable(false);
return t;
}*/
//modified the code like to use JScrollPane
private JScrollPane setContent(){
JScrollPane scroll=new JScrollPane();
scroll.setPreferredSize(new Dimension(250, 250));
JTextArea t=new JTextArea(3,120);
t.setText(thisComment.content);
t.setLineWrap(true);
t.setEditable(false);
scroll.getViewport().add(t);
return scroll;
}
private void buildTopJPane(){
JPanel lnFirst=new JPanel();
try{
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
this.add(setAttonIconCreator(), c);
c.gridx = 5;
c.gridy = 0;
this.add(setDeleteIcon(),c);
c.gridx=0;
c.gridy=1;
c.gridwidth=4;
this.add(setPageDateLabel(), c);
c.gridx=0;
c.gridy=2;
c.gridwidth=4;
this.add(setContent(), c);
} catch(Exception err){
Utility.DisplayErrorMsg(pageErrorPrefix+ err.getMessage().toString());
}
}
@SuppressWarnings("finally")
private SafeIcon getAnotIcon(){
SafeIcon antIcon=null;
try{
switch (thisComment.type.toLowerCase()){
case "underline":
antIcon= new SafeIcon( bean.getAnnotToolbar().getjbTextUnderline().getIcon());
break;
case "squiggly":
antIcon=new SafeIcon(bean.getjbTextSquiggly().getIcon());
break;
case "highlight":
antIcon=new SafeIcon(bean.getjbHighlight().getIcon());
break;
case "note":
antIcon=new SafeIcon(bean.getAnnotToolbar().getjbNote().getIcon());
default:
antIcon=new SafeIcon((Icon) ImageIO.read(getClass().getResource("SquigglyIcon.gif")));
return antIcon;
}
}catch(IOException err){
Utility.DisplayErrorMsg(pageErrorPrefix+ err.getMessage().toString());
}finally{return antIcon;}
}
}
这是在JScrollPane中添加JTextArea后的屏幕截图。