0
在下面的SSCCE中可以看到水平滾動條出現,即使可以使用垂直滾動條也是如此。這只是一個例子。我使用自己的LayoutManager而不是BoxLayout,但是如果你有解決方案,我可以在我自己的LayoutManager中使用它。當垂直滾動條可見時擺動BoxLayout水平滾動條
我已經嘗試過實現Scrollable的自定義JPanel,然後覆蓋TrackScrollableViewportWidth並返回true,但沒有得到我想要的。那時文本在右邊被切斷了。
另一件事,那不是那麼重要,但仍然讓我惱火:如果增加窗口的寬度,水平滾動條就會消失,JTextArea的寬度也會增加。但是,如果您減小窗口的寬度,則JTextArea不會相應地調整其寬度。
在此先感謝。這裏是SSCCE:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class MyFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(final String[] args)
{
new MyFrame();
}
private MyFrame()
{
setTitle("Scrollbars and width");
setSize(800, 200);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel container = new JPanel();
final BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
final JTextArea area1 = new JTextArea();
area1.setText("1sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf ");
area1.setLineWrap(true);
area1.setWrapStyleWord(true);
area1.setEditable(false);
area1.setForeground(Color.RED);
container.add(area1);
final JTextArea area2 = new JTextArea();
area2.setText("2sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf ");
area2.setLineWrap(true);
area2.setWrapStyleWord(true);
area2.setEditable(false);
area2.setBackground(Color.RED);
container.add(area2);
final JScrollPane scrollbox = new JScrollPane(container);
this.getContentPane().add(scrollbox, BorderLayout.CENTER);
this.getContentPane().invalidate();
this.getContentPane().validate();
}
}