2017-09-23 76 views
0

我想弄清楚如何讓我用字符串生成器所做的對象顯示在JOptionPane窗口中。現在它不能識別我用字符串生成器創建的2個字符串。我錯過了什麼。我需要做什麼才能在JOptionPane窗口中顯示2個字符串sb和ssb。我很難過。我怎樣才能讓JOptionPane使用我製作的StringBilder。

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

public class CarSelector extends JFrame implements ActionListener, ItemListener{ 

JButton submit = new JButton("Submit"); 
JLabel label1 = new JLabel("Select Vehicle type and options"); 
JLabel carLabel = new JLabel("Vehicle Type"); 
JLabel options = new JLabel("Options"); 
ButtonGroup group = new ButtonGroup(); 
JRadioButton carRadio = new JRadioButton("Car", true); 
JRadioButton vanRadio = new JRadioButton("Minivan"); 
JRadioButton truckRadio = new JRadioButton("Pickup Truck"); 
JRadioButton suvRadio = new JRadioButton("SUV"); 

JCheckBox leather = new JCheckBox("Leather Seats"); 
JCheckBox ac = new JCheckBox("Air Conditioning"); 
JCheckBox sat = new JCheckBox("Sattelite Radio"); 
JCheckBox warmer = new JCheckBox("Seat Warmers"); 
String optionsSelected; 
String carSelected; 


ActionListener listen = new ActionListener(){ 

    @Override 
    public void actionPerformed(ActionEvent ae){ 

      JOptionPane.showMessageDialog(
      CarSelector.this, sb + ssb); 
     } 

}; 

CarSelector(){ 
    super("Vehicle Selector"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(300, 300); 

    CarGUI(); 
} 

public void CarGUI(){ 

    JPanel vehicleTypes = new JPanel(); 
    JPanel carOptions = new JPanel(); 
    JPanel submitButton = new JPanel(); 

    submit.addActionListener(listen); 

    add(submitButton); 
    submitButton.setLayout(new BoxLayout(submitButton, BoxLayout.X_AXIS)); 
    submitButton.setBounds(100, 150, 100, 100); 

    add(vehicleTypes); 
    vehicleTypes.setLayout(new BoxLayout(vehicleTypes, BoxLayout.Y_AXIS)); 
    vehicleTypes.setBounds(150,0,125,125); 

    add(carOptions); 
    carOptions.setLayout(new BoxLayout(carOptions, BoxLayout.Y_AXIS)); 

    vehicleTypes.add(carLabel); 
    vehicleTypes.add(carRadio); 
    vehicleTypes.add(vanRadio); 
    vehicleTypes.add(truckRadio); 
    vehicleTypes.add(suvRadio); 
    group.add(carRadio); 
    group.add(vanRadio); 
    group.add(truckRadio); 
    group.add(suvRadio);   


    carOptions.add(options); 
    carOptions.add(leather); 
    carOptions.add(ac); 
    carOptions.add(sat); 
    carOptions.add(warmer); 

    submitButton.add(submit); 
    setVisible(true); 

} 

public void radioAction(){ 

    StringBuilder sb = new StringBuilder(); 
    sb.append("You have chosen a "); 

    if (carRadio.isSelected()){ 
    sb.append(" Car "); 
    } 
    else if(vanRadio.isSelected()){ 
    sb.append(" Minivan "); 
    } 
    else if(truckRadio.isSelected()){ 
    sb.append(" Pickup Truck "); 
    } 
    else if(suvRadio.isSelected()){ 
    sb.append(" SUV "); 
    } 
} 
public void checkAction(){ 
    StringBuilder ssb = new StringBuilder(); 
    ssb.append(" with these options: "); 
    if (leather.isSelected()){ 
     ssb.append(" Leather Seats, "); 
    } 
    if (ac.isSelected()){ 
     ssb.append(" Air Conditioning, "); 
    } 
    if (sat.isSelected()){ 
     ssb.append(" Sattelite Radio, "); 
    } 
    if (warmer.isSelected()){ 
     ssb.append(" Seat Warmers, "); 
    } 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 
@Override 
public void itemStateChanged(ItemEvent e) { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 

}

+1

有一個[本地和成員變量之間的差異] (http://www.cafeaulait.org/course/week3/11.html)。 'sb'和'ssb'都是局部變量。 – user3151902

+0

我有一種感覺是這個問題,但我無法弄清楚。感謝您的幫助。 –

回答

0

sbssb是局部變量,分別在radioAction()checkAction()方法來定義。即使你調用了這些方法(你不這樣做),你也不能使用這些變量,因爲它們不在範圍內。

你需要將這些方法返回一個String

public String radioAction(){ 
    StringBuilder sb = new StringBuilder(); 

    // rest of code. 

    return sb.toString(); 
} 

(同樣爲checkAction()),然後調用方法:

JOptionPane.showMessageDialog(
     CarSelector.this, radioAction() + checkAction());