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?