gpt4 book ai didi

java - 值更改后 JTextPane 不更新

转载 作者:行者123 更新时间:2023-12-02 06:53:06 25 4
gpt4 key购买 nike

代码在下面。基本上我想做的是在 JTextPane 的 JPanel 中进行显示。我有一个按钮可以编辑应该在 JTextPane 中显示的字符串的值。但是我不知道如何更新 JTextPane。我尝试过 revalidate()、validate()、repaint(),但这些似乎都不起作用。

代码已完成,应该可以运行。

导入java.awt.Canvas;

public class windowBuild extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private int health = 20;
private int energy = 4;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
windowBuild frame = new windowBuild();
frame.setVisible(true);

}
});
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
String which = e.getActionCommand();
if (which.equals("Claw")){
energy = energy-1;
System.out.println("Player one's dragon clawed the opponent. Dragon's energy is now at: "+ energy);}
else if (which.equals("Wait")){
System.out.println("Turn succesfully skipped");}
System.out.println(getEnergy());



}

}


public windowBuild() {
ButtonHandler bh;
System.out.println("Starting frame...");
bh = new ButtonHandler();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new TitledBorder(null, "Dragon Duel",
TitledBorder.CENTER, TitledBorder.TOP, null, Color.CYAN));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton btnClaw = new JButton("Claw");
btnClaw.setBounds(288, 511, 109, 39);
contentPane.add(btnClaw);
btnClaw.addActionListener(bh);
if (energy == 0)
btnClaw.setEnabled(false);
JButton btnWait = new JButton("Wait");
btnWait.setBounds(645, 511, 109, 39);
contentPane.add(btnWait);
btnWait.addActionListener(bh);

StringBuilder sb = new StringBuilder();
String strB = Integer.toString(health);
sb.append("H: ").append(strB).append("/20");
String healthString = sb.toString();

JTextPane txtpnH_1 = new JTextPane();
txtpnH_1.setEditable(false);
txtpnH_1.setFont(new Font("Impact", Font.PLAIN, 30));
txtpnH_1.setText(healthString);
txtpnH_1.setBounds(134, 511, 109, 39);
contentPane.add(txtpnH_1);

String strR = Integer.toString(energy);
String energyString = "E: ";
energyString += strR;
energyString += "/4";

JTextPane txtpnH = new JTextPane();
txtpnH.setEditable(false);
txtpnH.setText(energyString);
txtpnH.setFont(new Font("Impact", Font.PLAIN, 30));
txtpnH.setBounds(39, 511, 85, 39);
contentPane.add(txtpnH);

}


}

非常感谢!!

最佳答案

  1. 花点时间阅读 Code Conventions for the Java Programming Language
  2. 使用适当的布局管理器,请参阅 A Visual Guide to Layout ManagersUsing Layout Managers了解更多详情
  3. 就其值(value)而言,请使用 JTextField 而不是 JTextPane,使用 JTextPane 来实现您想要的效果几乎没有任何好处。正在努力实现。事实上,您实际上可能只使用 JLabel 会更好,因为您不希望它们可编辑
  4. 避免覆盖顶级容器,例如 JFrame,而是从 JPanel 之类的东西开始,在其上构建 UI,然后将其部署到您想要的任何顶级容器.

您遇到的问题是引用问题。在 windowBuild 的构造函数中,您正在定义所有 UI 组件。这意味着您无法在程序的其他任何地方引用它们。相反,制作您需要在其他实例字段中引用的组件。

public class WindowBuild extends JFrame {
//...//
private JTextPane txtpnH_1;
private JTextPane txtpnH;

//...//
public WindowBuild() {
//...//
txtpnH_1 = new JTextPane();
//...//
txtpnH = new JTextPane();
//...//
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
String which = e.getActionCommand();
// Now you can use txtpnH_1.setText and txtpnH.setText
}
}

关于java - 值更改后 JTextPane 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17777785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com