[英]Unable to gain focus on JTextField used in JDialog for the second time on linux server
我正在使用帶有2個JTextfields和JButtons的JDialog。 當Jdialog第一次打開時,我的第一個文本字段上有一個光標。 現在,如果我單擊Jdialog之外的某個地方,然后再嘗試單擊JDialog,則看不到任何文本字段上的光標,並且無法在文本字段上編寫或編輯任何內容。
我將模式設置為真實。 因此,即使我單擊其他任何位置,我的對話框仍會出現在屏幕上,但會被停用。
此問題僅適用於linux服務器,而不適用於Windows。 Windows完美運行。
在此處添加示例代碼以獲得該問題的圖片。 這里TestClass擴展了JFrame。
public TestClass(){
setSize(new Dimension(600,500));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
myPanel = new JPanel();
JButton openDialog = new JButton("Click here");
openDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
dialog = new JDialog(myFrame,true);
dialog.setSize(new Dimension(400,400));
JTextField myField = new JTextField(10);
JPanel innerPanel = new JPanel();
innerPanel.add(myField);
dialog.add(innerPanel);
dialog.setVisible(true);
//add(dialog);
}
});
myPanel.add(openDialog);
add(myPanel);
}
您不完整的示例無法測試。 下面的示例展示了Mac OS X和Ubuntu 12在Java 6下的預期行為。特別是,文本字段在從另一個程序切回后仍保持功能。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @see http://stackoverflow.com/a/15576897/230513
*/
public class TestClass extends JFrame {
private static JFrame myFrame;
public TestClass() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton openDialog = new JButton("Click here");
JPanel myPanel = new JPanel();
myPanel.add(new JButton(new AbstractAction("Click here") {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(myFrame, true);
JTextField myField = new JTextField(10);
JPanel innerPanel = new JPanel();
innerPanel.add(myField);
dialog.add(innerPanel);
dialog.pack();
dialog.setSize(new Dimension(160, 120));
dialog.setLocationRelativeTo(myFrame);
dialog.setVisible(true);
}
}));
add(myPanel);
pack();
setSize(new Dimension(320, 240));
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
myFrame = new TestClass();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.