2015-10-14 33 views
0

最近我决定在Java中使用JFrames,JPanels等。正如话题所说,我不知道如何为不同的按钮设置不同的操作。为了练习,我想写一个小程序,按下不同的按钮时应该绘制不同的形状。但不幸的是,它不起作用 - 点击按钮后,绘画形状不会执行。有没有更有效的方法来做到这一点?有人能指出错误在哪里吗?如何处理JButtons的多个动作

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

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

public class GUI { 

    public JFrame frame; 
    public JPanel panel; 
    public JButton button1, button2; 
    public final static String COMMAND_FOR_BUTTON_1 = "COMMAND_FOR_BUTTON_1"; 
    public final static String COMMAND_FOR_BUTTON_2 = "COMMAND_FOR_BUTTON_2"; 
    public int whatToDraw = 0; 
    public paintComponent pc; 

    public void initFrame() { 
     frame = new JFrame(); 
     frame.setTitle("Let's paint something"); 
     frame.setSize(300, 400);   
     frame.setVisible(true); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void initPanel() { 
     panel = new JPanel(); 
    } 

    public void initButtons() { 
     button1 = new JButton("Square"); 
     button1.setActionCommand(COMMAND_FOR_BUTTON_1); 
     button2 = new JButton("Circle"); 
     button2.setActionCommand(COMMAND_FOR_BUTTON_2); 

     button1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) {  
       if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_1)) { 
        whatToDraw = 1; 
        pc.repaint(); 
       } 
      } 
     }); 

     button2.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_2)) { 
        whatToDraw = 2; 
        pc.repaint(); 
       } 
      } 
     }); 
    } 

    @SuppressWarnings("serial") 
    class paintComponent extends JComponent{ 
     public void paint(Graphics g){ 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      Random rand = new Random(); 
      int radius = rand.nextInt(80)+40; 

      if(whatToDraw == 1) { 
       g2.setColor(Color.BLUE); 
       g2.fillOval(rand.nextInt(getWidth()-radius), rand.nextInt(getHeight())-radius, radius, radius); 
      } 
      if(whatToDraw == 2) { 
       g2.setColor(Color.GREEN); 
       g2.fillRect(rand.nextInt(getWidth()-radius), rand.nextInt(getHeight())-radius, radius, radius); 
      } 
     } 
    } 

    public void createGUI() { 
     initFrame(); 
     initPanel(); 
     initButtons(); 
     pc = new paintComponent(); 
     panel.add(button1); 
     panel.add(button2); 
     frame.add(pc, BorderLayout.CENTER); 
     frame.add(panel, BorderLayout.PAGE_START); 
    } 

    public static void main(String[] args) { 
     GUI gui = new GUI(); 
     gui.createGUI(); 
    } 
} 
+0

什么是 “它不工作” 是什么意思?你有什么异常? –

+0

更正,“......点击按钮后,绘画形状不执行......” – all0star

+0

在你的按钮,你打电话repaint。你打算叫油漆。另外,不要使用绘画方法来启动按钮。为什么不在构造函数中这样做? – user2651804

回答

1
public void paint(Graphics g){ 
     initButtons(); <------------------- delete 
     Graphics2D g2 = (Graphics2D) g; 
     if(whatToDraw == 1) { 
      g2.setColor(Color.BLUE); 
      g2.fillOval(30, 30, 30, 30); 
      repaint(); <------------------- delete 
     } 
     if(whatToDraw == 2) { 
      g2.setColor(Color.GREEN); 
      g2.fillRect(30, 30, 30, 30); 
      repaint(); <------------------- delete 
     } 
    } 

    @Override 
     public void actionPerformed(ActionEvent e) {  
      if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_1)) {//FredK suggested in comments you remove this line. 
       whatToDraw = 1; 
       pc.repaint() 
      } 
     } 
    }); 

    button2.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      whatToDraw = 2;//removed it would like like this, and do the same. Your solution would be neccessary if 2 buttons shared the same actionListener. 
      pc.repaint(); 
     } 
    }); 

编辑

public void createGUI() { 
    initFrame(); 
    initPanel(); //put your initButtons() method here 
    pc = new paintComponent(); 
    panel.add(button1); //these could go in initPanel() too 
    panel.add(button2); //^ 
    frame.add(pc, BorderLayout.CENTER);//your JFrame has a default layout of BorderLayout. 
    frame.add(panel, BorderLayout.PAGE_START); 
} 
+0

我已经更改了代码。不幸的是,这个问题依然存在。 – all0star

+0

事实上,我们走得很顺利。现在唯一的问题是正确地将你的paint组件添加到你的JFrame中。 – user2651804

+1

目前,你的paintComponent没有大小。使用布局可以解决这个问题。让我们给你的JFrame一个borderLayout,然后将你的paintComponent添加到中心。 – user2651804

相关问题