如何使用Java Swing
將滾動條添加到我的框架中?我有一個面板中的很多內容,並需要使其滾動,以不採取全屏幕的高度..Java Swing可滾動框架
我試過了folliwing代碼,但它只是增加了一個滾動條,沒有可滾動的內容
// add the panel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane(panel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Then, add the jScrollPane to your frame
frame.getContentPane().add(jScrollPane);
這是我的代碼:
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 500, 665);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
JTextPane textName = new JTextPane();
textName.setBounds(95, 36, 302, 20);
panel.add(textName);
JTextPane textNationality = new JTextPane();
textNationality.setBounds(95, 61, 302, 20);
panel.add(textNationality);
JTextPane textDate = new JTextPane();
textDate.setBounds(95, 86, 302, 20);
panel.add(textDate);
JTextPane textAddress = new JTextPane();
textAddress.setBounds(95, 164, 302, 20);
panel.add(textAddress);
JLabel lblWebsiteblog = new JLabel("Website/Blog");
lblWebsiteblog.setBounds(10, 244, 78, 23);
panel.add(lblWebsiteblog);
JTextPane textWebsite = new JTextPane();
textWebsite.setBounds(95, 239, 302, 20);
panel.add(textWebsite);
JLabel lblProfesional = new JLabel("Profesional Experience");
lblProfesional.setBounds(10, 267, 133, 23);
panel.add(lblProfesional);
JLabel lblEducationAndTraining = new JLabel("Education and Training");
lblEducationAndTraining.setBounds(10, 441, 133, 14);
panel.add(lblEducationAndTraining);
JTextArea textProfesional = new JTextArea();
textProfesional.setWrapStyleWord(true);
textProfesional.setBounds(40, 301, 384, 129);
panel.add(textProfesional);
JScrollPane scrollPane = new JScrollPane(textProfesional);
scrollPane.setBounds(40, 301, 384, 129);
panel.add(scrollPane);
JTextArea textEducation = new JTextArea();
textEducation.setBounds(40, 466, 384, 142);
panel.add(textEducation);
scrollPane_1 = new JScrollPane(textEducation);
scrollPane_1.setBounds(40, 466, 384, 142);
panel.add(scrollPane_1);
}
This - 'panel.setLayout(null);'將防止'JScrollPane'計算窗格的大小,這將阻止它確定何時應該顯示滾動條 – MadProgrammer