1

我编写了一个 java 应用程序,它使用 Netbeans 7.0.1 及其在 gui builder 中的构建来完成一些不同的事情。我遇到的问题是屏幕上有一些文本字段供用户输入。最初是一组字段和一个最多可添加 10 个的按钮。我还有一个删除按钮,用于删除一个字段。所以基本上按钮向面板添加和删除 jTextFields 和 jLabels。

当我单击按钮时似乎有延迟所以我添加了一些 System.out.prints 并发现有时按钮会被按下,系统会打印它应该打印的内容但只是忽略添加/删除组件.

这是一个已知问题吗?(我还没有找到任何东西,虽然我不是 100% 确定如何措辞我的搜索)还是我做错了什么?

代码示例:注意:当前组件是每个组件及其值的映射

private void addButtonMouseClicked(java.awt.event.MouseEvent evt) {                                       
    if(hiddenValue != 81) {
        currentComponents.get(hiddenValue).setVisible(true);
        currentComponents.get(hiddenValue + 1).setVisible(true);
        currentComponents.get(hiddenValue + 2).setVisible(true);
        currentComponents.get(hiddenValue + 3).setVisible(true);
        currentComponents.get(hiddenValue + 4).setVisible(true);
        currentComponents.get(hiddenValue + 5).setVisible(true);
        currentComponents.get(hiddenValue + 6).setVisible(true);
        currentComponents.get(hiddenValue + 7).setVisible(true);
        currentComponents.get(hiddenValue + 8).setVisible(true);
        hiddenValue =  hiddenValue + 10;
        numEntries++;
        removeButton.setVisible(true);
        removeButton.setEnabled(true);
        System.out.println(hiddenValue);
    }
    else {
        currentComponents.get(hiddenValue).setVisible(true);
        currentComponents.get(hiddenValue + 1).setVisible(true);
        currentComponents.get(hiddenValue + 2).setVisible(true);
        currentComponents.get(hiddenValue + 3).setVisible(true);
        currentComponents.get(hiddenValue + 4).setVisible(true);
        currentComponents.get(hiddenValue + 5).setVisible(true);
        currentComponents.get(hiddenValue + 6).setVisible(true);
        currentComponents.get(hiddenValue + 7).setVisible(true);
        currentComponents.get(hiddenValue + 8).setVisible(true);
        hiddenValue =  hiddenValue + 10;
        numEntries++;
        addButton.setVisible(false);
        addButton.setEnabled(false);
        System.out.println(hiddenValue);
    }
    }    
4

3 回答 3

2

我没有隐藏/显示组件的问题:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestVisibility {

    private class Pair<P1, P2> {
        public final P1 first;
        public final P2 second;

        public Pair(P1 first, P2 second) {
            this.first = first;
            this.second = second;
        }
    }

    private List<Pair<JLabel, JTextField>> pairs = new ArrayList<TestVisibility.Pair<JLabel, JTextField>>();

    protected void initUI() {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbcLabel = new GridBagConstraints();
        gbcLabel.gridx = 0;
        gbcLabel.weightx = 0.0;
        gbcLabel.weighty = 0.0;
        gbcLabel.insets = new Insets(3, 3, 3, 3);
        GridBagConstraints gbcTF = new GridBagConstraints();
        gbcTF.gridx = 1;
        gbcTF.fill = GridBagConstraints.HORIZONTAL;
        gbcTF.weightx = 1.0;
        gbcTF.weighty = 1.0;
        gbcTF.insets = new Insets(3, 3, 3, 3);
        for (int i = 0; i < 10; i++) {
            gbcLabel.gridy = i;
            gbcTF.gridy = i;
            JLabel label = new JLabel("Field " + (i + 1) + ":");
            JTextField textField = new JTextField(50);
            label.setLabelFor(textField);
            Pair<JLabel, JTextField> pair = new Pair<JLabel, JTextField>(label, textField);
            pairs.add(pair);
            frame.add(label, gbcLabel);
            frame.add(textField, gbcTF);
        }
        JButton button = new JButton("Toggle");
        button.addActionListener(new ActionListener() {

            private boolean visible = true;

            @Override
            public void actionPerformed(ActionEvent e) {
                visible = !visible;
                boolean isOdd = false;
                for (Pair<JLabel, JTextField> pair : pairs) {
                    boolean oddVisible = isOdd || visible;
                    pair.first.setVisible(oddVisible);
                    pair.second.setVisible(oddVisible);
                    isOdd = !isOdd;
                }
            }
        });
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 10;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        frame.add(button, gbc);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestVisibility().initUI();
            }
        });
    }
}

您可能应该考虑使用SSCCE更新您的帖子

于 2012-06-21T12:04:12.543 回答
2

如果您只想在运行时隐藏/显示一些组件,我建议使用MigLayout。它是一个 LayoutManager,允许您定义如果组件不可见会发生什么。

  • hidemode 0: 不可见组件占用的空间与可见组件完全相同。
  • hidemode 1:不可见的组件不占用空间,但会消耗它们的网格单元。
  • hidemode 2:不可见的组件不占用空间,消耗它们的网格单元,但单元间隙设置为大小 0。
  • hidemode 3:不可见的组件根本不会参与布局(它们不会消耗它们的网格单元)。

它的工作方式与标准的 LayoutManagers 略有不同,但它们有很好的代码示例和很棒的备忘单。

于 2012-06-21T12:15:47.250 回答
2

JComponents由他们(es)调用真的不是好主意index,也不是为什么要重新发明轮子,请使用(避免任何一个XxxExceptions

for (Component c : currentComponents.getComponents()) {
    //if (c instanceof JButton) {
       c.setVisible(true);
    //}
}

类似于setEnabled ( true / false)JButtons ActionPerfomed的例子JComponentsJPanel

于 2012-06-21T11:45:43.030 回答