2010-08-09 101 views
1

我想對一些不支持swing的舊java代碼做一些小的補充。我需要添加一個小對話框,其中包含一個具有複選框和幾個文本字段的面板。當用戶點擊複選框時,我想禁用或啓用複選框。這部分似乎工作正常,但文本字段不正確重繪。當我點擊複選框時,這些字段看起來並未啓用,但如果我然後單擊面板或文本字段,就會看到它們已啓用(相反也是如此,當我取消選中複選框時,字段仍處於啓用狀態直到你嘗試點擊它們,它們變成幻影並且不被選中)。我使用setEnabled(boolean)來設置字段的狀態。我已經嘗試調用repaint並在更改狀態後在字段和麪板上進行驗證,這似乎不起作用。我也試圖讓這些領域要求專注,並且這不起作用。任何人有任何其他想法?AWT重繪問題

//The class that contains all of this is of type Window 
//Declaration of the components 
    private Panel _inputPanel; 
    private TextField min , max; 
//This method adds to two text fields 
public void addMinMaxtextFields(String min, String max) { 
    TextField minField = new TextField(min); 
    TextField maxField = new TextField(max); 

    this.min = minField; 
    this.max = maxField; 
    this.min.setEnabled(false); 
    this.max.setEnabled(false); 
    _inputPanel.add(minField); 

    _inputPanel.add(maxField); 


} 
//listener for the checkbox 
public void itemStateChanged(ItemEvent e) { 
    Component[] components = _inputPanel.getComponents(); 

    min.setEnabled(!min.isEnabled()); 
    min.setVisible(true); 
    min.validate(); 
    min.repaint(); 

    _inputPanel.validate(); 
    _inputPanel.repaint(); 
    this.pack(); 

    this.setSize(this.getWidth(), this.getHeight()); 

    this.validate(); 

    this.repaint(); 
    /* do nothing */ 
} 
+0

你能添加一些代碼片段嗎? – 2010-08-09 17:38:21

+0

對不起,我有一個緊急情況,已經離開,我添加了代碼片段。 – Mike2012 2010-08-11 20:47:19

回答

1

您需要的setEnabled後(布爾)面板上調用update(圖形克)調用。

檢查: http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#update(java.awt.Graphics)

我嘗試下面的代碼(從您提供的代碼建),其工作的罰款。

import java.awt.Checkbox; 
import java.awt.Component; 
import java.awt.Dialog; 
import java.awt.Frame; 
import java.awt.Panel; 
import java.awt.TextField; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 

public class CheckUI extends Dialog implements ItemListener { 

    // The class that contains all of this is of type Window 
    // Declaration of the components 
    private Panel _inputPanel; 
    private TextField min, max; 
    private Checkbox cb; 

    public CheckUI(Frame owner, boolean modal) { 
     super(owner, modal); 
     _inputPanel = new Panel(); 

     this.add(_inputPanel); 
     addMinMaxtextFields("min", "max"); 
    } 


    // This method adds to two text fields 
    public void addMinMaxtextFields(String min, String max) { 
     cb = new Checkbox(); 
     cb.addItemListener(this); 
     TextField minField = new TextField(min); 
     TextField maxField = new TextField(max); 

     this.min = minField; 
     this.max = maxField; 
     this.min.setEnabled(false); 
     this.max.setEnabled(false); 
     _inputPanel.add(minField); 

     _inputPanel.add(maxField); 
     _inputPanel.add(cb); 

    } 

    // listener for the checkbox 
    public void itemStateChanged(ItemEvent e) { 
     Component[] components = _inputPanel.getComponents(); 

     min.setEnabled(!min.isEnabled()); 
     min.setVisible(true); 
     min.validate(); 
     min.repaint(); 

     _inputPanel.validate(); 
     _inputPanel.repaint(); 
     this.pack(); 

     this.setSize(this.getWidth(), this.getHeight()); 

     this.validate(); 

     this.repaint(); 
     /* do nothing */ 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Frame parent = new Frame(); 
     parent.setVisible(true); 
     parent.setExtendedState(Frame.MAXIMIZED_BOTH); 
     parent.pack(); 

     CheckUI ui = new CheckUI(parent, true); 
     ui.pack(); 
     ui.setVisible(true); 

    } 

} 
+0

嗯,這似乎並沒有伎倆。感謝您的建議。 – Mike2012 2010-08-09 18:27:01

+0

@ Mike2012你能分享一下你的代碼嗎? – YoK 2010-08-10 05:19:29

+0

@ Mike2012我不認爲這是完整的代碼。我還需要了解完全缺失的複選框相關代碼。 – YoK 2010-08-12 14:39:42