2013-05-02 109 views
-1

我想在我的java菜單中將ActionListener添加到JMenuItem中。如何將ActionListener添加到JMenuItem?

這是菜單的截圖:

enter image description here

我想添加的ActionListener爲「矩形」的JMenuItem爲了在點擊「矩形」菜單項的矩形形狀出現。我多次嘗試添加ActionListener,但每次都失敗。

這裏是我的代碼:

級 「menubar.java」:

import javax.swing.*; 

public class menubar extends JFrame{ 

public menubar(){ 
    JMenuBar menubar = new JMenuBar(); 
    setJMenuBar(menubar); 

    JMenu shape = new JMenu("Shape"); 
    menubar.add(shape); 

    JMenuItem rect = new JMenuItem("Rectangle"); 
    shape.add(rect); 

    JMenuItem star = new JMenuItem("Star"); 
    shape.add(star); 

    JMenu color = new JMenu("Color"); 
    menubar.add(color); 

    JMenuItem black = new JMenuItem("Black"); 
    color.add(black); 

    JMenuItem orange = new JMenuItem("Orange"); 
    color.add(orange); 
} 

public static void main(String[] args) { 
    menubar gui = new menubar(); 
    gui.setTitle("Menu Bar"); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    shapes SPS = new shapes(); 
    gui.add(SPS); 
    gui.setSize(500,300); 
    gui.setVisible(true); 
    gui.setLocationRelativeTo(null); 
} 
} 

級 「shapes.java」:

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class shapes extends JPanel{ 
int midX = 220; 
int midY = 90; 
int radius[] = {60,20,50,20}; 
int nPoints = 16; 
int[] X = new int[nPoints]; 
int[] Y = new int[nPoints]; 

public void paintComponent(Graphics gphcs){ 
super.paintComponent(gphcs); 
this.setBackground(Color.WHITE); 

gphcs.setColor(Color.BLUE); 
gphcs.fillRect(20,35,100,30); 

gphcs.setColor(Color.RED); 
gphcs.drawString("Welcome to Java", 20, 20); 

for (int i=0; i < nPoints; i++) { 
     double x = Math.cos(i * ((2 * Math.PI)/nPoints)) * radius[i % 4]; 
     double y = Math.sin(i * ((2 * Math.PI)/nPoints)) * radius[i % 4]; 

     X[i] = (int) x + midX; 
     Y[i] = (int) y + midY; 
} 
gphcs.setColor(Color.GREEN); 
gphcs.fillPolygon(X, Y, nPoints); 
} 
} 

,我會很感激,如果有人幫我與這個問題。

感謝您的時間..

+1

我沒有看到你試圖調用'addActionListener(...)'到任何東西。你看過Swing菜單教程嗎?它在那裏都是拼寫出來的,所以我們真的沒有必要讓這裏的信息反覆出現,因爲它在那裏是爲了提問。谷歌會幫你找到它 - 或者我也可以:[如何使用菜單](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) – 2013-05-02 03:13:25

+2

認真?你看過你的上一個問題中鏈接的[如何使用菜單](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)?投票關閉 – MadProgrammer 2013-05-02 03:20:15

+1

@MadProgrammer:我知道,它也讓我感到吃驚,我不知道他已經獲得了鏈接!對於原來的海報,如果您之前已經看過教程並且讓您感到困惑,那麼請告訴我們您的問題到底是什麼讓您感到困惑,但是請告訴我們您至少在嘗試解決問題之前先解決問題,而不會顯示努力的證據。請告訴我們你並不是在懶惰,而是讓其他人爲你解決問題。 – 2013-05-02 03:21:47

回答

-1

一個你可以一個ActionListener添加到您的容器的方法是:

公共類JMenuClass擴展JFrame中實現的ActionListener

然後,對於您需要聽衆的每個JMenuItem,您可以執行

item.addActionListener(本)

在類的底部,你將需要添加您的actionPerformed方法,但是還是要實現它。

乾杯

+5

-1,不是一個好方法。首先,沒有充分的理由來擴展JFrame,因爲你沒有爲框架添加任何新的功能。另外,每個菜單項都應該有自己的ActionListener。 – camickr 2013-05-02 03:40:36

+0

是的,我正在使用上面的代碼並提示一種隨代碼一起使用的方法;我不是說每個JMenuItem都需要他們自己的偵聽器嗎? – sparkyShorts 2013-05-02 03:47:32

+0

如果您傳入'this',每個JMenuItem如何獲得唯一的偵聽器?作爲ActionListener? – 2013-05-02 03:54:09