2013-07-21 112 views
0

代碼位於下方。基本上我想要做的是在JTextPane的JPanel中進行顯示。我有一個按鈕,它編輯應該顯示在JTextPane中的字符串的值。但我無法弄清楚如何更新JTextPane。我試過重新驗證(),驗證(),重繪(),這些似乎沒有工作。JTextPane在值更改後未更新

代碼完整,應該可以運行。

import 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); 

} 


} 

非常感謝!

+0

你試過txtpnH.setText(newInfo)嗎? –

+1

'setLayout(null)'是我第一個關注的領域,這會讓你長期以來更加困難 – MadProgrammer

+0

@MadProgrammer除非你的代碼是由IDE生成的。 – tbodt

回答

2
  1. 花時間通過Code Conventions for the Java Programming Language
  2. 利用適當的佈局管理器閱讀,看A Visual Guide to Layout ManagersUsing Layout Managers更多細節
  3. 對於它的價值,使用JTextField代替JTextPane,你獲得一點JTextPane對於您似乎試圖實現的目標沒有任何好處。事實上,你實際上可能是我們的好只是用JLabel,視爲你不想讓他們爲可編輯
  4. 避免覆蓋頂層容器,像JFrame,而是用類似JPanel開始,建立在它的用戶界面和然後將其部署到您想要的任何頂級容器。

您遇到的問題是一個參考問題。在您的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 
     } 
    } 
+0

好的。非常感謝!! – jdawg495