2013-11-25 64 views
1

您好我正在嘗試解決以下問題:編寫一個程序,提示用戶使用文本字段輸入中心點和半徑的x和y位置。當用戶點擊「繪圖」按鈕時,在組件中繪製一個帶有該中心和半徑的圓。我沒有看到我的代碼有什麼問題,但是有些事情是因爲它看起來不像repaint()調用paintComponent(),因爲消息將變爲TESTING 1,而不是TESTING 2並且沒有繪製。 我的代碼:repaint()不調用paintComponent()?

import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.applet.*; 
import java.awt.geom.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
public class q3{ 

    public static class cgPanel extends JPanel{ 
     private static double x; 
     private static double y; 
     private static double r; 
     private static JTextField xField; 
     private static JTextField yField; 
     private static JTextField rField; 
     private static JButton draw; 
     private static JLabel message; 
//This is all just Layout work. 
     public cgPanel(){ 
      setLayout(new BorderLayout()); 
      JPanel drawPanel = new JPanel(); 
      drawPanel.setBackground(Color.WHITE); 
      add(drawPanel, BorderLayout.CENTER); 
      message = new JLabel(""); 
      JPanel sub1ForSub1 = new JPanel(); 
      sub1ForSub1.add(message); 
      JLabel coordinates = new JLabel("Coordinates:"); 
      JPanel sub2ForSub1 = new JPanel(); 
      sub2ForSub1.add(coordinates); 
      JPanel subPanel1 = new JPanel(); 
      subPanel1.setLayout(new GridLayout(2, 1)); 
      subPanel1.add(sub1ForSub1); 
      subPanel1.add(sub2ForSub1); 
      JLabel xLabel = new JLabel("x:"); 
      xField = new JTextField(4); 
      JLabel yLabel = new JLabel(" y:"); 
      yField = new JTextField(4); 
      JLabel rLabel = new JLabel(" Radius:"); 
      rField = new JTextField(4); 
      JPanel subPanel2 = new JPanel(); 
      subPanel2.add(xLabel); 
      subPanel2.add(xField); 
      subPanel2.add(yLabel); 
      subPanel2.add(yField); 
      subPanel2.add(rLabel); 
      subPanel2.add(rField); 
      draw = new JButton("Draw"); 
      ActionListener bL = new ButtonListener(); 
      draw.addActionListener(bL); 
      JPanel subPanel3 = new JPanel(); 
      subPanel3.add(draw); 
      JPanel Panel = new JPanel(); 
      Panel.setLayout(new BorderLayout()); 
      Panel.add(subPanel1, BorderLayout.NORTH); 
      Panel.add(subPanel2, BorderLayout.CENTER); 
      Panel.add(subPanel3, BorderLayout.SOUTH); 
      add(Panel, BorderLayout.SOUTH); 
      setVisible(true); 
     } 
     static class ButtonListener extends JComponent implements ActionListener{ 
      public void actionPerformed(ActionEvent e){ 
       try{ 
        String xString = xField.getText(); 
        String yString = yField.getText(); 
        String rString = rField.getText(); 
        message.setText("TESTING 1"); 
        x = Double.parseDouble(xString); 
        y = Double.parseDouble(yString); 
        r = Double.parseDouble(rString); 
        repaint(); 
       } 
       catch (NumberFormatException exception){ 
        message.setText("Please enter a number."); 
       } 
      } 
//This is where I cant seem to get the code in paintComponent to run when the Draw button is pressed. 
      public void paintComponent(Graphics g){ 
       Graphics2D g2 = (Graphics2D) g; 
       Ellipse2D.Double circle = new Ellipse2D.Double(x - r, y - r, r*2, r*2); 
       g2.draw(circle); 
       message.setText("TESTING 2"); 
      } 
     } 
    } 
    public static void main(String[] args){ 
     JFrame frame = new JFrame(); 
     frame.setSize(800, 800); 
     frame.setTitle("Circle Generator"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     cgPanel panel = new cgPanel(); 
     frame.add(panel); 
     frame.setVisible(true); 
    } 
} 

回答

0

所以,幾件事。

你的問題源於你的ButtonListener擴展了一個JComponent,因此repaint()方法正在調用ButtonListener(它實際上不是JComponent)。

paintComponent方法也適用於ButtonListener。相反,您希望您的按鈕偵聽器可以訪問您的cgPanel,因此它可以告訴IT重新繪製。而你的paintComponent需要移動到cgPanel,但即使如此,你可能也不想在那裏,因爲你在cgPanel上有很多其他的組件。

從代碼中不清楚您真的想要繪製圓的地方。

您應該創建一個擴展JPanel的CirclePanel,並重寫paintComponent來繪製您的圈子,然後將其添加到您的cgPanel。然後讓你的ButtonListener告訴CirclPanel實例重繪。

+0

當我嘗試這樣的事情時,我得到一個錯誤,試圖在靜態上下文中使用非靜態方法repaint()。 – Reddy

相關問題