0
我使用的是JScrollPane
容器,該容器中有一個JPanel
。 在JPanel裏面有很多標籤和JTextFields
。現在最後的JTextField不顯示在我想用TAB鍵得到它,並開始和有垂直滾輪滾動到它。如何自動滾動到可焦點JTextField的位置?
我試圖
DefaultCaret caret = (DefaultCaret) textField.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
也:
scrollRectToVisible(textField.getBounds());
'
沒有工作:(
這是代碼:
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InsertNewOrderWindow frame = new InsertNewOrderWindow();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InsertNewOrderWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 903, 1001);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel , JScrollPane.VERTICAL_SCROLLBAR_ALWAYS , JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(300, 577));
contentPane.add(scrollPane);
...... // gridBag stuff
textField = new JTextField();
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
DefaultCaret caret = (DefaultCaret) textField.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
}
});
... // gridBag stuff
panel.add(textField, gbc_textField_19);
...
} //InsertNewOrderWindow() method
我怎麼能自動滾動到可調焦的JTextField?