【问题标题】:Java Container remove method not working correctlyJava 容器删除方法无法正常工作
【发布时间】:2012-07-30 20:49:16
【问题描述】:

我添加了 1.TextArea 2.TextField 然后我开始在容器上连续添加 JButton...,现在通过使用 JRadioButton 我想使用此代码从容器中删除 JButton

i=0;
k=0;
while(!birdButton[i].isSelected()){
    i++;    
}   
System.out.println(i);
k=i+2;
list.removeElementAt(i);
listName.removeElementAt(i);
System.out.println(k);
c.getContentPane().remove(k);

但是当我选择第一个单选按钮时,应该删除第一个 JButton,因为 k=i+2; 而不是删除这个,而是删除 TextArea(第一个)。 当我选择第三个单选按钮时,第一个 JButton 被删除。谁能让我知道问题是什么?还有System.out.println(i); System.out.println(k); 没有打印任何值....这里是代码

public class RadioDemo implements ActionListener {

    String buttonName;
    JPanel radioPanel = new JPanel();
    ButtonGroup group = new ButtonGroup();
    Enumeration enl;
    int result;
    ActionEvent e;
    JRadioButton birdButton[];
    int i, k;

    Vector<String> listName;
    Vector<JComponent> list;
    Container c;

    public RadioDemo(Vector<String> listName,Vector<JComponent> list,Container c) {

        birdButton=new JRadioButton[listName.size()];
        this.listName=listName;
        this.c=c;
        this.list=list;

        i = 0;
        for (String buttonName : listName){
               birdButton[i] = new JRadioButton(buttonName);
               birdButton[i].setActionCommand(buttonName);
               group.add(birdButton[i]);
               birdButton[i].addActionListener(this);
               radioPanel.add(birdButton[i]);
               i++;
         }

        birdButton[0].setSelected(true);
        radioPanel.setLayout(new BoxLayout

        (radioPanel,BoxLayout.Y_AXIS));
                                    //birdButton.setBorder

        (BorderFactory.createEmptyBorder(5,5,5,5));
        result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION);                
        show();
    }

    /** Listens to the radio buttons. */
    public void actionPerformed(ActionEvent e) {
        this.e = e;
    }

    public void show() {
        if (result == JOptionPane.OK_OPTION) {
            i = 0;
            k = 0;
            while (!birdButton[i].isSelected()) {
                i++;

            }
            System.out.println(i);
            k = i + 2;
            list.removeElementAt(i);
            listName.removeElementAt(i);
            System.out.println(k);
            c.getContentPane().remove(k);
            c.getContentPane().validate();

            // System.out.println(e.getActionCommand());
            // c.getContentPane().rePaint();
        }
    }

}

【问题讨论】:

  • 相反,您要保持单选按钮与要删除的按钮之间的关系如何,可能类似于HashMap?
  • 我也想看看你是如何构建用户界面的

标签: java swing jframe jpanel


【解决方案1】:

getContentPane() 返回的Container 默认情况下是由top-level container、JFrame 管理的JRootPane 的contentPane。尽管“为方便起见,add 方法及其变体 remove 和 setLayout 已被覆盖以根据需要转发到 contentPane”,但没有先验方法了解框架对组件索引的内部使用。

相反,将您自己的JComponent 添加到框架并对其进行操作; JPanel 是常见的选择。

附录:还可以考虑另一种布局,例如CardLayout,图示为here。

【讨论】:

  • @kleopatra:绝对正确;感谢您的批判性评论;以上。
  • 只是阅读 :-) 很酷的编辑,很想给你第二个赞!
猜你喜欢
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多