JcomboBox 无法在单击时打开并将焦点转移到它旁边的那个

JcomboBox not open on click and looses focus to the one next to it

我想在同一个 GUI 中创建多个下拉菜单。我想根据 (x,y) 定位它们,所以我必须这样做: .setLayout(null); 不确定这是否导致了问题。但是,当我尝试单击任何下拉菜单时,其中 none 打开(根本没有任何反应)并且另一个下拉菜单(同一页面中很少)得到重点。真的很奇怪……而且我确定我没有做对。我在这里添加代码。它由 3 个不同的 classes 构建(每个 class 在其自己的文件中)。如果有人可以 运行 按原样查看代码并简单地查看行为,也许您就会知道是什么原因造成的。还有一件事。程序从一个名为:Menu.txt 的文件中读取。该文件应位于 netbeans 项目的主目录中,并且应包含以下行(确切行):

文件:Menu.txt => 内容:
寿司片
1
20.0
主页 Potetos
2
5.0
胡萝卜汤
2
10.0
热咖啡
3
5.0

第一个文件:ItemInMenu.java:

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;

public class ItemInMenu {
    int itemAmount, itemType, maxOrderAmountPerItem=50; 
    double itemPrice;
    String itemDisc;
    JComboBox itemComboBox;
    JCheckBox itemCheckBox;
    JTextField itemTextField; 

    public ItemInMenu(double itemPrice, String itemDisc, int itemType){

       this.itemCheckBox = new JCheckBox("Select Item");
       this.itemTextField = new JTextField();
       this.itemComboBox = new JComboBox();

       this.itemAmount = 0;
       this.itemType = itemType;
       this.itemPrice = itemPrice;
       this.itemDisc = itemDisc;

       //Add options to the dropdown (max 50 items to be ordered fro meach item)
       for (int i=0; i<maxOrderAmountPerItem; i++)
        {
         this.itemComboBox.addItem("Amount: " + i);
        }
    }
}

现在第二Class。 MainFunction.java:

import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Integer.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class MainFunction {
    public static void main(String[] args) {

        int defaultWidth = 900, defaultHeight = 600;

        JFrame frame = new JFrame("Maman 13 - Question 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(defaultWidth,defaultHeight);

        //Adding the option not to resize the window.
        frame.setResizable(false);

        // Creation of a MyPanel object (which belongs to a class which inherits from JPanel class) //
        MyPanel newPanel = new MyPanel();

        //The .setLayot(null) is a line that disables the Layout manager and lets us position
        //items by ourselves (manual positioning of elements like Jcombo and such). We do
        //this line on the JPanel element (and not on the frame....).
        newPanel.setLayout(null);
        frame.add(newPanel);


        newPanel.readFromFile("Menu.txt");
        newPanel.printMenu();
        frame.setVisible(true);

    }

}

第三个文件:MyPanel.java:

import java.awt.Color;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class MyPanel extends JPanel {

    private ArrayList<ItemInMenu> itemsList = new ArrayList();
    private String fileName;

    public void readFromFile(String whichFile)
    {

         ItemInMenu newItem;
         int lineNumber, itemType = -1;
         Double itemPrice = -1.0;
         String currentLine, itemDisc = "";

         this.fileName = whichFile;

         //Now lets start reading this file...
         System.out.println("Reading from File: " + this.fileName);
         File myFile = new File(this.fileName);

         try {
             lineNumber = 0;
             Scanner myScanner = new Scanner(myFile);
             while (myScanner.hasNext())
                 {
                  lineNumber++;

                  currentLine = myScanner.nextLine();

                  System.out.println(currentLine);

                  if (lineNumber == 1)
                     {
                      itemDisc = currentLine;
                     }
                  else if (lineNumber == 2)
                      {
                       itemType = Integer.parseInt(currentLine);
                      }
                  else
                     {
                      itemPrice = Double.parseDouble(currentLine);

                      newItem = new ItemInMenu(itemPrice, itemDisc, itemType);
                      this.itemsList.add(newItem);

                      lineNumber = 0;
                     }

                  //System.out.println(currentLine);
                 }
             myScanner.close();
         }
         catch (FileNotFoundException ex)  
              {
               System.out.println("File Not Found!");
              }
        }

    public void printMenu(){

        for (ItemInMenu tmpVar : this.itemsList)
         {
          System.out.println("Printing Item Details:");   
          System.out.print("Item Disc: " + tmpVar.itemDisc + "\nItem Price: " + tmpVar.itemPrice + "\nItem Quantity: " + tmpVar.itemAmount + "\nItem Type: " + tmpVar.itemType + "\n\n");
         }
    }

    // The function who update the graphics of JPanel on every operation of the user (resize etc') //
    public void paintComponent(Graphics g) {

        int xLeftBox = 5, xCenterBox = 305, xRightBox = 605;
        int yLeftBox = 5, yCenterBox = 5, yRightBox = 5;
        int comboBoxWidth = 100, comboBoxHeight=25, spaceBetweenItems=100;



        super.paintComponent(g);

        //Now start drawing your own painting.
        for (ItemInMenu tmpVar : this.itemsList)
         {
          if (tmpVar.itemType == 1)
           {
            tmpVar.itemComboBox.setBounds(xLeftBox, yLeftBox, comboBoxWidth, comboBoxHeight);
            yLeftBox += spaceBetweenItems;
           }
          else if (tmpVar.itemType == 2)
                {
                 tmpVar.itemComboBox.setBounds(xCenterBox, yCenterBox, comboBoxWidth, comboBoxHeight);  
                 yCenterBox += spaceBetweenItems;
                }
               else
                {
                 tmpVar.itemComboBox.setBounds(xRightBox, yRightBox, comboBoxWidth, comboBoxHeight);
                 yRightBox += spaceBetweenItems;
                }

          tmpVar.itemComboBox.setSelectedIndex(-1);

          tmpVar.itemComboBox.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e){
                        JComboBox combo = (JComboBox)e.getSource();
                        String currentQuantity = (String)combo.getSelectedItem();
                        System.out.println(currentQuantity);
                    }
                }            
          );

          this.add(tmpVar.itemComboBox);
         }
    }
}

希望你一切都好运行就可以了..谢谢你的帮助。

看来问题出在 .add inside paintComponent 函数上。它不应该在里面,因为它重新添加了很多组件,而且它似乎会导致问题。