如何让JOptionPane使用我制作的StringBilder。

时间:2017-09-23 18:49:57

标签: java stringbuilder

我试图找出如何使用String Builder创建的对象显示在JOptionPane窗口中。现在它无法识别我用字符串生成器创建的2个字符串。我错过了什么。为了在JOptionPane窗口中显示2个字符串sb和ssb,我需要做什么。我很难过。

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 个答案:

答案 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());