我正在制作一个闪存卡应用程序,而我正试图将它放到顶部有一个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;
}
}
答案 0 :(得分:0)
我想你忘了在你的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();
}
}
电贺 帕特里克