我有一个包含三个scrollPanes的简单程序。我设置它,以便滚动条始终可见。出于某种原因,滚动条仅在三个窗格中的两个窗格上可见,即使所有三个窗格的代码都相同。它们之间的唯一区别是第三个(有问题的窗格)位于边界布局的南部区域。
代码:
package SwingAssign2;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.GridLayout;
public class SwingAssign2 {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SwingAssign2 window = new SwingAssign2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SwingAssign2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelTop = new JPanel();
FlowLayout fl_panelTop = (FlowLayout) panelTop.getLayout();
fl_panelTop.setAlignment(FlowLayout.LEFT);
frame.getContentPane().add(panelTop, BorderLayout.NORTH);
//panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JComboBox comboBox = new JComboBox();
panelTop.add(comboBox);
comboBox.setPreferredSize(new Dimension(100, 20));
textField = new JTextField();
panelTop.add(textField);
textField.setColumns(20);
JPanel panelBottom = new JPanel();
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
panelBottom.setLayout(new BorderLayout(0, 0));
JLabel status = new JLabel("Status");
status.setHorizontalAlignment(SwingConstants.CENTER);
panelBottom.add(status, BorderLayout.SOUTH);
status.setBorder(BorderFactory.createLoweredBevelBorder());
JTextPane bText = new JTextPane();
JScrollPane jScrollPane3 = new JScrollPane();
jScrollPane3.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane3.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panelBottom.add(bText, BorderLayout.NORTH);
bText.setPreferredSize(new Dimension(200, 200));
//bText.setBorder(BorderFactory.createLoweredBevelBorder());
JPanel panelCenter = new JPanel();
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
panelCenter.setLayout(new GridLayout(0, 2, 0, 0));
JTextPane textPane_0 = new JTextPane();
JScrollPane jScrollPane1 = new JScrollPane();
jScrollPane1.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panelCenter.add(jScrollPane1);
JTextPane textPane_1 = new JTextPane();
JScrollPane jScrollPane2 = new JScrollPane(textPane_1);
jScrollPane2.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panelCenter.add(jScrollPane2);
}
}
答案 0 :(得分:2)
因为一个愚蠢的错误。
panelBottom.add(bText, BorderLayout.NORTH);
应该是
panelBottom.add(jScrollPane3, BorderLayout.NORTH);