如何内的JPanel移除项目是由一个ArrayList指定 [英] How to remove item inside JPanel which is designated by an ArrayList

查看:252
本文介绍了如何内的JPanel移除项目是由一个ArrayList指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与GridLayout的一个JPanel是随机使用一个ArrayList创建Jbutton将填充。阵列(一瓦)的每个元素都包含一个字符。 它应该是一个瓷砖移除游戏。的,但我意识到,code我写的没有工作,因为一旦瓦片被删除,其他人的指标变化。

There is a JPanel with GridLayout which is randomly populated by JButtons created using an ArrayList. Each element of the array (a Tile) contains a character. It's supposed to be a tile removing game. However, I realised that the code I wrote didn't work, because once a tile is removed, the others' indexes change.

一个KeyListener的被应用到的JPanel。这种方法是在我的模型类,其中第一次创建的ArrayList。 getchar函数,在瓷砖类,只需返回其性格。

A KeyListener is applied to the JPanel. This method is in my Model class, where the ArrayList is first created. getChar, in the Tile class, simply returns its character.

public void removeChar(char typedChar) {
    for(Tile t: _al){
            if(t.getChar() == typedChar) {
                System.out.println(typedChar + " is in the game, at index " + _al.indexOf(t) + " of the array.");
                _p.remove(_al.indexOf(t));
            }

使用我的code,我基本上只希望当它的字符键键入删除相应的瓷砖。谁能帮我看到一个更好的(当然,工作)的方式来做到这一点?我的code似乎变得更加复杂,因为ArrayList是满的对象,但后来我需要的对象。

With my code, I basically just want to remove the corresponding tile when its character key is typed. Can anyone help me see a better (well, working) way to do this? My code seems to become much more complicated because the ArrayList is full of objects, but I will need the objects later.

推荐答案

不要使用的KeyListener。秋千的设计与键绑定使用。

Don't use a KeyListener. Swing was designed to be used with Key Bindings.

添加键绑定到每个按钮。然后,当信打的按钮,将事件的源,以便你可以从面板上的按钮。

Add a Key Binding to each button. Then when the letter is typed the button will be the source of the event so you can just remove the button from the panel.

有关如何键绑定工作,请参见下面的例子:

For an example of how key bindings work see below:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
//              display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(30, 30) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

所以你的情况在行动actionPerformed()方法的code会是这样的:

So in your case the code in the actionPerformed() method of the Action would be something like:

JButton button = (JButton)e.getSource();
JPanel parent = (JPanel)button.getParent();
parent.remove(button);
parent.revalidate();
parent.repaint();

编辑:

在使用的KeyListener您可以使用HashMap中的角色绑定到相关的按钮:

When using a KeyListener you might use HashMap to bind the character to the related button:

HashMap<Character, JButton> buttons = new HashMap<Character, JButton>();
...
buttons.put('a', aButton);
buttons.put('b', bButton);

然后在KeyListener的code中的的keyTyped()code会是这样的:

Then the keyTyped() code in the KeyListener code would be something like:

JButton button = buttons.get( e.getKeyChar() );
panel.remove( button );
panel.revalidate();
panel.repaint();

这篇关于如何内的JPanel移除项目是由一个ArrayList指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆