Salve ragazzi quando eseguo il codice seguente:
codice:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBox extends BaseFrame implements ActionListener {
JLabel label;
ComboBox() {
label = new JLabel("Seleziona quanti anni hai altrimenti inseriscilo manualmente");
String[] valori = new String[] {"Quindici", "Sedici", "Diaciasette", "Diciotto"};
JComboBox box = new JComboBox(valori);
box.setEditable(true);
box.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(2,2));
panel.add(label);
panel.add(box);
panel.setPreferredSize(new Dimension(200, 300));
this.add(panel);
this.pack();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JComboBox) {
JComboBox item = (JComboBox)e.getSource();
String value = null;
if(item.getSelectedIndex() > -1) {
value = (String)item.getSelectedItem();
} else {
value = item.getEditor().getItem().toString();
}
label.setText(String.format("Anni: %s", value));
}
}
public static void main(String[] args) {
new ComboBox();
}
}
non mi da nessun errore di compilazione ma quando lo avvio si vede una finestra tutta bianca non riesco proprio a capire il perchè .
Cosa sbaglio? :master:
Inoltre il contenuto di BaseFrame è il seguente:
codice:
import javax.swing.*;
import java.awt.*;
/**
* Implementazione di base JFrame che definisce delle impostazioni
*/
public class BaseFrame extends JFrame {
BaseFrame() {
super();
this.setTitle("Componenti Swing");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1280, 1024);
this.setVisible(true);
}
}
Grazie