Java-选择新的JTabbedPane时如何关注JTextArea [英] Java - how do I gain focus on JTextArea when selecting a new JTabbedPane
问题描述
我有一个带有多个JTabbedPane
的swing GUI;每个选项卡的顶部都包含两个JButtons
,然后是一个JTextArea
(供用户输入),并在底部包含一个JTextField
以得到结果.
I have a swing GUI with multiple JTabbedPane
s; each tab contains two JButtons
at the top, then a JTextArea
(for user input), and a JTextField
at the bottom for a result.
我的问题是,在切换选项卡后,如果不使用鼠标单击或使用键盘上的Tab键,就无法获得JTextArea
的焦点?
My problem is that I can't get the JTextArea
to gain focus after switching tabs without either clicking in it with the mouse or using the tab key on the keyboard?
我有...
frame.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
textArea_1.requestFocusInWindow();
...这在首次运行该应用程序时效果很好(第一个选项卡中的textArea具有焦点),但是当我切换到另一个选项卡窗格时,第一个按钮现在具有焦点而不是textArea,并且当我切换回去时到第一个标签上,textArea失去了焦点,第一个按钮又有了焦点.
...and this works well when the app is first run (the textArea in the first tab has focus) but when I switch to another tabbedpane the first button now has the focus instead of the textArea, and when I switch back to the first tab the textArea has lost focus and once again the first button has focus.
我尝试将requestFocus添加到每个textArea,并且尝试在每个textArea上带到最前面",并且搞砸了Focus Traversal,但是我似乎没有做任何事情使TextArea专注于标签更改?
I've tried adding a requestFocus to each textArea, and I've tried "Bring to front" on each textArea, and I've messed around with Focus Traversal but nothing I do seems to make the textArea gain focus on a tab change?
这让我难过了一个星期,所以将不胜感激地收到任何帮助吗?
This has had me stumped for a week so any help will be gratefully received?
推荐答案
向您的JTabbedPane添加一个ChangeListener.这是一般的想法:
Add a ChangeListener to your JTabbedPane. Here's the general idea:
[抱歉,因为我周围有一个旧存根,所以我使用了JTextFields而不是JTextAreas-但想法是相同的.]
[Sorry, I used JTextFields instead of JTextAreas since I had an old stub laying around - but the idea is the same.]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTabbedPaneDemo3 implements Runnable
{
JTextField txtFoo;
JTextField txtBar;
JTabbedPane tabbedPane;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new JTabbedPaneDemo3());
}
public void run()
{
txtFoo = new JTextField(10);
final JPanel pnlFoo = new JPanel();
pnlFoo.add(new JButton("Button 1"));
pnlFoo.add(new JLabel("Foo"));
pnlFoo.add(txtFoo);
txtBar = new JTextField(10);
final JPanel pnlBar = new JPanel();
pnlBar.add(new JButton("Button 3"));
pnlBar.add(new JLabel("Bar"));
pnlBar.add(txtBar);
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", pnlFoo);
tabbedPane.addTab("Tab 2", pnlBar);
tabbedPane.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
Component comp = tabbedPane.getSelectedComponent();
if (comp.equals(pnlFoo))
{
txtFoo.requestFocusInWindow();
}
else if (comp.equals(pnlBar))
{
txtBar.requestFocusInWindow();
}
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(460, 200);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
txtFoo.requestFocusInWindow();
}
}
这篇关于Java-选择新的JTabbedPane时如何关注JTextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!