2017-02-26 8 views
-1

我正在开发一个可以绘制数学函数图形的swing应用程序。我正在使用getGraghics函数,但我不知道如何删除并重新绘制它们,因此我决定重写paintComponent()方法来实现我正在寻找的内容。添加到面板时,paintComponent()不起作用

我想要做的是在面板中绘制函数图用户点击按钮。但它似乎paintCompnent()不起作用。我完全按照任何现有的教程和堆栈溢出类似的问题,但他们都没有为我工作:(它只是没有意义:(

请帮助我坚持这个问题一整个晚上:(

以下是绘制功能图的代码,但由于它不工作,所以我只剩下绘图坐标系的部分进行测试,之后是如何创建实例并尝试将其添加到我的面板中的代码主类

class drawfunction extends JPanel{ 


@Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 


    g.setColor(Color.red); 
    g.drawLine(0, 200, 400, 200); 
    g.drawLine(200,0 , 200, 400); 


} 

}

然后在主类中的代码

 JPanel panel = new JPanel(); 



    panel.setBounds(14, 104, 400, 400); 
    contentPane.add(panel); 

    panel.setBackground(Color.white); 

    JButton btnNewButton = new JButton("View the graph"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 


      //a= Integer.parseInt(cofficient_a.getText()); 
      //b= Integer.parseInt(cofficient_b.getText()); 
      //c= Integer.parseInt(cofficient_c.getText()); 
      //d= Integer.parseInt(cofficient_d.getText()); 
      //e= Integer.parseInt(cofficient_e.getText()); 


     drawfunction a=new drawfunction(); 
     panel.add(a); 



    }); 

谁能告诉我,我应该做些什么来解决这个问题。谢谢 !!!!

+1

好吧,两件事情,一个 - 面板的默认大小为'0x0'和两个 - Swing是懒惰的,你需要调用'revalidate'和'重绘'当你想更新UI – MadProgrammer

+0

另外,类名应该以大写字母开头! – camickr

+0

@MadProgrammer我刚刚尝试使用setPreferredSize()来设置面板的大小,我也尝试添加这两个函数。现在每次按下按钮时都会出现一个小空白方块。 –

回答

2

两个基本的东西...

  1. A组分具有的0x0默认首选大小,所以几乎所有的布局管理器的控制下将它添加一个容器中时会得到它的尺寸0x0(或非常关闭)
  2. Swing通常是懒惰的,当你添加或删除组件时,它不会更新UI,这可能会妨碍性能,因为UI不知道根据你在做什么更新UI, ,您需要拨打revalidate和(大部分时间)repaint才能更新用户界面

例如...

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JPanel center; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      JButton btnNewButton = new JButton("View the graph"); 
      center = new JPanel(); 
      center.setPreferredSize(new Dimension(400, 400)); 
      add(center); 
      btnNewButton.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent arg0) { 

        remove(center); 

        //a= Integer.parseInt(cofficient_a.getText()); 
        //b= Integer.parseInt(cofficient_b.getText()); 
        //c= Integer.parseInt(cofficient_c.getText()); 
        //d= Integer.parseInt(cofficient_d.getText()); 
        //e= Integer.parseInt(cofficient_e.getText()); 
        center = new Drawfunction(); 
        add(center); 
        revalidate(); 
        repaint(); 

       } 

      }); 
      add(btnNewButton, BorderLayout.NORTH); 
     } 

     public class Drawfunction extends JPanel { 

      public Drawfunction() { 
      } 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 400); 
      } 

      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       Graphics2D g2d = (Graphics2D) g.create(); 
       g2d.setColor(Color.red); 
       g2d.drawLine(0, 200, 400, 200); 
       g2d.drawLine(200, 0, 200, 400); 
       g2d.dispose(); 
      } 

     } 

    } 
} 
+0

我的UI实际上是由Windows Builder Pro设计的,它确实使我的Jframe的创建与其他人不同。你的代码可以完美的运行,谢谢你,但我仍然不能,虽然我已经做了你所说的话:( –

+0

嗯,这是一个很好的新机会,沟渠窗口生成器,并开始手动建立你的UI,因为问题在于使用Window Builder – MadProgrammer

+0

是的,听起来像一个很好的计划。谢谢你的回答:) –