2016-07-10 6 views
0

私はjavaで新しく、私はjava swingを使って簡単なペイントプログラムを作ろうと思っていました。 私の簡単なペイントプログラムは、ボタンをクリックするたびに、三角形、円形、正方形のような形を描く必要があります。 私はこれらの図形を描画し、ボタンなしで印刷することができましたが、私はActionListenerを使用してそれを行うことはできませんか?java Swing button action

私は現時点では1つのボタンしか持っていないので、このボタンをクリックするたびに楕円を描きたいと思います。

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


public class PaintProject extends JComponent implements ActionListener{ 
    public static void main(String[] args) { 


     JFrame frame=new JFrame("NEW PAINT PROGRAME!"); 
     JButton button1=new JButton("ADD"); 
     PaintProject paint=new PaintProject(); 

     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout()); 
     frame.add(paint); 
     frame.add(button1); 

     frame.pack(); 
     frame.setVisible(true); 

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

    } 
    @Override 
    protected void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillOval(0,0, 100, 100); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 


    } 


} 
+0

[ボタンをクリックした後にのみJFrameペイントを作成する方法は?](http://stackoverflow.com/questions/23752636/how-to-make-jframe-paint-only-after-i-クリックボタン) –

答えて

0

では、次の手順を実行してもらえ:

ステップ1: これは私がこれまでにそれに取り組んでいますコードです

挿入button1.addActionListener(paint);直後main方法でPaintProject paint=new PaintProject();PaintProject.java

手順2:

protected void paintComponent(Graphics g)という名前のメソッドを削除します。次のように

コール上記の方法:

@Override 
public void actionPerformed(ActionEvent e) { 
    drawOval();  
} 

EDIT:

次の例では、2つを描画する方法を示し

private void drawOval(){ 
    Graphics g = this.getGraphics(); 
    g.setColor(Color.red); 
    g.fillOval(0,0, 100, 100);   
} 

ステップ3:代わりに次のメソッドを作成しますそれぞれのボタンがクリックされると形状:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

public class PaintProject extends JComponent implements ActionListener { 
    public static void main(String[] args) { 

     JFrame frame = new JFrame("NEW PAINT PROGRAME!"); 
     JButton ovalButton = new JButton("Oval"); 
     ovalButton.setActionCommand("Oval"); 

     JButton rectangleButton = new JButton("Rectangle"); 
     rectangleButton.setActionCommand("Rectangle"); 

     PaintProject paint = new PaintProject(); 
     ovalButton.addActionListener(paint); 
     rectangleButton.addActionListener(paint); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout()); 
     frame.add(paint); 
     frame.add(ovalButton); 
     frame.add(rectangleButton); 

     frame.pack(); 
     frame.setVisible(true); 

    } 

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

    } 

    private void drawOval() { 
     Graphics g = this.getGraphics(); 
     g.setColor(Color.red); 
     g.fillOval(0, 0, 100, 100); 
    } 

    private void drawRectangle() { 
     Graphics g = this.getGraphics(); 
     g.setColor(Color.green); 
     g.fillRect(150, 150, 100, 100); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 
     if (command.equals("Oval")) { 
      drawOval(); 
     } else if (command.equals("Rectangle")) { 
      drawRectangle(); 
     } 

    } 

} 
+0

ありがとうございました、もう一つの問題は、私がどのように私はそれをactionlistenerで変更する必要があります矩形を描画する別のボタンを追加しますか?私は4つのボタンを持っていて、すべてのボタンは今私のために同じ形を描いています –

+0

@ nah1991答えの 'EDIT'セクションを見てください、あなたは手がかりを見つけるのは間違いありません。ところで、あなたは間違って答えを受け入れていません。 –

+0

ありがとうたくさん:) –