2011-08-23 7 views
-1
import java.io.*; 
import javax.swing.*; 

import java.awt.event.*; 
import java.awt.*; 
import java.text.DecimalFormat; 

public class Final extends JFrame 
{ 
private JButton calcButton, exitButton; 
private JButton pcalcButton, pexitButton; 
private JTextField plength, pwidth, pdepth, pvolume; 
private JTextField hlength, hwidth, hdepth, hvolume; 
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel; 
private JRadioButton roundrButton, ovalrButton; 

public Final() 
{ 
super("Final"); 

JTabbedPane tab = new JTabbedPane(); 
// constructing the first panel 
JPanel p1 = new JPanel(new GridLayout(5,1)); 
pcalcButton = new JButton("Calculate Volume"); 
pexitButton = new JButton("Exit"); 
plength = new JTextField(5); 
pwidth = new JTextField(5); 
pdepth = new JTextField(5); 
pvolume = new JTextField(5); 
lengthLabel = new JLabel("Enter the pool's length (ft):"); 
widthLabel = new JLabel("Enter the pool's width (ft):"); 
depthLabel = new JLabel("Enter the pool's depth (ft):"); 
volumeLabel = new JLabel("The pool's volume (ft^3):"); 
p1.add(lengthLabel); 
p1.add(plength); 
p1.add(widthLabel); 
p1.add(pwidth); 
p1.add(depthLabel); 
p1.add(pdepth); 
p1.add(volumeLabel); 
p1.add(pvolume); 
p1.add(pcalcButton); 
p1.add(pexitButton); 
tab.addTab("Pools", null, p1, " Panel #1"); 

calcButtonHandler chandler =new calcButtonHandler(); 
pcalcButton.addActionListener(chandler); 
exitButtonHandler ehandler =new exitButtonHandler(); 
pexitButton.addActionListener(ehandler); 
FocusHandler fhandler =new FocusHandler(); 
plength.addFocusListener(fhandler); 
pwidth.addFocusListener(fhandler); 
pdepth.addFocusListener(fhandler); 
pvolume.addFocusListener(fhandler); 

// constructing the second panel 
JPanel p2 = new JPanel(new GridLayout(6,1)); 
ButtonGroup tubtype = new ButtonGroup(); 
roundrButton = new JRadioButton("Round", true); 
roundrButton.setActionCommand("round"); 
tubtype.add(roundrButton); 
ovalrButton = new JRadioButton("Oval", false); 
ovalrButton.setActionCommand("oval"); 
tubtype.add(ovalrButton); 
calcButton = new JButton("Calculate Volume"); 
exitButton = new JButton("Exit"); 
hlength = new JTextField(5); 
hwidth = new JTextField(5); 
hdepth = new JTextField(5); 
hvolume = new JTextField(5); 
lengthLabel = new JLabel("Enter the tub's length (ft):"); 
widthLabel = new JLabel("Enter the tub's width (ft):"); 
depthLabel = new JLabel("Enter the tub's depth (ft):"); 
volumeLabel = new JLabel("The tub's volume (ft^3):"); 
p2.add(roundrButton); 
p2.add(ovalrButton); 
p2.add(lengthLabel); 
p2.add(hlength); 
p2.add(widthLabel); 
p2.add(hwidth); 
p2.add(depthLabel); 
p2.add(hdepth); 
p2.add(volumeLabel); 
p2.add(hvolume); 
p2.add(calcButton); 
p2.add(exitButton); 
tab.addTab("Hot Tubs", null, p2, " Panel #1"); 

calcButtonHandler2 ihandler =new calcButtonHandler2(); 
calcButton.addActionListener(ihandler); 
exitButtonHandler ghandler =new exitButtonHandler(); 
exitButton.addActionListener(ghandler); 
FocusHandler hhandler =new FocusHandler(); 
hlength.addFocusListener(hhandler); 
hwidth.addFocusListener(hhandler); 
hdepth.addFocusListener(hhandler); 
hvolume.addFocusListener(hhandler); 

// add JTabbedPane to container 
getContentPane().add(tab); 
setSize(550, 500); 
setVisible(true); 
} 
public class calcButtonHandler implements ActionListener { 
public void actionPerformed(ActionEvent e) { 
    DecimalFormat num =new DecimalFormat(",###.##"); 
    double sLength, sWidth, sdepth, Total; 

    sLength = Double.parseDouble(plength.getText()); 

    sWidth = Double.parseDouble(pwidth.getText()); 

    sdepth = Double.parseDouble(pdepth.getText()); 

    if(e.getSource() == pcalcButton) { 
     Total = sLength * sWidth * sdepth; 
     pvolume.setText(num.format(Total)); 
     try{ 
      String value=pvolume.getText(); 
      File file = new File("output.txt"); 
      FileWriter fstream = new FileWriter(file,true); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value); 
      out.newLine(); 
      out.close(); 
     } 
     catch(Exception ex){} 
    } 
} 
} 

public class calcButtonHandler2 implements ActionListener { 
public void actionPerformed(ActionEvent g) { 
    DecimalFormat num =new DecimalFormat(",###.##"); 
    double cLength, cWidth, cdepth, Total; 

    cLength = Double.parseDouble(hlength.getText()); 

    cWidth = Double.parseDouble(hwidth.getText()); 

    cdepth = Double.parseDouble(hdepth.getText()); 

    try 
    { 

     if(roundrButton.isSelected())//**roundrButton cannot be resolved 
     { 
      Total = Math.PI * Math.pow(cLength/2.0, 2) * cdepth; 
     } 
     else 
    { 
      Total = Math.PI * Math.pow(cLength * cWidth, 2) * cdepth; 
    } 
     hvolume.setText(""+num.format(Total)); 
    } 
catch(Exception ex){} 
} 
} 
} 



public class exitButtonHandler implements ActionListener { //**The public type exitButtonHandler must be defined in its own file 
    public void actionPerformed(ActionEvent g){ 
     System.exit(0); 
    } 
} 
public class FocusHandler implements FocusListener { //**The public type FocusHandler must be defined in its own file 
    public void focusGained(FocusEvent e) { 
    } 
    public void focusLost(FocusEvent e) { 
    } 


public static void main(String args[]) 
{ 
    Final tabs = new Final(); 
    tabs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

} 

줄 옆에 //**으로 표시된 3 개의 오류가 발생합니다. 제가 가지고있는 문제를 찾아 내도록 도와주세요.초기화시 문제 발생

+0

마지막 두 개의 오류는 다소 자명하다. 각 클래스를 자신의 파일에 넣어야합니다. 그리고 [이전 질문] (http://stackoverflow.com/questions/7130300/java-initialization-question)에서 roundrButton 문제가 해결되지 않았습니까? – JJJ

답변

1

정의 된 위치에서 roundButton에 대한 참조를 허용하도록 calcButtonHandler2 정의를 변경하십시오. 마지막 두 오류 calcButtonHandler2

calcButtonHandler chandler =new calcButtonHandler(roundrButton); 

과 같은 인스턴스를 만들 때

public class calcButtonHandler2 implements ActionListener { 

    private final JRadioButton roundrButton; 

    public calcButtonHandler(JRadioButton roundrButton) 
    { 
     this.roundrButton= roundrButton; 
    } 
    .... 
} 

과 컴파일 오류가 발생한으로 별도의 파일에 클래스 선언을 이동하거나 제거, 참조 전달 그들의 정의에서 공개 키워드 (나는 첫 번째 방법을 권하고 싶습니다).

+0

정말 고마워요. – james

0

먼저 모든 클래스를 각각의 .java 파일에 작성하십시오.

JRadioButton roundrButtonFinal 클래스에 선언되어 있으므로 다른 클래스 calcButtonHandler2에서 직접 액세스 할 수 없습니다.

액세스하려면 클래스 Final의 객체를 사용해야하며 액세스하려면 내부 클래스를 사용할 수 있습니다.