2012-08-31 26 views
5

मैं एक उस में साथ है। जब मैं PaintPanel (लेकिन बटन पर क्लिक करके repaint() का आह्वान करता हूं) ButtonPanel का बटन PaintPanel में चित्रित किया जा रहा है! यह क्लिक करने योग्य या कुछ भी नहीं है, यह सिर्फ वहां है।जेबटन ने मरम्मत के दौरान प्रतिलिपि बनाई? <code>ButtonPanel</code> एक <code>PaintPanel</code> (एक <code>paint()</code> विधि के साथ) और एक (बटन के साथ):

public class Main { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("frame"); 
     frame.setSize(400,400); 
     frame.setLayout(new GridLayout(2,1)); 
     PaintPanel paint = new PaintPanel(); 
     ButtonPanel buttons = new ButtonPanel(paint); 
     frame.add(paint); 
     frame.add(buttons); 
     frame.setVisible(true); 
    } 
} 

public class PaintPanel extends JPanel{ 
    public void paint(Graphics g){ 
     g.drawRect(10, 10, 10, 10); 
    } 
} 

public class ButtonPanel extends JPanel implements ActionListener{ 

    private PaintPanel paintPanel; 

    public ButtonPanel(PaintPanel paintPanel){ 
     this.paintPanel=paintPanel; 
     JButton button = new JButton("button"); 
     button.addActionListener(this); 
     add(button); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     paintPanel.repaint();   
    } 
} 

यह समस्या मेरे पास है पुन: बनाने सकता था (विषम कोड चिह्नों के लिए खेद है, यह सही पाने के लिए नहीं कर पा रहे):

मैं इस कोड के साथ समस्या को पुन: करने की कोशिश की।

मैं सच में आशा है कि आप में से एक जानता है कि यहाँ क्या हो रहा है, क्योंकि मैं नहीं ...

+1

इस तरह के प्रतिपादन कलाकृतियों को अक्सर सम्मानित करने के लिए [अस्पष्टता] में नाकाम रहने से उत्पन्न होती हैं (http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) संपत्ति। इसके अलावा, "स्विंग कार्यक्रमों को 'पेंट()' ओवरराइड करने के बजाय 'पेंटकंपोनेंट()' ओवरराइड करना चाहिए।" - [* एडब्ल्यूटी और स्विंग में चित्रकारी: पेंट विधि *] (http://java.sun.com/products/jfc /tsc/articles/painting/index.html#callbacks)। – trashgod

उत्तर

6

सबसे पहले, आप paintComponent() बजाय paint() ओवरराइड करना चाहिए। कुछ पैनल अनुकूलन करने की बात आती है जब यह स्विंग में सर्वोत्तम प्रथाओं का हिस्सा है।

दूसरे, यहां कोड है कि मेरे लिए काम करता (: एस मैं नहीं जानता कि क्यों तुम्हारा हालांकि नहीं करता है): है

public class Main { 

    public static void main(String[] args) { 

     JFrame frame = new JFrame("frame"); 
     frame.setSize(400, 400); 
     // frame.setLayout(new GridLayout(2, 1)); 
     PaintPanel paint = new PaintPanel(); 
     ButtonPanel buttons = new ButtonPanel(paint); 
     // frame.add(paint); 
     // frame.add(buttons); 
     frame.setVisible(true); 

     JPanel pan = new JPanel(new BorderLayout()); 
     pan.add(paint); 
     pan.add(buttons, BorderLayout.SOUTH); 
     frame.add(pan); 

    } 
} 

class PaintPanel extends JPanel { 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(new Color(new Random().nextInt())); 
     g.drawRect(10, 10, 10, 10); 
    } 
} 

class ButtonPanel extends JPanel implements ActionListener { 

    private final PaintPanel paintPanel; 

    public ButtonPanel(PaintPanel paintPanel) { 

     this.paintPanel = paintPanel; 
     JButton button = new JButton("button"); 
     button.addActionListener(this); 
     add(button); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     if (getParent() != null) { 
      getParent().repaint(); 
     } 
    } 
} 
+1

अक्सर आप super.paintComponent (जी) चाहते हैं; आपके ओवरराइड पेंट कॉम्पोनेंट विधि में पहली पंक्ति के रूप में। यह उस सब कुछ को साफ़ कर देगा जिसे आप उस विशेष कॉल में चित्रित नहीं कर रहे हैं। – mrranstrom

+0

@mrranstrom यह सही है! मैंने अपना उदाहरण तय किया, धन्यवाद! चित्र के लिए – aymeric

+2

+1 कॉम्पोनेंट और super.paintCompont। @aymeric आपको याद रखना होगा, ग्राफिक्स ऑब्जेक्ट का पुन: उपयोग किया जाता है, इसलिए यदि आप यह सुनिश्चित करने के लिए समय नहीं लेते हैं कि आपने इसे पहले साफ़ कर लिया है, तो आप 'super.paintComponent' का उपयोग करने के लिए अप्रत्याशित पेंट कलाकृतियों – MadProgrammer