Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 值更改后JTextPane未更新_Java_Swing_Jtextpane - Fatal编程技术网

Java 值更改后JTextPane未更新

Java 值更改后JTextPane未更新,java,swing,jtextpane,Java,Swing,Jtextpane,代码在下面。基本上,我要做的是在JTextPane的JPanel中进行显示。我有一个按钮,用于编辑应该显示在JTextPane中的字符串的值。但是,我不知道如何更新JTextPane。我尝试过重新验证()、验证()、重新绘制(),但这些似乎都不起作用 代码是完整的,它应该能够运行 导入java.awt.Canvas public class windowBuild extends JFrame { /** * */ private static final long serialVers

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

代码是完整的,它应该能够运行

导入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);

}


}
非常感谢

  • 花点时间通读这本书
  • 使用适当的布局管理器,有关详细信息,请参阅和
  • 为了实现它的价值,使用
    JTextField
    而不是
    JTextPane
    ,使用
    JTextPane
    来实现您似乎想要实现的目标几乎没有任何好处。事实上,您可能实际上比我们更喜欢使用
    JLabel
    ,因为您不希望它们是可编辑的
  • 避免覆盖顶级容器,如
    JFrame
    ,而是从
    JPanel
    开始,在其上构建UI,然后将其部署到所需的顶级容器
  • 您遇到的问题是参考问题。在
    windowBuild
    的构造函数中,定义所有UI组件。这意味着,您无法通过程序在其他任何地方引用它们。相反,使这些组件需要引用其他where实例字段

    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
            }
        }
    

    你试过txtpnH.setText(newInfo)吗?
    setLayout(null)
    是我关注的第一个领域,这只会让你在长期内变得更困难run@MadProgrammer除非您的代码是由IDE生成的。@tbodt Still,当你第一次尝试在一个不是专门设计的系统上运行它时,它会在你脸上爆炸。我以前有过使用计算器的经验。它在IDE中看起来是正确的,但在现实生活中并非如此。所以,投反对票——好奇地想知道为什么?它怎么不回答这个问题呢?如何改进?由于我没有看到任何其他答案,我只能猜测,要么你不理解问题,要么你不理解答案:(