JButtonをクリックしたときにJLabelを表示しようとしています。私はアクションリスナーを追加し、そのコンポーネントをレイアウトに追加しました。 actionPerformedでJButtonをクリックしたときにlabel1.setVisible(true)を使用しています。私はまだそれを働かせることはできません。私のコードを見ることができますか?actionPerformedでJButtonをクリックするとJLabelが表示される
public class LearnAppMain extends JFrame implements ActionListener {
// Define variables
public JButton button1;
public JLabel label1;
public JTextField field1;
private Image image1;
private String apple = "apple.jpg";
public LearnAppMain() {
ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
JLabel label1 = new JLabel(image1);
button1 = new JButton("A");
button1.addActionListener(this);
field1 = new JTextField(10);
// Create layout
setLayout(new FlowLayout());
// create Container
final Container cn = getContentPane();
cn.add(button1);
cn.add(field1);
cn.add(label1);
// setLayout(new FlowLayout());
setSize(250, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (e.getSource() == button1) {
label1.setVisible(true);
field1.setText("Apple");
}
}
}
私は別のクラスファイルに私のmainメソッドを持っています。私が得るエラーは、label1.setVisible(true)につながります。
私はこれらのことを言うと私は見てきましたが、追加する必要のあるものがあるかどうか疑問に思っています。
ありがとう、本当に助けになりました!私は間違いなくそれらのリンクを調べます。 –
確かに、それはうまくいった! –
@KeithKaplan:icyrockの投稿があなたを助けているなら、質問の隣にある上向きの矢印をクリックして投票してください(私がすでに行ったように)。問題が解決したら、その左側のチェックマークをクリックして*回答*として選択します。 –