Java - el componente <string> no me permite compilar

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 4 puestos en Java (en relación al último mes)
Gráfica de Java

el componente <string> no me permite compilar

Publicado por Martin (3 intervenciones) el 23/05/2021 15:58:12
Yo posee este programa basico para vender computadora pero necesito utlizar una combobox para una de las opciones de personalizacion y debo utilizar una version de java mas antigua, si tienen alguna solucion se los agradeceria.

**No estoy permitido a cambiar el programa o actualizarlo**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package parciall;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
 
public class Ventapc {
 
    private JFrame frmVentaPc;
    private JComboBox<String> micro;
    private JRadioButton asusbutton;
    private JRadioButton gigabutton;
    private JRadioButton msibutton;
    private JRadioButton twoGB;
    private JRadioButton fourGB;
    private JRadioButton eightGB;
    private JRadioButton sixteenGB;
    private JCheckBox chkMonitor;
    private JCheckBox chkFixedDisk;
    private JTextField textField;
    private final ButtonGroup PlacaMadre = new ButtonGroup();
    private final ButtonGroup Memoria = new ButtonGroup();
 
    private final String micros[] = {"Intel", "Athlon", "Turion"};
    // Prices
    private final int intel = 150;
    private final int athlon = 80;
    private final int turion = 120;
    private final int asus = 75;
    private final int giga = 320;
    private final int msi = 100;
    private final int twogb = 50;
    private final int fourgb = 80;
    private final int eightgb = 130;
    private final int sixteengb = 200;
    private final int monitor = 250;
    private final int discofijo = 80;
    private int precioTotal = 0;    // Total Price
 
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Ventapc window = new Ventapc();
                    window.frmVentaPc.setVisible(true);
                    window.frmVentaPc.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    public Ventapc() {
        initialize();
    }
 
    private void initialize() {
        frmVentaPc = new JFrame();
        frmVentaPc.setTitle("Venta de PC");
        frmVentaPc.setBounds(100, 100, 450, 310);
        frmVentaPc.setResizable(false);
        frmVentaPc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmVentaPc.getContentPane().setLayout(null);
 
        JLabel lblNewLabel = new JLabel("<html><font color=red><b>*</b></font> Micro Type:</html>");
        lblNewLabel.setBounds(128, 15, 75, 22);
        frmVentaPc.getContentPane().add(lblNewLabel);
 
        JPanel placamadrepanel = new JPanel();
        placamadrepanel .setBorder(new LineBorder(new Color(0, 0, 0)));
        placamadrepanel .setBounds(120, 52, 280, 42);
        frmVentaPc.getContentPane().add(placamadrepanel );
 
        asusbutton = new JRadioButton("Asus");
        PlacaMadre.add(asusbutton);
        placamadrepanel .add(asusbutton);
 
        gigabutton = new JRadioButton("GigaByte");
        placamadrepanel .add(gigabutton);
        PlacaMadre.add(gigabutton);
 
        msibutton = new JRadioButton("MSI");
        placamadrepanel .add(msibutton);
        PlacaMadre.add(msibutton);
 
        JPanel memoriapanel = new JPanel();
        memoriapanel.setBorder(new LineBorder(new Color(0, 0, 0)));
        memoriapanel.setBounds(120, 111, 280, 42);
        frmVentaPc.getContentPane().add(memoriapanel);
 
 
        twoGB = new JRadioButton("2MB");
        memoriapanel.add(twoGB);
        Memoria.add(twoGB);
 
        fourGB = new JRadioButton("4MB");
        memoriapanel.add(fourGB);
        Memoria.add(fourGB);
 
        eightGB = new JRadioButton("8MB");
        memoriapanel.add(eightGB);
        Memoria.add(eightGB);
 
        sixteenGB = new JRadioButton("16MB");
        memoriapanel.add(sixteenGB);
        Memoria.add(sixteenGB);
 
        JLabel lblPlacaMadre = new JLabel("<html><font color=red><b>*</b></font> Motherboard:</html>");
        lblPlacaMadre.setBounds(10, 65, 90, 14);
        frmVentaPc.getContentPane().add(lblPlacaMadre);
 
        JLabel lblMemoria = new JLabel("<html><font color=red><b>*</b></font> Memory Size:</html>");
        lblMemoria.setBounds(10, 120, 90, 22);
        frmVentaPc.getContentPane().add(lblMemoria);
 
        chkMonitor = new JCheckBox("Monitor");
        chkMonitor.setBounds(128, 160, 97, 23);
        frmVentaPc.getContentPane().add(chkMonitor);
 
        chkFixedDisk = new JCheckBox("Disco Duro 1TB");
        chkFixedDisk.setBounds(231, 160, 120, 23);
        frmVentaPc.getContentPane().add(chkFixedDisk);
 
        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setText("0,00");
        textField.setBounds(235, 190, 140, 30);
        frmVentaPc.getContentPane().add(textField);
        textField.setColumns(10);
 
        JButton calcuimp = new JButton("Calcular importe");
        calcuimp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (!verifyEntriesAndSetTotal()) {
                    JOptionPane.showMessageDialog(frmVentaPc, "Las opciones necesarias no an sido elegidas!",
                                                  "seleccion invalida", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        calcuimp.setBounds(75, 194, 140, 23);
        frmVentaPc.getContentPane().add(calcuimp);
 
        JButton salir = new JButton("salir");
        salir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                 System.exit(0);
            }
        });
        salir.setBounds(235, 234, 140, 23);
        frmVentaPc.getContentPane().add(salir);
 
        micro = new JComboBox<>(micros);
        micro.setSelectedIndex(-1);
        micro.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
 
            }
        });
        micro.setBounds(215, 15, 130, 22);
        frmVentaPc.getContentPane().add(micro);
        micro.getSelectedItem();
 
        JButton verdial = new JButton("Ver Dialogo");
        verdial.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                JOptionPane.showInputDialog(frmVentaPc, "<html><center>El procesador es: &nbsp;&nbsp;<font color=blue><b>"
                        + micro.getSelectedItem().toString() + "</b></font></center><br></html>",
                                            "Tipo de Procesador", JOptionPane.QUESTION_MESSAGE);
            }
        });
        verdial.setBounds(75, 234, 140, 23);
        frmVentaPc.getContentPane().add(verdial);
    }
 
    private boolean verifyEntriesAndSetTotal() {
        boolean res = true;
        precioTotal = 0;
 
        // Processor
        if (micro.getSelectedIndex() != -1) {
            switch (micro.getSelectedItem().toString().toLowerCase()) {
                case "intel":
                    precioTotal += intel;
                    break;
                case "athlon":
                    precioTotal += athlon;
                    break;
                case "turion":
                    precioTotal += turion;
                    break;
                default:
                    return false;
            }
        }
        else {
            return false;
        }
 
        // Motherboard
        if (asusbutton.isSelected()) {
            precioTotal += asus;
        }
        else if (gigabutton.isSelected()) {
            precioTotal += giga;
        }
        else if (msibutton.isSelected()) {
            precioTotal += msi;
        }
        else {
            return false;
        }
 
        // Memory
        if (twoGB.isSelected()) {
            precioTotal += twogb;
        }
        else if (fourGB.isSelected()) {
            precioTotal += fourgb;
        }
        else if (eightGB.isSelected()) {
            precioTotal += eightgb;
        }
        else if (sixteenGB.isSelected()) {
            precioTotal += sixteengb;
        }
        else {
            return false;
        }
 
        //Opcional
        // Monitor
        if (chkMonitor.isSelected()) {
            precioTotal += monitor;
        }
 
        // Disco Duro
        if (chkFixedDisk.isSelected()) {
            precioTotal += discofijo;
        }
 
        // Precio total
        textField.setText(String.valueOf(precioTotal));
        return res;
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

el componente <string> no me permite compilar

Publicado por Kabuto (1381 intervenciones) el 23/05/2021 23:13:14
¿Y que versión de Java es la que tienes que usar?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 7
Ha aumentado su posición en 4 puestos en Java (en relación al último mes)
Gráfica de Java

el componente <string> no me permite compilar

Publicado por Martin (3 intervenciones) el 23/05/2021 23:29:01
con java 6, una version antes de la que permite que combobox fuese generic y se permitiera este tipo de comando.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

el componente <string> no me permite compilar

Publicado por Kabuto (1381 intervenciones) el 24/05/2021 00:35:49
Val.
He probado el código bajo un JDK actual, y funciona perfectamente.

Ahora estoy probando en otro proyecto bajo Java 6, y salen dos errores.

Uno, en la línea 172, que es al que tú te referías:

1
micro = new JComboBox<>(micros);

Se queja de que el constructor del ComboBox no puede "inferir" automáticamente la clase de objeto que va a contener.
Pero no importa, le indicamos nosotros la clase y ya está

1
micro = new JComboBox<String>(micros);

Con eso, este error me desaparece. De hecho, yo al menos, SIEMPRE le indico la clase al constructor del JComboBox bajo los JDK actuales.

Ahora bien, hay un segundo error, y me temo que para este no hay solución posible, sin tener que alterar sensiblemente el código.
Es en el switch de la línea 203:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (micro.getSelectedIndex() != -1) {
    switch (micro.getSelectedItem().toString().toLowerCase()) {
    case "intel":
        precioTotal += intel;
        break;
    case "athlon":
        precioTotal += athlon;
        break;
    case "turion":
        precioTotal += turion;
        break;
    default:
        return false;
    }

Java 6 no admite usar la clase String en los switch. Se incluyó esta posibilidad a partir de Java 7.
Para esto no hay solución, no hay magia posible.
O se cambia el código sustituyendo ese switch por if else.... o se compila el proyecto bajo Java 7 o posterior
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar