Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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
Java 如何在透明的JPanel上添加JComboBox?_Java_Swing_Jpanel_Jcombobox_Transparent - Fatal编程技术网

Java 如何在透明的JPanel上添加JComboBox?

Java 如何在透明的JPanel上添加JComboBox?,java,swing,jpanel,jcombobox,transparent,Java,Swing,Jpanel,Jcombobox,Transparent,我目前正在开发一个Java项目。在透明的JPanel上添加JComboBox时,我遇到了一个问题。单击JComboBox的一个元素后,将显示一个t字段(区域)。这里我给出了我的项目的三个部分 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.plaf.nimbus.NimbusLook

我目前正在开发一个Java项目。在透明的
JPanel
上添加
JComboBox
时,我遇到了一个问题。单击
JComboBox
的一个元素后,将显示一个
t字段(区域)
。这里我给出了我的项目的三个部分

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class Test  {

    private static Panel1 p1 = new Panel1();
    // private static Panel2 p2 = new Panel2();
    // private static Panel3 p3 = new Panel3();

    private static int i;
    private static JPanel pl1;
    private static Image icon;
    private static Ads panel;

    public Test()
    {

        setLookAndFeel();

        JMenuBar m = new JMenuBar();
        JMenu about = new JMenu("About"); 


        JFrame f = new JFrame();
        f.setTitle("SUPERSHOP MANAGEMENT");
        f.setSize(900, 700);
        f.setLayout(null);  
        icon = new ImageIcon("C:\\Users\\Montasir\\desktop\\12.jpg").getImage();
        panel = new Ads(icon);
        f.add(panel);
        pl1 = new JPanel();
        pl1.setBounds(0, 0, 900, 200);
        pl1.setBackground(new Color(0, 0, 0, 0));

        JButton button1 = new JButton("ITEM");
        JButton button2 = new JButton("UPDATE");
        JButton button3 = new JButton("DAILY SALES");

        pl1.add(button1);
        pl1.add(button2);
        pl1.add(button3);
        panel.add(pl1);
        button1.setBackground(Color.CYAN);
        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setI(1);
               // refreshMe();
            }
        });
        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setI(2);
                //refreshMe();
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setI(3);
               // refreshMe();
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);
        f.setResizable(false);

    }

    public static void setI(int j) {
        i = j;
    }

    public static void refreshMe() {

        checkPanel();

    }
    public static void checkPanel() {

        if (i == 1) {
            panel.add(p1);
            //panel.remove(p2);
           // panel.remove(p3);
            //panel.revalidate();
           // panel.repaint();
        } /*else if (i == 2) {
            panel.add(p2);
            panel.remove(p1);
            panel.remove(p3);
            panel.revalidate();
            panel.repaint();
        }else if (i == 3) {
            panel.add(p3);
            panel.remove(p1);
            panel.remove(p2);
            panel.revalidate();
            panel.repaint();
        }*/
    }

        public static void main(String[] args) 
        {    
            new Test();
        }

    public static void setLookAndFeel()
    {
        try 
        {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}




class Panel1  extends JPanel{

    private JComboBox comb ;

    private JButton b = new JButton("Add items");

    public Panel1() {

        Test.setLookAndFeel();
        setLayout(new FlowLayout());
        this.setBackground(new Color(0,0,0,0));
        this.setBounds(0,200,900,500);


        final String[]name={"Aziz","masum","sakib","shaon"};
        comb=new JComboBox(name);
                this.setPreferredSize(new Dimension(900,600));


        this.add(comb);

    }


}


class Ads extends JPanel {  

  private Image img;  

  public Ads(Image s) 
  {  
      this.img = s;  
      Dimension size = new Dimension(s.getWidth(null), s.getHeight(null));  
      setPreferredSize(size);  
      setMinimumSize(size);  
      setMaximumSize(size);  
      setSize(size);  
      setLayout(null); 
  }  

  public void paintComponent(Graphics g) 
  {
      super.paintComponent(g);
      g.drawImage(img, 0, 0,null);  

  }  


}  

你的代码很难理解,所以我做了一个基本的例子

import java.awt.BorderLayout;
import javax.swing.*;


public class TestBox extends JFrame{

    public static void main(String[] args)
    {
        new TestBox();
    }


    public TestBox()
    {   

        //comboBox creation
        final String[] name = {"Aziz", "masum", "sakib", "shaon"};

        JComboBox comboBox=new JComboBox(name);

        //transparent JPanel creation
        JPanel mainPanel = new JPanel(new BorderLayout()); // transparent frame to add comboBox
        mainPanel.add(comboBox, BorderLayout.NORTH); // comboBox added to transparent frame

        //now dealing with the creation of the JFrame to display it all
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());

        //adding everything to the frame

        this.add(mainPanel, BorderLayout.CENTER);
        this.pack();
        this.setVisible(true);
    }
}
请尝试使用描述性的名称,当其他人不得不分析您的代码时,这会有很大帮助。此外,尝试按照逻辑流程将类似的项目分组在一起

我注意到的一件事是缺乏布局管理。您缺乏对项目显示位置和方式的管理可能是您出现问题的原因。在构建GUI时,请尝试一次添加和测试一个组件,直到您开始工作

编辑:


似乎添加组合框的唯一位置是
checkPanel()
方法,该方法由
refreshMe()
方法触发,该方法从未被调用以添加组合框

查看玻璃窗格