Java Swing Button执行具有该功能

时间:2012-06-21 11:19:39

标签: java swing jbutton

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

当我点击按钮时似乎有一个滞后,所以我添加了一些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);
    }
    }    

3 个答案:

答案 0 :(得分:2)

通过他们的JComponents调用index真的不太好,也不是为什么重新发明轮子,请使用(以避免任何XxxExceptions

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

关于JButtons ActionPerfomed }内所有JComponents JPanel的{​​{1}}示例

答案 1 :(得分: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

更新帖子

答案 2 :(得分:2)

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

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

它与标准的LayoutManagers有点不同,但它们有很好的代码示例和很好的备忘单。

相关问题