gpt4 book ai didi

java - 使用 cardlayout 在面板之间切换时,JTextField 和按钮不会更新

转载 作者:行者123 更新时间:2023-11-29 07:36:26 24 4
gpt4 key购买 nike

这几天我一直在努力更新卡片布局中的 Jtextfield 和 Jbutton 数据。我创建了一个小演示来解释我的问题。当我点击“开始”按钮时,它应该显示另一个面板并且它正在工作但是当我返回主页时我希望我的 Jtextfield 和 jbutton 从“世界”更新到“你好”,但这不起作用。任何帮助和建议将不胜感激。(抱歉代码缩进,我不知道为什么复制粘贴不能正常工作)。

  public class CardlayoutDemo {

public static String data = "world";
private static final String INTRO = "intro";
private static final String GAME = "game";
private CardLayout cardlayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardlayout);
private IntroPanel introPanel = new IntroPanel();
private GamePanel gamePanel = new GamePanel();
public CardlayoutDemo() {
mainPanel.add(introPanel.getMainComponent(), INTRO);
mainPanel.add(gamePanel.getMainComponent(), GAME);

introPanel.addBazBtnActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardlayoutDemo.data = "hello";
mainPanel.repaint();
mainPanel.revalidate();
CardLayout cl = (CardLayout)mainPanel.getLayout();
cl.show(mainPanel, GAME);
}
});

gamePanel.addBackBtnActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)mainPanel.getLayout();
cl.show(mainPanel, INTRO);
}
});
}

private JComponent getMainComponent() {
return mainPanel;
}

private static void createAndShowUI() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CardlayoutDemo().getMainComponent());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

class IntroPanel {
private JPanel mainPanel = new JPanel();
public JButton start;
private JButton exit;
private JTextField lblData;

public IntroPanel() {
mainPanel.setLayout(new BorderLayout());
JPanel content = new JPanel();
start = new JButton("Start");
exit = new JButton(CardlayoutDemo.data);
lblData = new JTextField(CardlayoutDemo.data);
content.add(lblData);
content.add(start);
content.add(exit);
mainPanel.add(content, BorderLayout.CENTER);

exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.dispose();
}
});
}
public void addBazBtnActionListener(ActionListener listener) {
start.addActionListener(listener);
}
public JComponent getMainComponent() {
return mainPanel;
}
}

class GamePanel {
private static final Dimension MAIN_SIZE = new Dimension(400, 200);
private JPanel mainPanel = new JPanel();
private JButton back;

public GamePanel() {
back = new JButton("return to main menu");
mainPanel.add(back);
mainPanel.setPreferredSize(MAIN_SIZE);
}

public JComponent getMainComponent() {
return mainPanel;
}

public void addBackBtnActionListener(ActionListener listener) {
back.addActionListener(listener);
}
}

最佳答案

当此行在您的监听器中执行时:

CardlayoutDemo.data = "hello";

您正在创建一个新的 java.lang.String 对象并设置字段数据以引用您刚刚创建的新字符串。这对变量先前引用的 JTextField 或 String 对象的内部状态没有影响。

要更改 JTextField 的文本,您应该调用 JTextField 的 setText(String) 方法。

关于java - 使用 cardlayout 在面板之间切换时,JTextField 和按钮不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com