2016-04-25 16 views
0

私のコードは基本的にフレームを持っていてボタンを持っています。ボタンを押すと、長方形を描くことができます。マウスのプレスとマウスのリリースから座標を取得できます。JButtonが押されたときにJFrameがコンポーネントを追加しない

ボタンを削除すると、コードは完全に動作します。ここにコードがあります。

//テストファイル

package ActionTest;  
import java.awt.*; 
import javax.swing.*;  

public class MouseTest 
{ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       JFrame frame = new MouseFrame(); 
       frame.setTitle("MouseTest"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
       frame.setSize(500,500); 
      } 
     }); 
    } 
} 

私のフレーム、コンポーネントクラス

package ActionTest; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class MouseFrame extends JFrame 
{   
    public MouseFrame() 
    {  
     add(new MouseComponent());     
    } 
} 

マウスコンポーネントを呼び出し:マウスクリックを処理し、それが動作長方形に

package ActionTest; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import java.util.*; 
import javax.swing.*; 
public class MouseComponent extends JComponent 
{  
    Point first; 
    Point second; 
    private ArrayList<Rectangle2D> rectangles; 

    public MouseComponent() 
    { 
     rectangles = new ArrayList<>();//contains rectangles 
     addMouseListener(new MouseHandler()); 
    } 
    //paint method of the component, simply re-paint the array 
    public void paintComponent(Graphics g) 
    { 
     Graphics2D g2 = (Graphics2D) g; 
     for (Rectangle2D r : rectangles) 
     g2.draw(r); 
    } 


    /** 
    * creates a rectangle and adds it to the rectangles ArrayList 
    * then repaint the component 
    * inside some operations to deal with rectangle drawing nothing to do with the issue 
    * @param p1: first coordinates 
    * @param p2: second coordinates 
    */ 
    public void addRec(Point2D p1, Point2D p2) 
    { 
     double w, h, x, y; 
     double x1 = p1.getX(); 
     double y1 = p1.getY(); 
     double x2 = p2.getX(); 
     double y2 = p2.getY(); 
     if(x1 <= x2){ 
      x = x1; 
      w = x2-x1;   
     } 
     else{ 
      x = x2; 
      w = x1-x2;   
     } 
     if (y1 <= y2){ 
      y = y1; 
      h = y2-y1;   
     } 
     else{ 
      y = y2; 
      h = y1-y2;   
     } 
     rectangles.add(new Rectangle2D.Double(x, y, w, h)); 
     repaint(); 
    } 


    //records mouse click and mose release 
    //you press the mouse that is the 1st coordinates 
    //you release it that is the 2nd coordinates 
    //pass both to the addRec method 
    private class MouseHandler extends MouseAdapter 
    { 
     @Override 
     public void mousePressed(MouseEvent event) 
     {   
      first = event.getPoint(); 
     } 
     @Override 
     public void mouseReleased(MouseEvent event) 
     { 
      second = event.getPoint();   
      addRec(first, second); 
     } 
    } 
} 

を描きます完全に。しかし、元の問題に戻って、ボタンを追加し、ボタンが押されて矩形描画を開始すると、それは機能しません。ここで

は、変更されたフレームクラスは、事前に

package ActionTest; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class MouseFrame extends JFrame 
{  
    private JPanel buttonPanel; 
    public MouseFrame() 
    {  

     JFrame frame = this; 
     buttonPanel = new JPanel(); 
     JButton rec = new JButton("Rectangle"); 
     rec.addActionListener(new ActionListener(){   
      public void actionPerformed(ActionEvent event) 
      {   
       System.out.println("pressed");     
       frame.add(new MouseComponent());     
      }}); 
     buttonPanel.add(rec); 
     add(buttonPanel);    
    } 
} 

おかげです。

答えて

2
frame.add(new MouseComponent()); 

新しく作成されたコンポーネントのサイズは(0、0)なので、何も描画されません。したがって、表示可能なGUIにコンポーネントを追加するときにレイアウトマネージャを呼び出す必要があります。

frame.add(new MouseComponent()); 
frame.revalidate(); 
frame.repaint(); 

これは、レイアウトマネージャで複数のコンポーネントをフレームに追加できる場合にのみ機能します。フレームのデフォルトのレイアウトマネージャはBorderLayoutで、BorderLayoutのCENTERに追加できるコンポーネントは1つだけです。

だから多分あなたは使用してボタンを追加する必要があります。

frame.add(button, BorderLayout.PAGE_START); 

は、より多くの情報と作業例についてHow to Use Layout Managers上のSwingのチュートリアルからのセクションをお読みください。

また、カスタムペイントを行うときはいつでも、カスタムコンポーネントのgetPreferredSize()メソッドをオーバーライドしてレイアウトマネージャが自分の仕事を行うことができるようにする必要があります。

+0

動作しませんでした。 –

+0

@ThyGamosh、レイアウトマネージャのチュートリアルを読んでください。おそらく、レイアウトマネージャを変更する必要があります。 'getPreferredSize()'についての私のコメントも読んでください。 – camickr

+0

私はそれをチェックします。 –

関連する問題