2010-06-16 85 views
0
public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener 
{ 
    int packageIndex; 
    double price; 
    double[] prices = {49.99, 39.99, 34.99, 99.99}; 

    DecimalFormat money = new DecimalFormat("$0.00"); 
    JLabel priceLabel = new JLabel("Total Price: "+price); 
    JButton button = new JButton("Check Price"); 
    JComboBox packageChoice = new JComboBox(); 
    JPanel pane = new JPanel(); 
    TextField text = new TextField(5); 
    JButton accept = new JButton("Accept"); 
    JButton decline = new JButton("Decline"); 
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false); 
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10); 

    public JFrameWithPanel() 
    { 
     super("JFrame with Panel"); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pane.add(packageChoice); 
     setContentPane(pane); 
     setSize(250,250); 
     setVisible(true); 

     packageChoice.addItem("A+ Certification"); 
     packageChoice.addItem("Network+ Certification "); 
     packageChoice.addItem("Security+ Certifictation"); 
     packageChoice.addItem("CIT Full Test Package"); 

     pane.add(button); 
     button.addActionListener(this); 

     pane.add(text); 
     text.setEditable(false); 
     text.setBackground(Color.WHITE); 
     text.addActionListener(this); 

     pane.add(termsOfService); 
     termsOfService.setEditable(false); 
     termsOfService.setBackground(Color.lightGray); 

     pane.add(serviceTerms); 
     serviceTerms.addItemListener(this); 

     pane.add(accept); 
     accept.addActionListener(this); 

     pane.add(decline); 
     decline.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     packageIndex = packageChoice.getSelectedIndex(); 
     price = prices[packageIndex]; 
     text.setText("$"+price); 

     Object source = e.getSource(); 

     if(source == accept) 
     { 
      if(serviceTerms.isSelected() == false) 
      { 
       JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product."); 
       pane.dispose(); 
      } 
     } 
     else if(source == decline) 
     { 
      System.exit(0); 
     } 
    } 

    public void itemStateChanged(ItemEvent e) 
    { 
     int select = e.getStateChange(); 
    } 

    public static void main(String[] args) 
    { 
     String value1; 
     int constant = 1, invalidNum = 0, answerParse, packNum, packPrice; 

     JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program."); 

     JOptionPane.showMessageDialog(null,"IT WORKS!"); 
    } 



}//class 

如何獲取此框架以關閉,以便我的JOptionPane消息對話框可以在程序中繼續,而無需完全退出程序。如何在程序中間關閉JFrame

編輯:我試過.dispose(),但我得到這個:

cannot find symbol 
symbol : method dispose() 
location: class javax.swing.JPanel 
       pane.dispose(); 
        ^

回答

0

嘗試:this.dispose()代替。

JPanel沒有這種方法,但JFrame確實

編輯

在你的主你不叫你的框架:

public static void main(String[] args) { 
    String value1; 
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice; 

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program."); 

    JOptionPane.showMessageDialog(null,"IT WORKS!"); 
    } 

嘗試增加它,看到了差距:

public static void main(String[] args) { 
    String value1; 
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice; 

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program."); 

    JOptionPane.showMessageDialog(null,"IT WORKS!"); 
    new JFrameWithPanel(); //<-- creating a JFrameWithPanel 
} 

同樣在執行操作的方法中,您正在顯示對話框然後進行處理,可能您想以相反的方式進行處理。

if(serviceTerms.isSelected() == false) { 
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE); 
} else { 
    this.dispose(); 
    JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product."); 
} 

導致:隨後

result http://img85.imageshack.us/img85/8513/capturadepantalla201006l.png

編輯2

試試下面的代碼

main http://img194.imageshack.us/img194/7038/capturadepantalla201006x.png

,應該翔w一個框架,當你點擊「關閉」按鈕時,它應該顯示一個對話框,那是你在找什麼?

import javax.swing.*; 
import java.awt.event.*; 

class FrameDemo { 
    public static void main(String [] args) { 
     final JFrame frame = new JFrame("Main frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new JPanel(){{ 
      add(new JLabel("This is the main content")); 
      add(new JButton("Close"){{ 
       addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e) { 
         frame.dispose(); 
         JOptionPane.showMessageDialog(frame,"IT WORKS!"); 

        } 
       }); 
      }}); 
     }}); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 
} 
+0

這是完全不同的東西。在你的main方法中,你甚至不會調用你的'JFrameWithPanel'在'main'方法的最後一行嘗試添加:'new JFrameWithPanel();'並且看到區別 – OscarRyz 2010-06-16 17:52:46

+0

是的,這正是我在做的事情。點擊接受顯示「謝謝你」對話框。 – OscarRyz 2010-06-16 17:59:06

+0

前兩個?歡迎來到CIT和IT作品?可能你只是刪除它們。 – OscarRyz 2010-06-16 18:02:01

-1

我認爲這可能是你的問題:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

這將導致應用程序時關閉框退出。

你可以嘗試:

setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 

,並更換dispose()電話:

setVisible(false) 
0

我知道這可能是一個愚蠢的答案,但有時最明顯的一點的是問題的根源。我沒有看到你在代碼中導入javax.swing ...你有這樣做嗎?

+0

這可能應該是一個評論 – Linger 2012-11-04 16:55:00