尝试使用一个组合盒保存joptionpane,并在字符串中产生一个字符串,但它使我无法转换为字符串”。

发布于 2025-01-26 23:14:03 字数 564 浏览 4 评论 0原文

这是给我错误的代码:

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);
}                                    

here is the code that gives me error:

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);
}                                    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2025-02-02 23:14:03

joptionpane.showmessagedialog(...)不会返回任何内容,因此它返回void。没有什么(void)不能转换为字符串。您要使用方法showInputDialog。为了灵感,请查看(docs.oracle.com/javase.com/javase/tutorial /uiswing/组件/…

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();
            }
        });
    }
}
                

JOptionPane.showMessageDialog(...) does not return anything hence it returns void. And nothing (void) cannot be converted into a string. You want to use the method showInputDialog. For inspiration look at (docs.oracle.com/javase/tutorial/uiswing/components/….

A short executable hack to show the dialogs:

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();
            }
        });
    }
}
                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文