添加到面板

时间:2017-02-26 05:28:36

标签: java swing graphics windows-applications

我正在使用一个可以绘制数学函数图形的swing应用程序。我正在使用getGraghics函数,但我不知道如何删除和重绘它们所以我决定覆盖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 个答案:

答案 0 :(得分:2)

两件基本的事情......

  1. 组件的默认首选大小为0x0,因此在添加它时,几乎任何布局管理器控制的容器都会将其大小设置为0x0(或非常接近)
  2. Swing通常是懒惰的,当你添加或删除组件时它不会更新UI,这会阻碍性能,因为UI根据你正在做的事情不知道最好更新UI​​,相反,你我需要调用revalidate和({大多数情况下)repaint来更新用户界面
  3. 例如......

    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();
                }
    
            }
    
        }
    }