Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Netbeans JcomboBox在单击时未打开,并将焦点释放到它旁边的一个_Netbeans_Jcombobox - Fatal编程技术网

Netbeans JcomboBox在单击时未打开,并将焦点释放到它旁边的一个

Netbeans JcomboBox在单击时未打开,并将焦点释放到它旁边的一个,netbeans,jcombobox,Netbeans,Jcombobox,我想在同一个GUI中创建几个下拉列表。我想根据(x,y)来定位它们,所以我必须做:。setLayout(null); 不确定这是否导致了问题。。然而,当我尝试点击任何一个下拉列表时,它们都不会打开(只是什么都不会发生),而另一个下拉列表(同一页面中几乎没有)会获得焦点。真的很奇怪。。我肯定我做得不好。我在这里添加代码。它由3个不同的类(每个类在其自己的文件中)构建。如果任何人都可以按原样运行代码并简单地看到行为,那么您可能知道是什么导致了它。还有一件事。这些程序从一个名为:Menu.txt的文件

我想在同一个GUI中创建几个下拉列表。我想根据(x,y)来定位它们,所以我必须做:。setLayout(null); 不确定这是否导致了问题。。然而,当我尝试点击任何一个下拉列表时,它们都不会打开(只是什么都不会发生),而另一个下拉列表(同一页面中几乎没有)会获得焦点。真的很奇怪。。我肯定我做得不好。我在这里添加代码。它由3个不同的类(每个类在其自己的文件中)构建。如果任何人都可以按原样运行代码并简单地看到行为,那么您可能知道是什么导致了它。还有一件事。这些程序从一个名为:Menu.txt的文件中读取数据。该文件应位于netbeans项目的主目录中,并应具有以下行(精确的行):

文件:Menu.txt=>内容:
寿司片
一,
20.0
家常菜
二,
5.0
胡萝卜汤
二,
10.0
热咖啡
三,
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);
        }
    }
}
第三个文件: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);
         }
    }
}
导入java.awt.Color;
导入javax.swing.*;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Random;
导入java.util.Scanner;
公共类MyPanel扩展了JPanel{
private ArrayList itemsList=new ArrayList();
私有字符串文件名;
public void readFromFile(字符串whichFile)
{
ItemInMenu新建项;
int lineNumber,itemType=-1;
双项目价格=-1.0;
字符串currentLine,itemDisc=“”;
this.fileName=whichFile;
//现在让我们开始读取此文件。。。
System.out.println(“从文件读取:“+this.fileName”);
File myFile=新文件(this.fileName);
试一试{
行号=0;
扫描器myScanner=新扫描器(myFile);
while(myScanner.hasNext())
{
lineNumber++;
currentLine=myScanner.nextLine();
系统输出打印项次(当前行);
如果(行号==1)
{
itemDisc=当前线路;
}
else if(行号==2)
{
itemType=Integer.parseInt(currentLine);
}
其他的
{
itemPrice=Double.parseDouble(currentLine);
newItem=新建ItemInMenu(itemPrice、itemDisc、itemType);
this.itemsList.add(newItem);
行号=0;
}
//系统输出打印项次(当前行);
}
myScanner.close();
}
捕获(FileNotFoundException ex)
{
System.out.println(“未找到文件!”);
}
}
公共无效打印菜单(){
for(ItemInMenu tmpVar:this.itemsList)
{
System.out.println(“打印项目详细信息:”);
系统输出打印(“项目光盘:+tmpVar.itemDisc+”\nItem价格:+tmpVar.itemPrice+”\nItem数量:+tmpVar.itemAmount+“\nItem类型:+tmpVar.itemType+”\n\n”);
}
}
//在用户每次操作时更新JPanel图形的功能(调整大小等)//
公共组件(图形g){
int-xLeftBox=5,xCenterBox=305,xRightBox=605;
int yLeftBox=5,yCenterBox=5,yRightBox=5;
int comboBoxWidth=100,comboBoxHeight=25,spaceBetweenItems=100;
超级组件(g);
//现在开始画你自己的画。
for(ItemInMenu tmpVar:this.itemsList)
{
if(tmpVar.itemType==1)
{
tmpVar.itemComboBox.setBounds(xLeftBox、yLeftBox、comboBoxWidth、ComboxHeight);
yLeftBox+=元素之间的空格;
}
else if(tmpVar.itemType==2)
{
tmpVar.itemComboBox.setBounds(xCenter框、cCenter框、comboBoxWidth、ComboxHeight);
cEnterBox+=元素之间的空格;
}
其他的
{
tmpVar.itemComboBox.setBounds(xRightBox、yRightBox、comboBoxWidth、ComboxHeight);
yRightBox+=元素之间的空格;
}
tmpVar.itemComboBox.setSelectedIndex(-1);
tmpVar.itemComboBox.addActionListener(
新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
JComboBox组合=(JComboBox)e.getSource();
字符串currentQuantity=(字符串)组合。getSelectedItem();
系统输出打印项次(当前数量);
}
}            
);
this.add(tmpVar.itemComboBox);
}
}
}

希望你运行得很好。。感谢您的帮助。

问题似乎是。添加内部paintComponent函数。它不应该在它里面,因为它再次添加了很多组件,而且似乎会导致问题

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);
         }
    }
}