import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Dummy{
String newSelection = null;
public void init(){
JFrame jFrame = new JFrame("Something");
jFrame.setVisible(true);
jFrame.setSize(new Dimension(600, 600));
jFrame.setLayout(null);
jFrame.setBackground(Color.BLACK);
final String[] possibleNoOfPlayers = {"Two","Three"};
final JComboBox comboBox = new JComboBox(possibleNoOfPlayers);
newSelection = possibleNoOfPlayers[0];
comboBox.setPreferredSize(new Dimension(200,130));
comboBox.setLocation(new Point(200,200));
comboBox.setEditable(true);
comboBox.setSelectedIndex(0);
comboBox.setVisible(true);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JComboBox box = (JComboBox) actionEvent.getSource();
newSelection = (String) box.getSelectedItem();
System.out.println(newSelection);
}
});
jFrame.add(comboBox);
}
}
我正在尝试将组合框添加到框架中。但它不可见。如果您单击该位置,它将显示选项。但它不可见。如果我遗漏了什么,请告诉我。