2017-02-16 6 views
0

DrawPanel内のペイントコンポーネントの色を変更する方法がわかりません。JPanelのpaintコンポーネントをJavaのボタンパネルから変更する方法

使用する必要があるメソッドは、ButtonPanelを使用してペイントコンポーネントの色を変更できることです。

問題は、ハンドラにパネルを認識させて色を変更させる方法が見つからないように見えることです。

JavaでボタンパネルからのJPanelでpaintcomponentを変更する方法

main.java

import javax.swing.SwingUtilities; 

public class main { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
       new Window(); 
      }   
     });  
    } 
} 

Window.java

import javax.swing.JFrame; 
import java.awt.BorderLayout; 
import java.awt.Dimension; 

public class Window extends JFrame { 

    public Window() { 
     // `super ' calls a function inherited from the parent class (JFrame) 
     super(); 
     setTitle("Callbacks"); 
     setSize (new Dimension(420, 350)); 

     // Make sure the window appears in the middle of your screen 
     setLocationRelativeTo(null); 

     // Determines what should happen when the frame is closed 
     setDefaultCloseOperation (EXIT_ON_CLOSE); 

     // Chooses a certain layout type for the elements in this frame 
     getContentPane().setLayout (new BorderLayout()); 

     // TODO : add elements to the content pane 
     DrawPanel dp = new DrawPanel(); 
     ButtonPanel bp = new ButtonPanel(); 

     // Places the DrawPanel in the center of the frame 
     getContentPane(). add (dp , BorderLayout . CENTER); 
     // Places the ButtonPanel in the top of the frame 
     getContentPane(). add (bp , BorderLayout . NORTH); 


     // Set the window to visible ! Yup ... This is necessary 
     setVisible (true); 
    } 
} 

DrawPanel.java

import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.*; 

public class DrawPanel extends JPanel { 

    private Color color; 

    public DrawPanel(){ 
     super(); 
     color = Color.BLACK ; 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(getColor()); 
     g.fillRect(100 , 30, 200 , 200); 
    } 

} 

ButtonPanel.java

import javax.swing.JButton; 
import javax.swing.JPanel; 

public class ButtonPanel extends JPanel { 


    public ButtonPanel() { 
     super(); 
     // Add a button to the panel . The argument to the JButton constructor 
     // will become the text on the button . 
     JButton b = new JButton ("Change color!"); 
     JButton c = new JButton ("Test"); 
     add (b); 
     add (c); 
     b.addActionListener(new InputHandler()); 

    } 
} 

InputHandler

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

import com.sun.prism.paint.Color; 

public class InputHandler implements ActionListener { 

    public InputHandler() { 
     // TODO Auto-generated constructor stub 
    } 

    public void actionPerformed (ActionEvent e) { 
     // TODO : add code here that will 
     // be ran when the button is clicked 
    } 
} 

は君たちが私を助けたり、正しい方向に私を送ることができます願っています。

+1

大きな問題は、実際にコードを実行する必要がないときにコードがコンポーネントを拡張していることです。これは 'Window'と' ButtonPanel'の両方に当てはまります( 'DrawPanel'を拡張する論理的なケースがありますが)。 –

+0

それは私が取り組んでいる課題の一部です。私が答えを探していたとき、彼らはどちらも伸びなかった。 – Danton

+0

あなたが共有したいデータを表す相互契約(すなわち 'interface')で2つを結合し、関係者がデータの変更を監視して自分自身を更新できるようにオブザーバーパターンを提供します – MadProgrammer

答えて

0

ListenerPanelを知っているように、それらが互いに友人ください:

// Window 
DrawPanel dp = new DrawPanel(); 
ButtonPanel bp = new ButtonPanel (dp); // pass panel to button 

その後:

public class ButtonPanel extends JPanel {  
    public ButtonPanel (DrawPanel d) { 
     super(); 
     // Add a button to the panel . The argument to the JButton constructor 
     // will become the text on the button . 
     JButton b = new JButton ("Change color!"); 
     JButton c = new JButton ("Test"); 
     add (b); 
     add (c); 
     b.addActionListener(new InputHandler(d)); // pass panel to listener  
    } 
} 

と:その後、

public class InputHandler implements ActionListener { 
    DrawPanel d; 
    public InputHandler(DrawPanel d) { 
     this.d = d; 
    }  
    public void actionPerformed (ActionEvent e) { 
     d.setColor(Color.BLUE); // whatever 
     d.repaint(); // force painting 
    } 
} 

public class DrawPanel extends JPanel { 
    private Color color;  
    public DrawPanel(){ 
     super(); 
     color = Color.BLACK ; 
    } 
    public void setColor(Color c) { 
     this.color = c; 
    } 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(getColor()); 
     g.fillRect(100 , 30, 200 , 200); 
    }  
} 
+0

ありがとう! このプロジェクトに関する最後の質問の1つは、ボタンプレスで形状を変更できるかどうかです。塗料成分を使用する必要がありますか? – Danton

+0

もちろん、シェイプは 'paintComponent'の外部に記述する必要があります。 –

関連する問題