嘿,我有以下 Java 代码,其中只有一些 swing JLabel、JPanel 和 JScrollPane。
我似乎无法让窗格内的标签 (546211) 导致滚动垂直激活。现在,随着我在里面添加标签并在场景外添加一些标签,它仍然不会滚动到那些标签。
但是当我运行它时,我无法滚动查看该标签:
如上图所示,我的卷轴在那里,但没有任何价值。卷轴应该在那里,因为我在场景外放了一个标签,我需要向下滚动才能看到它。
希望这只是我在 JScrollPane 中忽略的东西。或者我有什么问题?
我的java代码:
static JPanel panel;
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tester window = new tester();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public tester() {
initialize();
}
private void initialize() {
Image imgBG = new ImageIcon(this.getClass().getResource("/background.png")).getImage();
Image imgAvatar = new ImageIcon(this.getClass().getResource("/blank.png")).getImage();
panel = new JPanel();
panel.setLayout(null);
JLabel lblLabelBG = new JLabel("");
lblLabelBG.setBounds(10, 3, 635, 129);
lblLabelBG.setIcon(new ImageIcon(imgBG));
lblLabelBG.setBackground(new Color(204, 102, 0));
JLabel AvatarImg = new JLabel("");
AvatarImg.setIcon(new ImageIcon(imgAvatar));
AvatarImg.setBounds(5, 4, 100, 111);
panel.add(AvatarImg);
JLabel lblPH = new JLabel();
lblPH.setFont(new Font("Segoe UI", Font.BOLD, 25));
lblPH.setBounds(15, 110, 172, 22);
lblPH.setText("555-555-5555");
panel.add(lblPH);
JLabel lblRX = new JLabel();
lblRX.setFont(new Font("Segoe UI", Font.BOLD, 25));
lblRX.setBounds(55, 170, 157, 22);
lblRX.setText("546211");
panel.add(lblRX);
panel.add(lblLabelBG);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBackground(new Color(153, 153, 102));
scrollPane.setBounds(10, 11, 743, 156);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JFrame frame = new JFrame("");
frame.getContentPane().setBackground(new Color(240, 240, 240));
frame.getContentPane().setLayout(null);
frame.setSize(785, 291);
frame.getContentPane().add(scrollPane);
frame.setVisible(true);
}