2011-04-05 81 views
2

所以我有這個代碼在Java中創建了一個計算器。但它是用JApplet製作的,我需要它與JFrame一起。什麼需要改變?從JApplet更改爲JFrame

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

public class Calculator extends JApplet { 
    public void init() { 
     CalculatorPanel calc=new CalculatorPanel(); 
     getContentPane().add(calc); 
     } 
    } 

    class CalculatorPanel extends JPanel implements ActionListener { 
     JButton 
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal; 
     static JTextField result=new JTextField("0",45); 
     static String lastCommand=null; 
     JOptionPane p=new JOptionPane(); 
     double preRes=0,secVal=0,res; 

     private static void assign(String no) 
     { 
     if((result.getText()).equals("0")) 
      result.setText(no); 
      else if(lastCommand=="=") 
      { 
      result.setText(no); 
      lastCommand=null; 
      } 
      else 
      result.setText(result.getText()+no); 
     } 

     public CalculatorPanel() { 
     setLayout(new BorderLayout()); 
     result.setEditable(false); 
     result.setSize(300,200); 
     add(result,BorderLayout.NORTH); 
     JPanel panel=new JPanel(); 
     panel.setLayout(new GridLayout(4,4)); 

     n7=new JButton("7"); 
     panel.add(n7); 
     n7.addActionListener(this); 
     n8=new JButton("8"); 
     panel.add(n8); 
     n8.addActionListener(this); 
     n9=new JButton("9"); 
     panel.add(n9); 
     n9.addActionListener(this); 
     div=new JButton("/"); 
     panel.add(div); 
     div.addActionListener(this); 

     n4=new JButton("4"); 
     panel.add(n4); 
     n4.addActionListener(this); 
     n5=new JButton("5"); 
     panel.add(n5); 
     n5.addActionListener(this); 
     n6=new JButton("6"); 
     panel.add(n6); 
     n6.addActionListener(this); 
     mul=new JButton("*"); 
     panel.add(mul); 
     mul.addActionListener(this); 

     n1=new JButton("1"); 
     panel.add(n1); 
     n1.addActionListener(this); 
     n2=new JButton("2"); 
     panel.add(n2); 
     n2.addActionListener(this); 
     n3=new JButton("3"); 
     panel.add(n3); 
     n3.addActionListener(this); 
     minus=new JButton("-"); 
     panel.add(minus); 
     minus.addActionListener(this); 

     dot=new JButton("."); 
     panel.add(dot); 
     dot.addActionListener(this); 
     n0=new JButton("0"); 
     panel.add(n0); 
     n0.addActionListener(this); 
     equal=new JButton("="); 
     panel.add(equal); 
     equal.addActionListener(this); 
     plus=new JButton("+"); 
     panel.add(plus); 
     plus.addActionListener(this); 
     add(panel,BorderLayout.CENTER); 
     } 
     public void actionPerformed(ActionEvent ae) 
     { 
     if(ae.getSource()==n1) assign("1"); 
     else if(ae.getSource()==n2) assign("2"); 
     else if(ae.getSource()==n3) assign("3"); 
     else if(ae.getSource()==n4) assign("4"); 
     else if(ae.getSource()==n5) assign("5"); 
     else if(ae.getSource()==n6) assign("6"); 
     else if(ae.getSource()==n7) assign("7"); 
     else if(ae.getSource()==n8) assign("8"); 
     else if(ae.getSource()==n9) assign("9"); 
     else if(ae.getSource()==n0) assign("0"); 
     else if(ae.getSource()==dot) 
      { 
      if(((result.getText()).indexOf("."))==-1) 
       result.setText(result.getText()+"."); 
      } 
     else if(ae.getSource()==minus) 
      { 
      preRes=Double.parseDouble(result.getText()); 
      lastCommand="-"; 
      result.setText("0"); 
      } 
     else if(ae.getSource()==div) 
      { 
      preRes=Double.parseDouble(result.getText()); 
      lastCommand="/"; 
      result.setText("0"); 
      } 
     else if(ae.getSource()==equal) 
      { 
      secVal=Double.parseDouble(result.getText()); 
      if(lastCommand.equals("/")) 
        res=preRes/secVal; 
      else if(lastCommand.equals("*")) 
        res=preRes*secVal; 
      else if(lastCommand.equals("-")) 
        res=preRes-secVal; 
      else if(lastCommand.equals("+")) 
        res=preRes+secVal; 
      result.setText(" "+res); 
      lastCommand="="; 
      } 
     else if(ae.getSource()==mul) 
      { 
       preRes=Double.parseDouble(result.getText()); 
       lastCommand="*"; 
       result.setText("0"); 
       } 
     else if(ae.getSource()==plus) 
       { 
       preRes=Double.parseDouble(result.getText()); 
       lastCommand="+"; 
       result.setText("0"); 
       } 

     } 
} 
+0

哦,休息一下。 – 2011-04-05 19:32:02

回答

2

對於簡單的小程序,這應該這樣做

  • 延長JFrame的(顯然)
  • 將東西從init()移動到構造函數。

    public class Calculator extends JFrame{ 
    
        public Calculator() { 
        CalculatorPanel calc=new CalculatorPanel(); 
        getContentPane().add(calc); 
        addWindowListener(new WindowAdapter() { 
    
         public void windowClosing(WindowEvent e) { 
          System.exit(0); 
         } 
        }); 
        pack(); 
        setVisible(true); 
        } 
    

    }

+0

它的工作原理。感謝很多 – Stormel 2011-04-05 19:39:34

+0

雖然有必要擴展'JApplet',擴展'JFrame'既不必要也不需要,除非代碼提供超出標準'JFrame'的額外方法。 – 2011-04-05 20:31:22

1

沒有太多

public class Calculator extends JFrame{ //1 
    public void init() { 
     CalculatorPanel calc=new CalculatorPanel(); 
     getContentPane().add(calc); 
     this.show();  //2 
     } 
    } 

,當然主要功能...

+0

感謝您的回答 – Stormel 2011-04-05 19:43:12

0

一種方式是通過一個JApplet的添加到一個JFrame:

JFrame f=new JFrame(); 
    f.setSize(x, y); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setVisible(true); 

    Calculator calculator=new Calculator(); 
    calculator.setSize(x, y); 

    f.add(calculator); 

編輯

Here是它的一個教程。

+0

thanks.this教程可以非常有用。 – Stormel 2011-04-05 19:40:36

+0

不客氣。請記住將您認爲適合您的任何答案標記爲已接受的答案。 – Shankar 2011-04-05 19:42:15

1

可怕的代碼... ;-)

無論如何,與簡單的變化,它的工作原理:

public class Calculator 
{ 
    public static void main(String[] args) 
    { 
    // Schedule a job for the event-dispatching thread: 
    // creating and showing this application's GUI. 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
     JFrame calc = new JFrame(); 
     CalculatorPanel display = new CalculatorPanel(); 
     calc.add(display); 
     calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     calc.pack(); 
     calc.setVisible(true); 
     } 
    }); 
    } 

    static class CalculatorPanel extends JPanel implements ActionListener 
    { 
    // ... 
    } 
} 
2

似乎沒有人注意到你的SSCCE是非常適合成爲一個混合應用程序/小程序。這是因爲主GUI編碼爲JPanelCalculatorPanel),然後可以將其添加到JAppletJFrame

要創建混合,更改源的第一行:

// <applet code='Calculator' width='400' height='450'></applet> 
import javax.swing.*; 
import javax.swing.JOptionPane; 
import java.awt.*; 
import java.awt.event.*; 

public class Calculator extends JApplet { 
    public void init() { 
     CalculatorPanel calc=new CalculatorPanel(); 
     getContentPane().add(calc); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
     public void run() { 
      JFrame f = new JFrame("Calculator"); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      f.add(new CalculatorPanel()); 
      f.pack(); 
      f.setLocationByPlatform(true); 
      f.setVisible(true); 
     } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 

然後,編譯&來看,這既是一個applet &應用程序,像..

prompt> javac Calculator.java 
prompt> appletviewer Calculator.java // run the applet 
prompt> java Calculator    // run the application