【发布时间】:2014-09-19 10:33:12
【问题描述】:
我想在单击按钮时显示标签。我正在使用 Eclipse Juno。我添加了标签并设置了可见部分...
wLabel = new JLabel("YOu and Me");
wLabel .setVisible(false);
wLabel .setBounds(80, 35, 100, 25);
wLabel .setFont(new Font("Meiryo", Font.PLAIN, 9));
wLabel .setForeground(new Color(255, 102, 21));
add(wLabel);
按钮
wButton = new JButton("W");
wButton .setActionCommand("myButton");
wButton .addActionListener(this);
wButton .setFont(new Font("Meiryo UI", Font.PLAIN, 11));
wButton .setBounds(10, 33, 70, 35);
wButton .setBackground(new Color(102, 51, 20));
add(wButton);
这里是actionPerformed。我已经实现了 ActionListener
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("myButton")) {
wLabel.setVisible(true);
}
}
【问题讨论】:
-
wButton = new JButton("W"); wButton.setActionCommand("myButton"); wButton.setFont(new Font("Meiryo UI", Font.PLAIN, 11)); wButton.setBounds(10, 32, 80, 30); wButton.setBackground(新颜色(102, 51, 0));添加(w按钮); wButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("myButton")) { wLabel.setVisible(true); } }});
-
这样操作还是不行
-
试着把例如System.out.println() 检查方法是否被调用。如果它不起作用,请尝试删除
if(..)声明 -
每当出现“我的组件未显示且我不知道为什么”形式的问题时,您必须向我们展示您为 (1) 创建组件 (2 ) 将组件添加到面板,以及 (3) 将面板添加到顶级容器。到目前为止,你还没有这样做。为了尽快获得更好的帮助,请发布 Minimal, Complete, Verifiable Example 来证明问题。很多时候,仅仅创建示例就会暴露问题。
标签: java eclipse swing jbutton jlabel