ボタンを押したときにラベルがテキストを変更したい。しかし、ボタンをクリックしたときにラベル上でsetText()を使用しようとすると、それを認識しません。Java - ボタンのラベルテキストを変更する
public class SingleButton JPanelを拡張します ActionListener { protected JButton b1;
:
public SingleButton() {
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif");
b1 = new JButton("Axxxxxx"/*, leftButtonIcon*/);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("enableb1");
b1.setEnabled(true);
//Listen for actions on all buttons.
b1.addActionListener(this);
b1.setToolTipText("Click for a UPNs");
JLabel labelUpn = new JLabel("UPN number here on button press");
//Add Components to this container, using the default FlowLayout.
add(b1);
add(labelUpn);
labelUpn.setText("UPN number here");
}
public void actionPerformed(ActionEvent e) {
if ("enableb1".equals(e.getActionCommand())) {
b1.setEnabled(false);
labelUpn.setText("New Text");
}
}
で最小である actionPerformedメソッドにButton_name.setText(「テキスト」)と呼ばれていなかった - まだ同じ問題を生成することをできるだけ少ないコードを使用してください。すべてのコードを表示しなければ、問題を再現することはできません。 – PerfectContrast
** 1 **ラベルと** 1 **ボタンだけで同じタイプのコードを書いてください。問題が再現された場合 - MVCEを取得していない場合は、実際のコードと比較して違いを見つけてください。 –
投稿する[mcve]。私たちはgithubプロジェクトを見ません。 – Reimeus