通过单击按钮在Jframe上绘制一条线

时间:2014-02-16 15:59:32

标签: java swing jframe jpanel paint

我想通过点击JFramepushButton上画一条线。

我在下面写了这段代码,但它不起作用。如果有人能帮助我解决这个问题,我将非常感激。

鲁宾

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testDrawing {

      int x = 0;
      int y = 0;
      int x1 = 0;
      int y1 = 0;

      JFrame frame=new JFrame();
      DrawPanel draw=new DrawPanel();

      public  testDrawing() {

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(800,600);
          frame.setVisible(true);

          JButton btntest = new JButton("Draw a line");
          btntest.setBounds(380, 100, 100, 20);
      frame.add(btntest);

      btntest.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {

                 x =  100;
                 y =  100; 
                 x1 = 150;
                 y1 = 130;

                 executeAction(); 

             }  
        });

       }  

      public void executeAction(){

          frame.getContentPane().add(draw);

          x =  100;
                  y =  100; 
                  x1 = 150;
                  y1 = 170;

          draw.repaint();  

                  try{
                  Thread.sleep(30);
                  }catch(Exception e)
                  {}
      }

    class DrawPanel extends JPanel{

        public void paintComponent(Graphics g)
           { 

            g.drawLine(x, y, x1, y1);

           }
    }


     public static void main(String[] args) {

             SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {

                 testDrawing test = new testDrawing();

            //   test.executeAction(); with this line uncommented the drawing is performed,
            //                         but the pushBotton event doesn't work

             }
          });

     }
}

3 个答案:

答案 0 :(得分:0)

您的按钮覆盖整个JFrame。如果要在按钮上绘制线条,则必须创建javax.swing.Icon的实现,使用该图标的实例作为按钮图标,然后在绘制线条时编辑图标。

但是,我认为没有理由在这里使用按钮。只需在JPanel上绘画。 See this thread

答案 1 :(得分:0)

您应该添加适当的布局管理器并预先安排您的项目(docs.oracle.com/javase/tutorial/uiswing/layout/box.html)

答案 2 :(得分:0)

只需添加标记boolean draw = false即可。在paintComponent方法if(draw)中使用它,并在actionPerfomed

中更新它

并且使用Thread.sleep,您将阻止EDT

您也不需要从super.paintComponent方法调用paintComponent


这是我正在谈论的一个例子

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Example {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DrawPanel());
            }
        });
    }

    private static class DrawPanel extends JPanel {
        private boolean draw = false;
        int x1, y1, x2, y2;

        public DrawPanel() {
            JButton button = new JButton("Draw");
            button.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    x1 = 50; y1 = 50; x1 = 200; y2 = 300;
                    draw = true;
                    repaint();
                }
            });
            add(button);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(0, 0, getWidth(), getHeight());
            if (draw) {
                g.setColor(Color.GREEN);
                g.drawLine(x1, y1, x2, y2);
            }  
        }

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

旁注

  • 使用Java命名约定。名为begin的大写字母。
相关问题