2016-07-24 51 views
1

这是我的主类:Java不借鉴的JPanel

public class Sad { 

    private JFrame frame; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Sad window = new Sad(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Sad() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 512, 399); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new CardLayout(0, 0)); 

     JPanel panel = new JPanel(); 
     frame.getContentPane().add(panel, "name_12361565901507"); 
     panel.setLayout(null); 

     JButton btnNes = new JButton("Nes"); 
     btnNes.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       Grafik grafik = new Grafik(20, 20, 100); 
       panel.add(grafik); 
      } 
     }); 
     btnNes.setBounds(90, 146, 89, 23); 
     panel.add(btnNes); 
    } 


} 

而且这是借鉴

public class Grafik extends JPanel{ 

    private int x; 
    private int y; 
    private int r; 

    public Grafik(int x, int y, int r){ 
     this.x = x; 
     this.y = y; 
     this.r = r; 
    } 

    public void paintComponent(Graphics g) { 
     Graphics2D g2 =(Graphics2D) g; 

     Ellipse2D circ = new Ellipse2D.Float(x, y, r, r); 
     g2.setColor(Color.RED); 
     g2.draw(circ); 


     } 

} 

他们在同一个包。而当我点击按钮它的阴影绘制椭圆红色,但它不显示任何东西。有人能帮我吗? BTW对不起,英语不好

+0

不要使用'null'布局。 – trashgod

回答

3

这是因为你不叫panel.setBounds(),revalidate()repaint()

  • 但是,您不应该使用空布局:无论如何使用layout managers
  • 您应该在 的paintComponent方法开始时致电super.paintComponent(g)
  • 在每次按下按钮之后,您可能只想在Grafik实例内切换布尔值,以确定椭圆是否可见,而不是在面板中添加新组件。
  • 如果您想使椭圆“平滑”,您可以拨打g2.setRenderingHint(hintKey, hintValue)

修改后的代码,包括我的建议:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Ellipse2D; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Sad { 

    private JFrame frame; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Sad window = new Sad(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Sad() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 512, 399); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Grafik grafik = new Grafik(20, 20, 100); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout()); 
     panel.add(grafik); 

     JButton btnNes = new JButton("Nes"); 
     btnNes.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       grafik.setEllipseVisible(true); 
       panel.repaint(); 
      } 
     }); 

     JPanel btnPanel = new JPanel(); 
     btnPanel.add(btnNes); 
     panel.add(btnPanel, BorderLayout.SOUTH); 

     frame.setContentPane(panel); 
    } 

} 

class Grafik extends JPanel { 

    private int x; 
    private int y; 
    private int r; 
    private boolean ellipseVisible; 

    public Grafik(int x, int y, int r) { 
     this.x = x; 
     this.y = y; 
     this.r = r; 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (isEllipseVisible()) { 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

      Ellipse2D circ = new Ellipse2D.Float(x, y, r, r); 
      g2.setColor(Color.RED); 
      g2.draw(circ); 
     } 
    } 

    public boolean isEllipseVisible() { 
     return ellipseVisible; 
    } 

    public void setEllipseVisible(boolean ellipseVisible) { 
     this.ellipseVisible = ellipseVisible; 
    } 

}