我已经为我的JTextPane
添加了一些组件,所有内容都打印得很好但是当我想打印更多时,我的JScrollPane
没有出现,所以我可以向下滚动查看其他组件。
int p=0;
int y1=0;
int y2=20;
int i=0;
while (i< 10){
JLabel date= new JLabel();
date.setSize(100, 21);
date.setText("date posted");
JLabel username= new JLabel();
username.setSize(100,21);
username.setText("username");
JLabel picture= new JLabel();
picture.setSize(120, 120);
try {
ImageIcon ii=new ImageIcon(scaleImage(120, 120, ImageIO.read(new
File(System.getProperty("user.dir")+"\\ProfilePicture.png"))));
picture.setIcon(ii);
} catch (Exception ex) {
ex.printStackTrace();
}
JTextArea caption= new JTextArea();
caption.setText("caption here...");
caption.setLineWrap(true);
caption.setEditable(false);
caption.setSize(100, 100);
JScrollPane CaptionScroll = new JScrollPane (caption,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
CaptionScroll.setSize(100,100);
JTextArea comments= new JTextArea();
comments.setText("comments here...");
comments.setLineWrap(true);
comments.setSize(100, 100);
JScrollPane CommentScroll = new JScrollPane (comments,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
CommentScroll.setSize(100,100);
JButton CommentButton= new JButton();
CommentButton.setText("Submit");
CommentButton.setSize(100, 21);
CommentButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
//do stufff
}
});
likes=0;
final JLabel likeLabel= new JLabel();
likeLabel.setText("LIKES: "+ Integer.toString(likes));
likeLabel.setSize(100,21);
final JButton LikeButton= new JButton();
LikeButton.setText("Like");
LikeButton.setSize(100, 21);
LikeButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
if (liked == false) {
likes++;
likeLabel.setText("LIKES: "+ Integer.toString(likes));
LikeButton.setText("UnLike");
liked=true;
}
else{
likes--;
likeLabel.setText("LIKES: "+ Integer.toString(likes));
LikeButton.setText("Like");
liked=false;
}
}
});
DMInboxOutputTextPane.add(username, p).setLocation(125, y1);
p++;
DMInboxOutputTextPane.add(CaptionScroll,p).setLocation(120, y2);
p++;
DMInboxOutputTextPane.add(CommentButton,p).setLocation(230,y1);
p++;
DMInboxOutputTextPane.add(CommentScroll, p).setLocation(230, y2);
p++;
DMInboxOutputTextPane.add(LikeButton,p).setLocation(340, y1);
p++;
DMInboxOutputTextPane.add(likeLabel,p).setLocation(340, y2);
p++;
DMInboxOutputTextPane.add(date,p).setLocation(450, y1);
p++;
DMInboxOutputTextPane.add(picture, p).setLocation(0, y1);
p++;
DMInboxOutputTextPane.repaint();
i++;
y1=y1+130;
y2=y2+130;
}
我可以打印我想要打印的三个对象,但之后其他所有内容都会被切断,并且不会出现scrollPane。
答案 0 :(得分:0)
通常,对于这类事情,您可以在使用垂直BoxLayout布局的JPanel中添加组件。类似的东西:
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
您可以将窗格设置为可滚动:
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(pane);
鉴于您的用例,您可能需要实现Scrollable接口以获得对scrollPane的更多控制:
public static class VerticalScrollablePane extends JPanel implements Scrollable {
public VerticalScrollablePane(LayoutManager layout) {super(layout);}
public VerticalScrollablePane() {}
@Override
public Dimension getPreferredScrollableViewportSize() {return getPreferredSize();}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
switch(orientation) {
case SwingConstants.VERTICAL:
return visibleRect.height / 10;
case SwingConstants.HORIZONTAL:
return visibleRect.width / 10;
default:
throw new IllegalArgumentException("Invalid orientation: " + orientation);
}
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
switch(orientation) {
case SwingConstants.VERTICAL:
return visibleRect.height;
case SwingConstants.HORIZONTAL:
return visibleRect.width;
default:
throw new IllegalArgumentException("Invalid orientation: " + orientation);
}
}
@Override
public boolean getScrollableTracksViewportWidth() {return true;}
@Override
public boolean getScrollableTracksViewportHeight() {return false;}
}
然后你会:
JPanel pane = new VerticalScrollablePane();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(pane);
希望它有所帮助!