2011-09-28 50 views
1

所以我有一個具有多個JButtons的繪畫程序,我知道如何將該按鈕與該按鈕的動作關聯起來的唯一方法是執行類似if(e.ActionCommand()。equals (「按鈕的標籤在這裏」);如果標籤匹配我知道我有正確的動作,但這是我的問題,我試圖改變我的JButtons是圖像,所以而不是有JButton first = new JButton 「Hello」);我試圖讓JButton first = new JButton(new ImageIcon(「c:/ .....」))然後得到這個圖像,這個工作正常,但是我的問題是,在這之後我不知道如何參考按鈕,使其實際上做些什麼。謝謝!!!JButtons和ActionListeners的問題

import javax.imageio.ImageIO; 
    import javax.swing.*; 
    import javax.swing.border.Border; 
    import javax.swing.border.LineBorder; 

    import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.image.BufferedImage; 
    import java.io.File; 
    import java.io.IOException; 
    import java.util.*; 


    public class PaintProgram extends JPanel implements MouseListener,ActionListener 
    { 
public static int stroke, eraser = 0; 
private int xX1, yY1 , xX2, yY2, choice ; 

public static void main(String [] args) 
{ 
    new PaintProgram(); 
} 

PaintProgram() 
{ 


    JFrame frame = new JFrame("Paint Program"); 
    frame.setSize(1200, 800); 
    frame.setBackground(Color.WHITE); 
    frame.getContentPane().add(this); 


    JMenuBar menuBar = new JMenuBar(); 
    frame.setJMenuBar(menuBar); 
    JMenu help = new JMenu("Help"); 
    menuBar.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 
    about.addActionListener(this); 



    JButton button1 = new JButton("Clear"); 
    button1.addActionListener(this); 
    JButton color = new JButton("Color"); 
    color.addActionListener(this); 
    JButton erase = new JButton("Erase?"); 
    erase.addActionListener(this); 
    JButton button2 = new JButton("Empty Rect"); 
    button2.addActionListener(this); 
    JButton button3 = new JButton("Filled oval"); 
    button3.addActionListener(this); 
    JButton button4 = new JButton("Filled Rect"); 
    button4.addActionListener(this); 
    JButton button5 = new JButton("Empty oval"); 
    button5.addActionListener(this); 
    JButton button6 = new JButton("Line"); 
    button6.addActionListener(this); 
    JRadioButton thin = new JRadioButton("Thin Line"); 
    thin.addActionListener(this); 
    JRadioButton medium = new JRadioButton("Medium Line"); 
    medium.addActionListener(this); 
    JRadioButton thick = new JRadioButton("Thick Line"); 
    thick.addActionListener(this); 


    ButtonGroup lineOption = new ButtonGroup(); 
    lineOption.add(thin); 
    lineOption.add(medium); 
    lineOption.add(thick); 

    this.add(button1); 
    this.add(color); 
    this.add(erase); 
    this.add(button2); 
    this.add(button3); 
    this.add(button4); 
    this.add(button5); 
    this.add(button6); 
    this.add(thin); 
    this.add(medium); 
    this.add(thick); 
    addMouseListener(this); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



} 

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D)g; 
    if(grid == null){ 
     int w = this.getWidth(); 
     int h = this.getHeight(); 
     grid = (BufferedImage)(this.createImage(w,h)); 
     gc = grid.createGraphics(); 
     gc.setColor(Color.BLUE); 
    } 

    g2.drawImage(grid, null, 0, 0); 
    check(); 
} 
BufferedImage grid; 
Graphics2D gc; 


public void draw() 
{ 
    Graphics2D g = (Graphics2D)getGraphics(); 
    int w = xX2 - xX1; 
     if (w<0) 
     w = w *(-1); 

    int h = yY2-yY1; 
     if (h<0) 
     h= h*(-1); 

    switch(choice) 
    { 
     case 1: 
      check(); 
      gc.drawRect(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 2: 
      check(); 
      gc.drawOval(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 3: 
      check(); 
      gc.drawRect(xX1, yY1, w, h); 
      gc.fillRect(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 4: 
      check(); 
      gc.drawOval(xX1, yY1, w, h); 
      gc.fillOval(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 5: 

      if (stroke == 0) 
      gc.setStroke(new BasicStroke (1)); 
      if (stroke == 1) 
      gc.setStroke(new BasicStroke (3)); 
      if (stroke == 2) 
      gc.setStroke(new BasicStroke (6)); 
      gc.drawLine(xX1, yY1, xX2, yY2); 
      repaint(); 
      break; 

     case 6: 
      repaint(); 
      Color temp = gc.getColor(); 
      gc.setColor(Color.WHITE); 
      gc.fillRect(0, 0, getWidth(), getHeight()); 
      gc.setColor(temp); 
      repaint(); 
      break; 

     case 7: 

      if (eraser == 1) 
      { 
       gc.clearRect(xX1, yY1, w, h); 
      } 
      else 
      { 

      } 
      break; 
    } 
} 

public void check() 
{ 
    if (xX1 > xX2) 
    { 
     int z = 0; 
     z = xX1; 
     xX1 = xX2; 
     xX2 =z; 
    } 
    if (yY1 > yY2) 
    { 
     int z = 0; 
     z = yY1; 
     yY1 = yY2; 
     yY2 = z; 
    } 
} 




public void actionPerformed(ActionEvent e) 
{ 

if (e.getActionCommand().equals("Color")) 
{ 
    Color bgColor 
    = JColorChooser.showDialog(this,"Choose Background Color", getBackground()); 
    if (bgColor != null) 
    gc.setColor(bgColor); 
} 


if (e.getActionCommand().equals("About")) 
{ 
    System.out.println("About Has Been Pressed"); 
    JFrame about = new JFrame("About"); 
    about.setSize(300, 300); 
    JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png")); 
    about.add(picture); 
    about.setVisible(true); 
} 

if (e.getActionCommand().equals("Empty Rect")) 
{   
    System.out.println("Empty Rectangle Has Been Selected~"); 
    choice = 1; 

    } 

if (e.getActionCommand().equals("Empty oval")) 
{   
System.out.println("Empty Oval Has Been Selected!"); 
    choice = 2; 
    } 


if (e.getActionCommand().equals("Filled Rect")) 
{   
    System.out.println("Filled Rectangle Has Been Selected"); 
    choice = 3; 
    } 


if (e.getActionCommand().equals("Filled oval")) 
{   
System.out.println("Filled Oval Has Been Selected"); 
    choice = 4; 
    } 


if (e.getActionCommand().equals("Line")) 
{ 
    System.out.println("Draw Line Has Been Selected"); 
    choice = 5; 
} 

if (e.getActionCommand().equals("Thin Line")) 
{ 
    stroke = 0; 
} 

if (e.getActionCommand().equals("Medium Line")) 
{ 
    stroke = 1; 
} 

if (e.getActionCommand().equals("Thick Line")) 
{ 
    stroke = 2; 
} 

if(e.getActionCommand().equals("Erase?")) 
{ 
    eraser = 1; 
    choice = 7; 
} 

if (e.getActionCommand().equals("Clear")) 
{   
    System.out.println("Clear All The Things!!!"); 
    choice = 6; 
    draw(); 
} 


} 

public void mouseExited(MouseEvent evt){} 
public void mouseEntered(MouseEvent evt){} 
public void mouseClicked(MouseEvent evt){} 
public void mousePressed(MouseEvent evt) 
{ 

    xX1 = evt.getX(); 
    yY1= evt.getY(); 



    } 
public void mouseReleased(MouseEvent evt) 
{ 
    xX2 =evt.getX(); 
    yY2=evt.getY(); 
    draw(); 
    eraser = 0; 
    } 
+0

沒有人想通過大量的代碼來解決問題 - 將它解決到您正在嘗試解決的具體問題 – kleopatra

回答

3

我會使用Actio nListener和事件註冊到您的按鈕是這樣的:

yourButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent event) 
    {// your logic here; 
    }}); 

除了這裏是一個很好的鏈接,瞭解事件偵聽器是如何工作的http://download.oracle.com/javase/tutorial/uiswing/events/index.html您要處理事件的方式,使你的代碼難以閱讀,難以保持。

2

您有幾種可能性:

  • 保持監聽器代碼IS,並在每個按鈕
  • 使用e.getSource()如果來源是Button1的,或BUTTON2測試呼叫setActionCommand
  • 使用每個按鈕有一個獨立的監聽器,如Bartzilla所示

這最後一個方法通常是最好的,因爲每個按鈕都應該做某些事情與其他的不同。對所有的按鈕有一個巨大的方法,有很多if語句,並不是很乾淨。

此外,命名您的按鈕fillOvalButtondrawLineButton等會使代碼比按鈕1,按鈕2等更清晰

+0

命名轉換,最完整的答案+1 – mKorbel

0

如果你想對JButton的一個ImageIcon和方法來識別它:

JButton b = new JButton(ImageIcon i, String name); 

然後用

JButton c = (JButton) e.getSource(); 
if (c.getText()).equals(name); 

這就是僞代碼,但希望它有助於