2014-02-16 23 views
0

我想通過點擊pushButtonJFrame上畫一條線。通過點擊pushBotton在Jframe上畫一條線

我寫了下面的代碼,但它不起作用。如果有人能幫我解決這個問題,我將非常感激。

魯賓

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 

      } 
      }); 

    } 
} 

回答

0

你的按鈕覆蓋整個JFrame中。如果要在按鈕上繪製線條,則必須創建javax.swing.Icon的實現,將該圖標的實例用作按鈕圖標,然後在繪製線條時編輯該圖標。

但是,我看不出在這裏使用按鈕的理由。只需在JPanel上繪製即可。 See this thread

0

您應該添加適當的佈局管理器,並安排您的項目前期(docs.oracle.com/javase/tutorial/uiswing/layout/box.html)

0

只需添加一個標誌boolean draw = false。使用它在paintComponent方法if(draw)並在actionPerfomed

而且更新不要使用Thread.sleep,你會阻止EDT

也不需要從paintComponent方法調用super.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類。