2013-05-12 78 views
-1

所以我有下面的代码,每当我按下方形工具按钮,它应该在左手边画一个方形,但我甚至不能让控制台打印它的消息。这让我认为actionListener从来没有响应我的点击。任何人都可以帮忙吗?ActionListener和JButtons

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenuBar; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Game extends JPanel implements ActionListener { 

private static final long serialVersionUID = 1L; 

private GridPane gridPane; 

private DragPanel drag; 

    public boolean isMouseClicked = false; 

    public static JMenuBar bar = new JMenuBar(); 

    public int gridY = 1; 
    public int gridX = 1; 

    public int x = 0,y = 0; 


    public Game() { 
     setLayout(new BorderLayout()); 

     OptionPanel options = new OptionPanel(); 
     options.addActionListener(this); 
     add(options, BorderLayout.NORTH); 


     gridPane = new GridPane(); 
     gridPane.setBorder(BorderFactory.createLineBorder(Color.white)); 
     add(gridPane); 

     drag = new DragPanel(); 
     drag.setBorder(BorderFactory.createLineBorder(Color.white)); 
     drag.setBackground(new Color(100,100,125)); 
     add(drag,BorderLayout.WEST); 

} 

public static void main(String args[]) { 
    Game game = new Game(); 

    JFrame frame = new JFrame(); 
    frame.setVisible(true); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    frame.setTitle("Game"); 
    frame.setAlwaysOnTop(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    frame.setLayout(new BorderLayout()); 
    frame.add(game); 


} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getActionCommand().equalsIgnoreCase("grid")) { 
     gridPane.setGridOn(!gridPane.isGridOn()); 
    } 

    if (e.getActionCommand().equalsIgnoreCase("square")){ 
     gridPane.setSqaureOn(!gridPane.isSquareOn()); 
    } 
    if (e.getActionCommand().equalsIgnoreCase("vgrid")){ 
     gridPane.setVertOn(!gridPane.isVertOn()); 
    } 
    } 

public class GridPane extends JPanel { 


    private static final long serialVersionUID = 1L; 
    private boolean gridOn = false; 
    private boolean squareOn = false; 
    private boolean vertOn = false; 

    public GridPane() { 
     setBackground(Color.BLACK); 
    } 

    public boolean isGridOn() { 
     return gridOn; 
    } 

    public boolean isSquareOn(){ 

     return squareOn; 
    } 

    public boolean isVertOn(){ 

     return vertOn; 
    } 

    public void setGridOn(boolean value) { 
     if (value != gridOn) { 
      this.gridOn = value; 
      repaint(); 
     } 
    } 

    public void setVertOn(boolean value){ 

     if (value != vertOn){ 
      this.vertOn = value; 
      repaint(); 
     } 
    } 

    public void setSqaureOn(boolean value){ 
     if (value != squareOn){ 
      this.squareOn = value; 
      repaint(); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Toolkit tk = Toolkit.getDefaultToolkit(); 

     if (gridOn) { 
      System.out.println("Grid works"); 
      g.setColor(Color.white); 
      for (int i = 0; i < tk.getScreenSize().height; i += 64){ 
       gridY++; 
       g.drawLine(0, (64 * gridY), tk.getScreenSize().width,(64 * gridY)); 
      } 
     } 

     gridY = -1; 

     gridX = -1; 



     if (vertOn){ 
      System.out.println("vert grid works"); 
      g.setColor(Color.white); 
      for (int ig = 0; ig < tk.getScreenSize().width; ig += 64){ 
       gridX++; 
       g.drawLine((64 * gridX), 0,(64 * gridX),tk.getScreenSize().height); 
      } 
     } 

     if (squareOn) 
     { 
      System.out.println("Square works"); 
      g.setColor(Color.red); 
      g.fillRect(0,0,64,64); 
     } 
} 



} 

public class DragPanel extends JPanel{ 

    OptionPanel op = new OptionPanel(); 


    public DragPanel(){ 
     add(op.squareButton); 
     op.squareButton.setActionCommand("square"); 
    } 

    public void addActionListener(ActionListener listener){ 
     op.squareButton.addActionListener(listener); 
    } 

} 

public class OptionPanel extends JPanel { 

    public JButton grid; 

    public JButton vgrid; 

    public JButton squareButton; 

    public JTextField squareX; 

    public JTextField squareY; 

    public JTextField squareW; 

    public JTextField squareH; 

    public Square square = new Square(); 


    public OptionPanel() { 

     //Sets the stuff for the panel 
     setBackground(new Color(155,0,255)); 
     setLayout(new GridBagLayout()); 
     //end 

     //The Show Grid Button Stuff 
     grid = new JButton("Show Horizontal Grid"); 
     grid.setActionCommand("grid"); 
     //end 

     //The vertical grid 
     vgrid = new JButton("Show Vertical Grid"); 
     vgrid.setActionCommand("vgrid"); 
     //end 

     //The Square tool button stuff 
     squareButton = new JButton("Sqaure Tool"); 

     //end 

     squareX = new JTextField(3); 
     squareY = new JTextField(3); 
     squareW = new JTextField(3); 
     squareH = new JTextField(3); 

     //The gridbagConstraints things 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.NORTH; 


     //kind of like padding 
     gbc.weighty = 1; 

     //sets the positions 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     //add it 
     add(grid, gbc); 

     //changes position for the second button 
     gbc.gridx = -1; 
     gbc.gridy = 0; 

     // adds it 
     add(vgrid,gbc); 

     //end 

     add(squareX,gbc); 
     add(squareY,gbc); 
     add(squareW,gbc); 
     add(squareH,gbc); 


    } 

    public void addActionListener(ActionListener listener) { 
     //adds action listeners 
     grid.addActionListener(listener); 
     vgrid.addActionListener(listener); 

    } 
} 
+1

不是我的反对票,但你添加的ActionListeners到组件的一个有点令人费解的方式。 – 2013-05-12 00:46:25

+0

在'main()'中设置'frame.setVisible(true)'_last_。 – trashgod 2013-05-12 00:47:07

+0

有没有更好的方法,你会建议? @ Hovercraft充满鳗鱼 – 2013-05-12 00:47:39

回答

2

DragPanel不同OptionPanelGame。我添加了真实的参数,并使用this来限定参考,更多的是为了说明问题而不是建议正确的用法。正如HFOE所建议的,使用Action将您的听众重新设置为尽可能地在本地。

image

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenuBar; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Game extends JPanel implements ActionListener { 

    private static final long serialVersionUID = 1L; 
    private GridPane gridPane; 
    private DragPanel drag; 
    public boolean isMouseClicked = false; 
    public static JMenuBar bar = new JMenuBar(); 
    public int gridY = 1; 
    public int gridX = 1; 
    public int x = 0, y = 0; 

    public Game() { 
     setLayout(new BorderLayout()); 

     OptionPanel options = new OptionPanel(); 
     options.addActionListener(this); 
     add(options, BorderLayout.NORTH); 

     gridPane = new GridPane(); 
     gridPane.setBorder(BorderFactory.createLineBorder(Color.white)); 
     add(gridPane); 

     drag = new DragPanel(options); 
     drag.setBorder(BorderFactory.createLineBorder(Color.white)); 
     drag.setBackground(new Color(100, 100, 125)); 
     add(drag, BorderLayout.WEST); 
    } 

    public static void main(String args[]) { 
     Game game = new Game(); 
     JFrame frame = new JFrame(); 
     frame.setTitle("Game"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(game); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println(e); 
     if (e.getActionCommand().equalsIgnoreCase("grid")) { 
      gridPane.setGridOn(!gridPane.isGridOn()); 
     } 

     if (e.getActionCommand().equalsIgnoreCase("square")) { 
      gridPane.setSqaureOn(!gridPane.isSquareOn()); 
     } 
     if (e.getActionCommand().equalsIgnoreCase("vgrid")) { 
      gridPane.setVertOn(!gridPane.isVertOn()); 
     } 
    } 

    public class GridPane extends JPanel { 

     private static final long serialVersionUID = 1L; 
     private boolean gridOn = false; 
     private boolean squareOn = false; 
     private boolean vertOn = false; 

     public GridPane() { 
      setBackground(Color.BLACK); 
     } 

     public boolean isGridOn() { 
      return gridOn; 
     } 

     public boolean isSquareOn() { 
      return squareOn; 
     } 

     public boolean isVertOn() { 
      return vertOn; 
     } 

     public void setGridOn(boolean value) { 
      if (value != gridOn) { 
       this.gridOn = value; 
       repaint(); 
      } 
     } 

     public void setVertOn(boolean value) { 
      if (value != vertOn) { 
       this.vertOn = value; 
       repaint(); 
      } 
     } 

     public void setSqaureOn(boolean value) { 
      if (value != squareOn) { 
       this.squareOn = value; 
       repaint(); 
      } 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Toolkit tk = Toolkit.getDefaultToolkit(); 
      if (gridOn) { 
       System.out.println("Grid works"); 
       g.setColor(Color.white); 
       for (int i = 0; i < tk.getScreenSize().height; i += 64) { 
        gridY++; 
        g.drawLine(0, (64 * gridY), tk.getScreenSize().width, (64 * gridY)); 
       } 
      } 
      gridY = -1; 
      gridX = -1; 
      if (vertOn) { 
       System.out.println("vert grid works"); 
       g.setColor(Color.white); 
       for (int ig = 0; ig < tk.getScreenSize().width; ig += 64) { 
        gridX++; 
        g.drawLine((64 * gridX), 0, (64 * gridX), tk.getScreenSize().height); 
       } 
      } 
      if (squareOn) { 
       System.out.println("Square works"); 
       g.setColor(Color.red); 
       g.fillRect(0, 0, 64, 64); 
      } 
     } 
    } 

    public class DragPanel extends JPanel { 

     OptionPanel op; 

     public DragPanel(OptionPanel op) { 
      this.op = op; 
      this.add(this.op.squareButton); 
      this.op.squareButton.setActionCommand("square"); 
     } 

     public void addActionListener(ActionListener listener) { 
      System.out.println(listener); 
      this.op.squareButton.addActionListener(listener); 
     } 
    } 

    private static class Square { 
    } 

    private class OptionPanel extends JPanel { 

     public JButton grid; 
     public JButton vgrid; 
     public JButton squareButton; 
     public JTextField squareX; 
     public JTextField squareY; 
     public JTextField squareW; 
     public JTextField squareH; 
     public Square square = new Square(); 

     public OptionPanel() { 

      //Sets the stuff for the panel 
      setBackground(new Color(155, 0, 255)); 
      setLayout(new GridBagLayout()); 
      //end 

      //The Show Grid Button Stuff 
      grid = new JButton("Show Horizontal Grid"); 
      grid.setActionCommand("grid"); 
      //end 

      //The vertical grid 
      vgrid = new JButton("Show Vertical Grid"); 
      vgrid.setActionCommand("vgrid"); 
      //end 

      //The Square tool button stuff 
      squareButton = new JButton("Sqaure Tool"); 

      //end 

      squareX = new JTextField(3); 
      squareY = new JTextField(3); 
      squareW = new JTextField(3); 
      squareH = new JTextField(3); 

      //The gridbagConstraints things 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.anchor = GridBagConstraints.NORTH; 


      //kind of like padding 
      gbc.weighty = 1; 

      //sets the positions 
      gbc.gridx = 0; 
      gbc.gridy = 0; 

      //add it 
      add(grid, gbc); 

      //changes position for the second button 
      gbc.gridx = -1; 
      gbc.gridy = 0; 

      // adds it 
      add(vgrid, gbc); 

      //end 

      add(squareX, gbc); 
      add(squareY, gbc); 
      add(squareW, gbc); 
      add(squareH, gbc); 
     } 

     public void addActionListener(ActionListener listener) { 
      //adds action listeners 
      grid.addActionListener(listener); 
      vgrid.addActionListener(listener); 
      squareButton.addActionListener(listener); 
      System.out.println(listener); 
     } 
    } 
} 
+0

哦,谢谢你!有时会让编程变得如此困难。 @trashgod – 2013-05-12 01:17:48

+0

也考虑一个JToggleButton来控制开/关。 – trashgod 2013-05-12 04:19:26