选择出现在 JMenu 上

Choice appears over JMenu

我正在创建一个程序界面,用户可以在其中 select 将某些内容放入菜单中,然后它会显示多个选项卡。在一些选项卡内有选择。当用户 select 菜单中的某些内容时,选项卡变得可见,并且用户再次单击菜单时,问题就来了,这就是发生的事情: menu bugged

现在代码已经扩展了,所以我将坚持创建组件。

菜单项创建:

JMenu menuArquivo = new JMenu("Arquivo");
menuBar.add(menuArquivo);

JMenuItem arqAC = new JMenuItem("Aviso de Cobran\u00E7a");
menuArquivo.add(arqAC);
arqAC.addActionListener(menuItemListener);
arqAC.setActionCommand("AC");

将 tabPane 添加到 contentPane:

contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

tabs = new ArquivoTabs();
JTabbedPane tabbedPane = tabs.getTabs();
tabbedPane.setBounds(0, 0, 1061, 600);
tabbedPane.setSelectedIndex(index);
contentPane.add(tabbedPane);
tabbedPane.setTitleAt(index, title);
revalidate();

内容窗格:

public ArquivoTabs() {

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    add(tabbedPane);

    ACTab act = new ACTab(tabbedPane);
    tabbedPane.addTab("AC", null, act, "Aviso de Cobran\u00E7a");
}

创建一个标签:

public ACTab(JTabbedPane tp) {
    tabbedPane = tp;
    aut = Authority.getAuthority();             
    ButtonListener buttonListener = new ButtonListener();

    setLayout(null);

    tfAC = new JTextField();
    tfAC.setBounds(10, 25, 251, 20);
    add(tfAC);
    tfAC.setColumns(10);

    bpAC = new JButton("Procurar");
    bpAC.setBounds(271, 24, 75, 23);
    add(bpAC);
    bpAC.addActionListener(buttonListener);
    bpAC.setActionCommand("bpAC");

    JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
    separator.setBounds(10, 68, 336, 9);
    add(separator,
              BorderLayout.LINE_START);

    btnGerarAC = new JButton("GERAR");
    btnGerarAC.setBounds(271, 346, 75, 23);
    add(btnGerarAC);
    btnGerarAC.addActionListener(buttonListener);
    btnGerarAC.setActionCommand("btnGerarAC");


    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 113, 336, 106);
    add(scrollPane);

    tfDescDeb = new JTextArea();
    scrollPane.setViewportView(tfDescDeb);
    tfDescDeb.setFont(new Font("Arial", Font.PLAIN, 14));
    tfDescDeb.setColumns(10);
    tfDescDeb.setLineWrap(true);
    tfDescDeb.setWrapStyleWord(true);
    tfDescDeb.setEditable(false);

    motTypes = DescDeb.getMotTypes();

    choice = new Choice();
    choice.setBounds(10, 87, 336, 20);
    add(choice);
    choice.add("");
    for(DescDeb mt : motTypes){
        choice.add(mt.getTitle());
    }
    choice.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent ie)
        {
            for(DescDeb mt : motTypes){
                if(choice.getSelectedItem().equals(mt.getTitle())){
                    tfDescDeb.setEditable(false);
                    tfDescDeb.setText(mt.getText());
                }
            }
        }
    });


    JLabel lblDescDeb = new JLabel("Descri\u00E7\u00E3o dos d\u00E9bitos:");
    lblDescDeb.setBounds(10, 73, 110, 14);
    add(lblDescDeb);

    JLabel lblAutRole = new JLabel("Cargo da Autoridade:");
    lblAutRole.setBounds(10, 258, 110, 14);
    add(lblAutRole);

    txtAutRole = new JTextField();
    txtAutRole.setEditable(false);
    txtAutRole.setColumns(10);
    txtAutRole.setBounds(134, 258, 212, 20);
    txtAutRole.setText(aut.getRole());
    add(txtAutRole);

    JLabel lblAutMat = new JLabel("Matr\u00EDcula da Autoridade:");
    lblAutMat.setBounds(10, 286, 118, 14);
    add(lblAutMat);

    txtAutMat = new JTextField();
    txtAutMat.setEditable(false);
    txtAutMat.setColumns(10);
    txtAutMat.setBounds(134, 286, 212, 20);
    txtAutMat.setText(aut.getRegistration());
    add(txtAutMat);

    JLabel lblAutName = new JLabel("Nome da Autoridade:");
    lblAutName.setBounds(10, 230, 102, 14);
    add(lblAutName);

    txtAutName = new JTextField();
    txtAutName.setEditable(false);
    txtAutName.setBounds(134, 230, 212, 20);
    txtAutName.setText(aut.getName());
    add(txtAutName);

    JButton btnEditarAutoridade = new JButton("Editar Autoridade");
    btnEditarAutoridade.setBounds(226, 317, 119, 23);
    btnEditarAutoridade.addActionListener(buttonListener);
    btnEditarAutoridade.setActionCommand("edtAut");
    add(btnEditarAutoridade);

    JLabel lblArquivoDeEntrada = new JLabel("Arquivo de Entrada:");
    lblArquivoDeEntrada.setBounds(10, 11, 110, 14);
    add(lblArquivoDeEntrada);
}

我必须传递 JTabbedPane tp 才能将它用于不相关的东西。

如果需要更多代码,请告诉我。

您正在将 Swing 组件(JMenuJTabbedPane)与 AWT 组件(Choice)混合使用。这会导致各种问题,包括 Choice 组件在 JMenu.

上的重叠

使用JComboBox代替Choice