无法自动 Select JOption 窗格 showInputDialog
Can't Automatically Select JOption Pane showInputDialog
所以我在主要方法的开头有一个这样的JOptionPane
:
JFrame frame = new JFrame("Input");
String progName = JOptionPane.showInputDialog(frame, "Name?");
但是,在开始输入之前,我需要手动点击弹出窗口。有什么方法可以让我 运行 程序时,它会自动 "select" 弹出,这样当我开始输入时它就会出现在文本框中。如果这不能用 JOptionPane
来完成,我可以使用其他替代方法,我只需要在考虑上述约束的情况下获取用户输入的字符串。
我创建了一个简单的示例,其中询问用户姓名的逻辑集中在一个方法中。此方法在应用程序一开始以及您每次单击按钮时调用。
这样,用户在应用程序启动时被迫输入数据,每次 he/she 希望更改输入的值。
public class Jf53136132 extends JFrame {
private static final long serialVersionUID = -3336501835025139522L;
private JPanel contentPane;
public Jf53136132() {
setTitle("Jf53136132");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JButton btnInvokeJoptionpane = new JButton("set some text on label");
panel.add(btnInvokeJoptionpane);
JLabel lblx = new JLabel("-x-");
panel.add(lblx);
getNewTextForLabel(lblx);
btnInvokeJoptionpane.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getNewTextForLabel(lblx);
}
});
}
private void getNewTextForLabel(JLabel label) {
String inputText = JOptionPane.showInputDialog("Enter the text for the label");
System.out.println("you entered <" + inputText + ">");
if (inputText != null && !inputText.trim().isEmpty()) {
label.setText(inputText);
}
}
}
注意一旦标签添加到内容窗格以及每次单击按钮时如何调用方法 getNewTextForLabel(...)。
此外,正如 VGR 正确指出的那样,最好不要 运行 主应用程序线程内的任何 Swing 代码。
您可以查看 java swing 教程 (here's a classic example)。
以下是一些示例代码,运行在单独的线程上设置框架。
public class Main {
private static void createAndShowGUI() {
Jf53136132 f = new Jf53136132();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(640, 480));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
void execute() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void main(String[] args) {
new Main().execute();
}
}
所以我在主要方法的开头有一个这样的JOptionPane
:
JFrame frame = new JFrame("Input");
String progName = JOptionPane.showInputDialog(frame, "Name?");
但是,在开始输入之前,我需要手动点击弹出窗口。有什么方法可以让我 运行 程序时,它会自动 "select" 弹出,这样当我开始输入时它就会出现在文本框中。如果这不能用 JOptionPane
来完成,我可以使用其他替代方法,我只需要在考虑上述约束的情况下获取用户输入的字符串。
我创建了一个简单的示例,其中询问用户姓名的逻辑集中在一个方法中。此方法在应用程序一开始以及您每次单击按钮时调用。
这样,用户在应用程序启动时被迫输入数据,每次 he/she 希望更改输入的值。
public class Jf53136132 extends JFrame {
private static final long serialVersionUID = -3336501835025139522L;
private JPanel contentPane;
public Jf53136132() {
setTitle("Jf53136132");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JButton btnInvokeJoptionpane = new JButton("set some text on label");
panel.add(btnInvokeJoptionpane);
JLabel lblx = new JLabel("-x-");
panel.add(lblx);
getNewTextForLabel(lblx);
btnInvokeJoptionpane.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getNewTextForLabel(lblx);
}
});
}
private void getNewTextForLabel(JLabel label) {
String inputText = JOptionPane.showInputDialog("Enter the text for the label");
System.out.println("you entered <" + inputText + ">");
if (inputText != null && !inputText.trim().isEmpty()) {
label.setText(inputText);
}
}
}
注意一旦标签添加到内容窗格以及每次单击按钮时如何调用方法 getNewTextForLabel(...)。
此外,正如 VGR 正确指出的那样,最好不要 运行 主应用程序线程内的任何 Swing 代码。 您可以查看 java swing 教程 (here's a classic example)。
以下是一些示例代码,运行在单独的线程上设置框架。
public class Main {
private static void createAndShowGUI() {
Jf53136132 f = new Jf53136132();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(640, 480));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
void execute() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void main(String[] args) {
new Main().execute();
}
}