我想在单击 JButton 时更改我的 JLabel。听起来很简单,但真的找不到一段好的代码。这是我的代码的一部分:
final JButton continueGame = new JButton();
continueGame.setPreferredSize(new Dimension(1000, 30));
continueGame.setLocation(95, 45);
continueGame.setText("<html>Continue</html>");
continueGame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ev) {
panel.remove(continueGame);
SwingUtilities.updateComponentTreeUI(frameKontrastGame);
if(RandomNrJeden <= 50)
{
JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be green.");
frameKontrastGame.setVisible(false);
JFrame frameScenario2 = new JFrame();
frameScenario2.setTitle("Scenario2");
frameScenario2.setSize(1000,700);
frameScenario2.setLocationRelativeTo(null);
frameScenario2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelScenario = new JPanel ();
panel.setLayout(new GridLayout(2, 1));
final JLabel tekst = new JLabel ();
tekst.setText("<html>Część dialogu numer 1</html>");
//JTextField dialog = new JTextField(20);
//dialog.setText("<html>Eggs are not supposed to be green.</html>");
JButton OdpPierwsza = new JButton ();
OdpPierwsza.setText("<html>Opowiedź pierwsza</html>");
OdpPierwsza.addActionListener (new ActionListener(){
@Override
public void actionPerformed(ActionEvent ev) {
tekst.setText("<html>HERE I NEED A TEXT FROM FILE dialog.txt</html>");
}
});
//panelScenario.add(dialog);
panelScenario.add(tekst);
panelScenario.add(OdpPierwsza);
frameScenario2.getContentPane().add(panelScenario);
frameScenario2.setVisible(true);
}
(如果括号是错误的,那是因为它不是整个代码。)
所以:
- 我需要某种阅读器的“这里我需要来自文件 dialog.txt 的文本”在哪里。最好的将是逐行阅读文本的那个。我只是找不到如何写它。
- 我需要
JLabel
将JPanel
.