我看过其他JLabel线程虽然类似,但有些似乎并不适用于我正在经历的内容.首先,我想说我是Java的新手.接下来,我正在尝试在docs.oracle.com网站上关注教程和演示.现在,当我在JTextField中键入内容时,我可以更新标签,并且有一个ActionListener ...
但我也有一个菜单,当我选择菜单项时,该操作不想更新标签.
问题 -
我可以很容易地在这里复制代码,但它现在很像意大利面条,所以如果你想看到任何东西,请具体告诉我,我会发布它.
我将不胜感激任何人都能提供的帮助.
---编辑---哇!! 感谢所有的意见和建议.
1 - 我知道它必须是我的代码.正如我所提到的,我真的只是从演示和教程中拼凑起来,并尝试沿途学习Java.我从来没有得到面向对象的悬念...... 2 - 我知道个别听众正在工作.我正在使用System.out.println进行验证,以及在调试模式下检查这些标签以确定它们确实已更改.
3 - 我将查看此处发布的各种链接和代码,看看我是否可以弄清楚我的代码有什么问题.
真的,再次感谢!
- -编辑 - -
这是我最初在createAndShowGUI方法中所拥有的....
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Create XML for Photo Gallery");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(new CreateGalleryXML());
frame.pack();
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
在您的代码中,您自己似乎做错了.如果没有合适的SSCCE很难说你做什么东西错了,因为它完美,使用两个相同的ActionListener JMenuItem和JTextField.
这是一个与您的匹配的示例程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UpdateLabel
{
private JLabel label;
private String labelText;
private ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
setLabelText((String) ae.getActionCommand());
label.setText(getLabelText());
}
};
private void setLabelText(String text)
{
labelText = text;
}
private String getLabelText()
{
return labelText;
}
private void createAndDisplayGUI()
{
final JFrame frame = new JFrame("Update Label");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
JMenuBar menuBar = new JMenuBar();
JMenu programMenu = new JMenu("Program");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame.dispose();
}
});
JMenu labelMenu = new JMenu("Label");
JMenuItem updateMenuItem = new JMenuItem("Update Label");
updateMenuItem.setActionCommand("Updated by JMenuItem");
updateMenuItem.addActionListener(action);
programMenu.add(exitMenuItem);
labelMenu.add(updateMenuItem);
menuBar.add(programMenu);
menuBar.add(labelMenu);
frame.setJMenuBar(menuBar);
JPanel contentPane = new JPanel();
label = new JLabel("I am the LABEL which will be updated!!");
contentPane.add(label);
JTextField tfield = new JTextField(10);
tfield.setActionCommand("Updated by JTextField");
tfield.addActionListener(action);
frame.add(contentPane, BorderLayout.CENTER);
frame.add(tfield, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new UpdateLabel().createAndDisplayGUI();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
以下是两种情况下的输出:
和 
检查主要方法,可能是你没有把你的代码放在EDT中 - 事件调度程序线程,这也可能导致这样的问题.Swing GUI上的所有更新都必须在Event Dispatcher Thread上完成.
最新编辑
在我看来,它的外观CreateGalleryXML延伸JPanel.请参阅Line 3下面的代码,从您的更新中获取代码,这里您正在初始化一个新Object的CreateGalleryXML内部,当您已经window创建了一个Object 时Line 1:
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(new CreateGalleryXML());
Run Code Online (Sandbox Code Playgroud)
因此,尝试将上述内容更改为此
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(window);
Run Code Online (Sandbox Code Playgroud)
看看会发生什么,请再次回复:-)
| 归档时间: |
|
| 查看次数: |
970 次 |
| 最近记录: |