JcomboBox在点击时无法打开,并且将焦点丢失到旁边的焦点

时间:2017-01-07 08:07:11

标签: netbeans jcombobox

我想在同一个GUI中创建几个下拉列表。我想根据(x,y)定位它们,所以我必须这样做:.setLayout(null); 不确定这会导致问题。但是,当我尝试点击任何下拉列表时,它们都没有打开(根本没有任何反应),而另一个下拉列表(同一页面中很少有)获得焦点。一些非常奇怪的东西..我确定我没有做好。我在这里添加代码。它是由3个不同的类构建的(每个类都在自己的文件中)。如果任何人都可以按原样运行代码并且只是看到行为,那么您可能知道导致它的原因。还有一件事。程序从名为: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);
        }
    }
}

现在二等。 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);
         }
    }
}

希望你好好运行它..谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

看来问题是paintComponent函数中的.add。它不应该在里面,因为它再次添加了许多组件,似乎会导致问题。