简体   繁体   English

用新的替换旧的JButton

[英]replace old JButton with new one

I've created an array of buttons: 我创建了一个按钮数组:

JButton bt[][]=new JButton[8][8];

And then I call a function called refreshBoard in the following way 然后我通过以下方式调用一个称为refreshBoard的函数

public void refreshBoard() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            bt[i][j] = new JButton(); 
            bt[i][j].setIcon(new javax.swing.ImageIcon("icons/"+ board[i][j] +".png"));
                panel.add(bt[i][j);   
        }
    }  

The value in board[i][j] controls which image is to be displayed on the button. board[i][j]控制要在按钮上显示哪个图像。 I call this refreshBoard function at intervals. 我每隔一段时间调用一次refreshBoard函数。 The problem is that when i call the function the second time it adds 64(8X8) new buttons rather than replacing the already displayed ones. 问题是,当我第二次调用该函数时,它添加了64(8X8)新按钮,而不是替换已经显示的按钮。 How do i make it replace the old buttons instead of adding new ones. 我如何使其取代旧按钮,而不是添加新按钮。

This line bt[i][j] = new JButton() creates new buttons all the time you hit refreshBoard() board. 此行bt[i][j] = new JButton()始终在您单击refreshBoard()板时创建新按钮。 This is not right. 这是不对的。

Do as this: 这样做:

Make panel instance variable so that it can be accessed by all instance methods . 使panel instance variable以便所有instance methods都可以访问它。

Add board: 添加板:

public void addBoard() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            bt[i][j] = new JButton(); 
            bt[i][j].setIcon(new javax.swing.ImageIcon("icons/"+ board[i][j] +".png"));
                panel.add(bt[i][j);   
        }
    }  

Refresh Board: 刷新板:

To refresh board, you will have to extract the buttons from JPanel and then use them: 要刷新板,您将必须从JPanel提取buttons ,然后使用它们:

JButton button = null;
Component[] components = panel.getComponents();

public void refreshBoard() {
    for (int i = 0; i < components.length; i++) {

        if (components[i] instanceof JButton) {
            button = (JButton) components[i];
            button.setIcon(<set the icon however you want. extracting from the `board[][]` or by creating new ones>));
        }

    }
} 

You can also put the check to know what JButton you are extracting: 您还可以进行检查,以了解要提取的JButton

String buttonText = button.getText();

Note: You don't need to replace your old buttons when you hit Refresh, just extract from the panel and set the icon on them as I have done in above code. 注意:当您点击Refresh时,您不需要替换旧按钮,只需从面板中提取按钮并在其上设置图标即可,就像我在上面的代码中所做的一样。

You would need to remove your layout on the panel and recreate your layout with the new buttons you want to use I believe. 您将需要删除面板上的布局,并使用您希望使用的新按钮重新创建布局。 There may be a way to remove an item from a layout but i've found it easier to re-redo the layout when i want to do something like this. 可能有一种从布局中删除项目的方法,但是当我想要执行类似的操作时,我发现更容易重做布局。 It may be simpler to keep the same 64 buttons but store them maybe in an array or an array list and update the icons that you set on these buttons in refreshboard. 保留相同的64个按钮可能更简单,但可以将它们存储在数组或数组列表中,并在刷新板上更新在这些按钮上设置的图标。 From a design standpoint it makes sense if the 64 buttons are a fixed element and the icon is changing, you would only change the icons. 从设计的角度来看,如果64个按钮是固定元素并且图标正在更改,那将是有意义的,您只需更改图标即可。

The line: 该行:

        panel.add(bt[i][j); 

simply adds the new buttons to the panel. 只需将新按钮添加到面板即可。 If you call it twice, you got twice worth of buttons added. 如果您两次调用它,那么您将获得价值两倍的按钮添加。 You have to remove the old ones before you add the new ones. 您必须先删除旧版本,然后再添加新版本。

However, is there any reason why you have to create all 64 JButton every time you refresh? 但是,有什么理由每次刷新时都必须创建所有64个JButton吗? It creates a lot of Java objects which is not desired. 它创建了许多不需要的Java对象。 Could you simply set the icon to existing buttons without recreating new ones every time you refresh? 您能否将图标设置为现有按钮,而不必每次刷新都重新创建新按钮?

我不确定,但是也许您应该调用panel.repaint()?

Either explicitly remove them by looping over them (with null checks). 通过遍历它们(使用null检查)来显式删除它们。 Or, in your nested for-loop, replace the first line with the following. 或者,在嵌套的for循环中,将第一行替换为以下内容。

if(!(bt[i][j] instancceof JButton)) {
    bt[i][i] = new JButton();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM