我正在尝试从另一个类更新Jlabel的文本。我尝试使用setText方法,这会导致程序编译正常并运行,但是当我按下按钮时没有任何反应。
以下是我的Menu.class中的代码,Jlabel所在的位置。
public class Menu extends JFrame {
private JPanel contentPane;
private JLabel lblOthello;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Menu frame = new Menu();
frame.setVisible(true);
}
});
}
public Menu() {
//Sets program icon
ImageIcon img = new ImageIcon("icon.png");
//Creates the frame
setForeground(Color.RED);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 384, 475);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblOthello = new JLabel("Othello");
lblOthello.setForeground(Color.YELLOW);
lblOthello.setFont(new Font("Franklin Gothic Medium Cond", Font.BOLD, 33));
lblOthello.setHorizontalAlignment(SwingConstants.CENTER);
lblOthello.setBounds(0, 0, 366, 59);
contentPane.add(lblOthello);
JButton btnNewButton = new JButton("New Singleplayer Game");
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBackground(UIManager.getColor("Button.background"));
btnNewButton.setBounds(10, 72, 344, 45);
btnNewButton.addActionListener(new ButtonListener());
contentPane.add(btnNewButton);
JButton btnNewMultiplayerGame = new JButton("New Dualplayer Game");
btnNewMultiplayerGame.setBounds(10, 130, 344, 45);
btnNewMultiplayerGame.addActionListener(new ButtonListener());
contentPane.add(btnNewMultiplayerGame);
JSeparator separator = new JSeparator();
separator.setBounds(0, 57, 366, 2);
contentPane.add(separator);
JButton btnNewNetworkGame = new JButton("New Network Game");
btnNewNetworkGame.setBounds(10, 188, 344, 45);
btnNewNetworkGame.addActionListener(new ButtonListener());
contentPane.add(btnNewNetworkGame);
JButton btnAbout = new JButton("AI vs AI Game");
btnAbout.setBounds(10, 246, 344, 45);
btnAbout.addActionListener(new ButtonListener());
contentPane.add(btnAbout);
JButton btnNewButton_1 = new JButton("QUIT");
btnNewButton_1.setBounds(10, 446, 344, 25);
btnNewButton_1.addActionListener(new ButtonListener());
contentPane.add(btnNewButton_1);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\User\\workspace\\Othello\\src\\Othello.jpg"));
lblNewLabel.setBounds(0, -36, 366, 507);
contentPane.add(lblNewLabel);
JButton button = new JButton("QUIT");
button.setBounds(10, 390, 344, 25);
button.addActionListener(new ButtonListener());
contentPane.add(button);
JSeparator separator_1 = new JSeparator();
separator_1.setBounds(0, 375, 366, 2);
contentPane.add(separator_1);
}
public void setText(String text) {
lblOthello.setText(text);
System.out.println("executed");
contentPane.validate();
contentPane.repaint();
}
}
以下是从我的buttonlistener类
执行settext方法的代码部分 case PLAYER_VS_AI:
Menu men = new Menu();
men.setText("Now");
men.revalidate();
men.repaint();
break;
有谁能告诉我为什么这不起作用?
答案 0 :(得分:0)
每按一次按钮,您就会创建一个新的Menu对象,而不是修改现有的Menu对象。