我在选择JCombox项目时遇到问题是gif: Gif that shows exactly what is the problem
这里是您可以按照@UNKNOWN的要求测试自己的代码 我删除了所有不需要的东西,所以你可以测试,我在控制台没有错误; 代码工作正常,但组合选择错误是
import javax.swing.*;
import java.awt.*;
public class ForTest extends JFrame {
private JTextArea txtArea= null;
private JComboBox cmbFontSize = null;
private JComboBox cmbFontFamily = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ForTest();
}
});
}
private ForTest(){
init();
}
public void init(){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("FOR TEST");
this.setLocationRelativeTo(null);
this.setSize(850,500);
txtArea= new JTextArea();
txtArea.setSize(830,470);
JScrollPane panScrollable = new JScrollPane(txtArea);
panScrollable.setSize(840,480);
this.add(panScrollable, BorderLayout.CENTER);
JMenuBar jmbTop= new JMenuBar();
JMenu modifica = new JMenu("Modifica");
modifica.setFont(new Font("arial",Font.PLAIN,15));
JMenu fontSettings= new JMenu("Font Settings");
JMenuItem cmbFam= new JMenuItem("FONT FAMILY");
cmbFontSize = new JComboBox();
cmbFontFamily = new JComboBox();
cmbFam.add(cmbFontFamily);
fontSettings.add(cmbFam);
// fontSettings.add(cmbFontSize);
//fontSettings.add(cmbFontFamily);
modifica.add(fontSettings);
jmbTop.add(modifica);
loadFontFamily();
loadFontSize();
this.setJMenuBar(jmbTop);
this.setVisible(true);
}
private void loadFontFamily() {
String fonts[] =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for ( int i = 0; i < fonts.length; i++ )
{
cmbFontFamily.addItem(fonts[i]);
}
}
private void loadFontSize() {
for (int i= 10; i<50;i++){
cmbFontSize.addItem(i);
}
}
}
代码很简单,但无法理解为什么我无法选择项目 提前谢谢:)
答案 0 :(得分:0)
您应该将JComboBox
包裹在JMenuItem
对象中,然后将其添加到JMenu
对象中。那就行了