2013-10-26 45 views
0

我不是一個很好的java程序員。當我使用這個編碼時,我被一些錯誤困住了。任何人都可以幫助我嗎?在java中使用swing時出現錯誤信息對話框

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*; 

import javax.swing.JOptionPane; 

public class test4 implements ActionListener 

{ 

    public static void main(String[] args)throws Exception 

    { 

     JFrame f=new JFrame("test 4"); 

     JButton b=new JButton("gud morning"); 

     JButton b1=new JButton("yes or no"); 

     b.addActionListener(this); 

     b1.addActionListener(this); 

     Container content=f.getContentPane(); 

     content.add(b,BorderLayout.NORTH); 

     content.add(b1,BorderLayout.SOUTH); 

     f.setVisible(true); 

     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     f.setSize(100,100); 

     } 
} 

public void actionPerformed(ActionEvent e) 

{ 

    String s=e.getActionCommand(); 

    if(s.equals("gud morning")) 

    { 

     JOptionPane.showMessageDialog(f,"gud morning","My Message"); 

    } 

    else if(s.equals("yes or no")) 

    { 


     if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0); 

     { 

      JOptionPane.showMessageDialog(f,"u clicked yes button"); 

     } 

     else 

     { 

      JOptionPane.showMessageDialog(f,"u clicked no button"); 

     } 

    } 

} 

我得到這個錯誤:

\ram>javac test4.java test4.java:22: error: class, interface, or enum expected public void actionPerformed(ActionEvent e)^test4.java:25: error: class, interface, or enum expected if(s.equals("gud morning"))^test4.java:28: error: class, interface, or enum expected }^3 errors 

回答

2

if聲明是由一個;這將使else分支不正確teminated。取下;

if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0); 

變爲:

if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0) 

您也有一個額外的大括號}只是你actionPerformed開始之前。

相關問題