Java 单击按钮后使JLabel刷新

Java 单击按钮后使JLabel刷新,java,swing,user-interface,actionlistener,jlabel,Java,Swing,User Interface,Actionlistener,Jlabel,我正在制作一个闪存卡应用程序,我正在尝试将它放到顶部有一个JLabel的位置,它显示了闪存卡的活动集是什么。不过,我设置它的方式是,当用户单击“添加集”按钮时,它会在底部创建一个按钮,而不需要调用真正的标签 因此,我使用ActionListener尝试获取按钮本身的标签,但JLabel只显示Add Set按钮的标题,并且不会在单击其他按钮时更改 这是我的密码: import javax.imageio.ImageIO; import javax.swing.*; import java.awt.

我正在制作一个闪存卡应用程序,我正在尝试将它放到顶部有一个JLabel的位置,它显示了闪存卡的活动集是什么。不过,我设置它的方式是,当用户单击“添加集”按钮时,它会在底部创建一个按钮,而不需要调用真正的标签

因此,我使用ActionListener尝试获取按钮本身的标签,但JLabel只显示Add Set按钮的标题,并且不会在单击其他按钮时更改

这是我的密码:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class GraphicsUI extends JPanel {

    private DeckList setsList;
    CardActions action = new CardActions();

    JButton addSetButton, addCardButton;

    private JLabel label;
    private JPanel actives, optionsPanel,cardPanel, flashCardsPanel, bottomPanel;

    private String name;

    public GraphicsUI(){
        this.setPreferredSize(new Dimension(1000,825));
        this.setBackground(Color.LIGHT_GRAY);

        actives = new JPanel();
        actives.setPreferredSize(new Dimension(1000, 50));
        actives.setBackground(Color.blue);
        this.add(actives);

        label = new JLabel();
        actives.add(label);



        optionsPanel = new JPanel();
        optionsPanel.setPreferredSize(new Dimension(200, 350));
        optionsPanel.setBackground(Color.cyan);
        this.add(optionsPanel);

        cardPanel = new JPanel();
        cardPanel.setPreferredSize(new Dimension(580, 350));
        cardPanel.setBackground(Color.green);
        this.add(cardPanel);

        flashCardsPanel = new JPanel();
        flashCardsPanel.setPreferredSize(new Dimension(200, 350));
        flashCardsPanel.setBackground(Color.white);
        this.add(flashCardsPanel);


        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        bottomPanel.setPreferredSize(new Dimension(1000, 400));
        bottomPanel.setBackground(Color.black);
        this.add(bottomPanel);

        this.addSetButton = new JButton("Add Set");
        addSetButton.setPreferredSize(new Dimension(150, 30));
        optionsPanel.add(addSetButton, BorderLayout.WEST);
        addSetButton.addActionListener(new ButtonListener());



    }


    public class ButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            label.setText("Active set: " + e.getActionCommand());


            if(e.getSource() ==addSetButton){
            action.setCommand(CardActions.Command.ADDSET);
            action.setList(getSetInfo());
            }

        }

    }

    private CardList getSetInfo(){
        CardList cl = new CardList();
        String setName = JOptionPane.showInputDialog("Enter the name of your set.");

        if(setName.isEmpty()){
            JOptionPane.showMessageDialog(this, "Cannot have an empty set.");
        }
        else{
            cl.setSetName(setName);
            ImageIcon img = new ImageIcon("setIcon.png");
            JButton newset = new JButton(setName);
            newset.setIcon(img);
            newset.setBackground(Color.white);
            bottomPanel.add(newset);
            bottomPanel.revalidate();           
        }
        return cl;
    }

}

我想你忘了在你的newSet按钮中添加一个ActionListener,下面你会找到一个有效的例子。我用图标注释了这两行,所以取消注释并尝试一下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author ottp
 * @version 1.0
 */
public class CardPanel extends JPanel {

    final private JLabel label;
    private JButton addSetButton;
    private JPanel actives, optionsPanel,cardPanel, flashCardsPanel, bottomPanel;

    public CardPanel() {

        actives = new JPanel();
        actives.setPreferredSize(new Dimension(1000, 50));
        actives.setBackground(Color.BLUE);
        this.add(actives);

        label   = new JLabel();
        actives.add(label);

        optionsPanel = new JPanel();
        optionsPanel.setPreferredSize(new Dimension(200, 350));
        optionsPanel.setBackground(Color.cyan);
        this.add(optionsPanel);

        cardPanel = new JPanel();
        cardPanel.setPreferredSize(new Dimension(580, 350));
        cardPanel.setBackground(Color.green);
        this.add(cardPanel);

        flashCardsPanel = new JPanel();
        flashCardsPanel.setPreferredSize(new Dimension(200, 350));
        flashCardsPanel.setBackground(Color.white);
        this.add(flashCardsPanel);


        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        bottomPanel.setPreferredSize(new Dimension(1000, 400));
        bottomPanel.setBackground(Color.black);
        this.add(bottomPanel);

        this.addSetButton = new JButton("Add Set");
        addSetButton.setPreferredSize(new Dimension(150, 30));
        optionsPanel.add(addSetButton, BorderLayout.WEST);
        addSetButton.addActionListener(new ButtonListener());



    }

    public class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setForeground(Color.WHITE);
            label.setText("Active set: " + e.getActionCommand());

            if(e.getSource().equals(addSetButton)) {
                getSetInfo();
            }
        }

    }

    private void getSetInfo() {
        String newSetName = 
                JOptionPane.showInputDialog(cardPanel, "Enter the name of your set", JOptionPane.INFORMATION_MESSAGE);

        JButton newSet = new JButton(newSetName);
        newSet.setBackground(Color.WHITE);
        newSet.addActionListener(new ButtonListener());

        bottomPanel.add(newSet);
        bottomPanel.revalidate();
    }
}
问候
Patrick

你有没有试过像你之前的问题一样,在
actives
GraphicsUI
上调用
revalidate
?是的,我有,但似乎不起作用。JLabel仍保持原样。