0
私のプログラムでは、私はこのMAINウィンドウとHELPウィンドウを持っています。 HELPウィンドウ(開かれているとき)は、焦点が合っているかどうかにかかわらず常に上にとどまります。しかし、私がrequestFocusInWindow()
のMAINウインドウ内のコンポーネントをHELPウインドウから起動するアクションリスナーで試してみると、問題は解決しません。バックグラウンドウィンドウのコンポーネントのフォーカスを維持するには?
これを実行する適切な方法は何ですか? TY :)
編集:要求されたよう
、ここで私が達成しようとしているものの短い例です。基本的には、メインウィンドウ内のTextFieldへのフォーカスをトリガーするには、HELPウィンドウ内のボタンが必要です。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void initGUI() {
mainFrame = new JFrame("Main");
helpFrame = new JFrame("Help");
mainFrame.setPreferredSize(new Dimension(500, 200));
helpFrame.setPreferredSize(new Dimension(500, 200));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
helpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new FlowLayout());
helpFrame.setLayout(new FlowLayout());
mainTextView = new JTextField("", 20);
mainButton = new JButton("Open Help");
helpButton = new JButton("Request Focus");
mainButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(mainButton)) {
helpFrame.pack();
helpFrame.setVisible(true);
}
}
});
helpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(helpButton))
System.out.println("Focus requested:" + mainTextView.requestFocusInWindow());
}
});
helpFrame.add(helpButton);
mainFrame.add(mainTextView);
mainFrame.add(mainButton);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGUI();
}
static JFrame mainFrame, helpFrame;
static JTextField mainTextView;
static JButton mainButton, helpButton;
}
ポストあなたの[SSCCE](http://sscce.org/)問題を示しています。 SSCCEではMAINフレームにはHELPフレームを表示するボタンがあり、ヘルプフレームにはメインフレームボタンにフォーカスを戻すボタンがあります。まずそれを働かせてください。 – camickr