尝试将带有组合框的 JOptionPane 保存为字符串,但它给了我 "void cannot be converted to string"

tried saving a JOptionPane with a combo box result in a String, but it gives me "void cannot be converted to string"

这是给我错误的代码:

private void btnLLamada1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "numero de minutos"));
    String[] lista = {"Local", "Internacional", "Celular"};
    JComboBox optionList = new JComboBox(lista);
    optionList.setSelectedIndex(0);
    tp = JOptionPane.showMessageDialog(null, optionList, "Que tipo de llamada va a usar",
            JOptionPane.QUESTION_MESSAGE);
}                                    

JOptionPane.showMessageDialog(...) 没有 return 任何东西因此它 returns void。没有什么(void)不能转换成字符串。您要使用 showInputDialog 方法。如需灵感,请查看 (docs.oracle.com/javase/tutorial/uiswing/components/….

用于显示对话框的简短可执行 hack:

import java.awt.*;
import javax.swing.*;

public class Test {

    void initGui() {
        int minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "Numero de minutos"));
        System.out.println("Numero de minutos: " + minutos);
        
        String[] lista = { "Local", "Internacional", "Celular" };
        JComboBox<String> optionList = new JComboBox<String>(lista);
        optionList.setSelectedIndex(0);
        String tipo = JOptionPane.showInputDialog(null, optionList, "Que tipo de llamada va a usar", JOptionPane.QUESTION_MESSAGE);
        System.out.println("tipo de llamada: " + tipo);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().initGui();
            }
        });
    }
}