我想在点击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);
}
(如果括号错误,因为它不是整个代码。)
所以:
JLabel
添加到JPanel
。答案 0 :(得分:1)
您可以使用
将文件读入一个字符串BufferedReader br = new BufferedReader(new FileReader("your_file.txt"));
String line = br.readLine();
ArrayList<String> listOfStrings = new ArrayList<>();
listOfString.add(line);
while(line != null)
{
line = br.readLine();
listOfString.add(line);
}
现在执行for循环迭代JList并将文本添加到JLabel。更好的是JTextArea。