简体   繁体   中英

How can I get JOptionPane to use the StringBilder I made.

I am trying to figure out how to get the object I made with the String Builder to display in the JOptionPane window. Right now it doesn't recognize the 2 strings I made with the string builder. Am I missing something. What do I need to do in order to have the 2 strings sb and ssb display in the JOptionPane window. I am stumped.

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.
}

}

sb and ssb are local variables, defined in the radioAction() and checkAction() methods respectively. Even if you called these methods (which you don't), you can't use those variables because they're not in scope.

You need to make those methods return a String :

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

    // rest of code.

    return sb.toString();
}

(similarly for checkAction() ), and then call the methods:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM