Когда я запускаю следующий код, я получаю странный результат для String result = "Salutations, "+firstName_response+" "+lastName_response+", you are the chosen Hero of planet"+planet_response+".\n";
. Вывод программы просто дает мне кучу букв и цифр для этой части. Может кто-нибудь объяснить мне, почему он дает мне этот странный результат и как решить эту проблему?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class QuizWindowLayout extends JFrame implements ActionListener
{
private ButtonGroup group;
private JComboBox choice_field;
private JComboBox disguiseList;
private JComboBox powerList;
private JTextField firstName_response;
private JTextField lastName_response;
private JTextField planet_response;
private JCheckBox box_1;
private JCheckBox box_2;
private JCheckBox box_3;
private JCheckBox box_4;
private JCheckBox box_5;
private JRadioButton radBut1;
private JRadioButton radBut2;
private JRadioButton radBut3;
private JRadioButton radBut4;
private JRadioButton radBut5;
protected JPanel northPanel;
protected JLabel label;
private String title = "What kind of animal are you?";
private String response_dialog;
Font largeFont = new Font("TimesRoman", Font.BOLD, 30);
Font medFont = new Font("TimesRoman", Font.BOLD, 15);
Font smallFont = new Font("TimesRoman", Font.BOLD, 17);
public QuizWindowLayout()
{
int winWidth = 600;
int winHeight = 600;
this.setTitle("What kind of hero or villian are you are you?");
this.setSize(winWidth, winHeight);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initWin();
this.setVisible(true);
}
public void initWin()
{
JPanel north_pan = new JPanel(new GridLayout(2,0));
JLabel title = new JLabel("What kind of hero or villian are you?");
title.setForeground(Color.blue);
north_pan.setBackground(Color.MAGENTA);
title.setFont(largeFont);
title.setHorizontalAlignment(JLabel.CENTER);
north_pan.add(title);
JPanel bottom_label= new JPanel(new GridLayout(6,2));
bottom_label.setPreferredSize(new Dimension(190,140));
bottom_label.setBackground(Color.BLUE);
JLabel choice_lab = new JLabel("Enter Hero for hero or Villian for villain:");
choice_lab.setForeground(Color.magenta);
choice_lab.setFont(medFont);
choice_lab.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(choice_lab);
String hero_villian_selection[] ={"Select a side", "Hero", "villain"};
choice_field = new JComboBox(hero_villian_selection);
bottom_label.add(choice_field);
JLabel firstName = new JLabel("What is your First Name");
firstName.setForeground(Color.magenta);
firstName.setFont(medFont);
firstName.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(firstName);
firstName_response = new JTextField("");
bottom_label.add(firstName_response);
JLabel lastName = new JLabel("What is your Last Name");
lastName.setForeground(Color.magenta);
lastName.setFont(medFont);
lastName.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(lastName);
lastName_response = new JTextField("");
bottom_label.add(lastName_response);
JLabel planet_label = new JLabel("Enter a name for your planet?");
planet_label.setForeground(Color.magenta);
planet_label.setFont(medFont);
planet_label.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(planet_label);
planet_response = new JTextField("");
bottom_label.add(planet_response);
JLabel powers_label = new JLabel("Select your superpower:");
powers_label.setForeground(Color.magenta);
powers_label.setFont(medFont);
powers_label.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(powers_label);
String power_answers[]= {"Select a power", "Heat Vision", "Mind Control", "Super Strength",
"Invisibility", "Telekinesis"};
powerList = new JComboBox(power_answers);
bottom_label.add(powerList);
JLabel disgise = new JLabel("What is your disguise?");
disgise.setForeground(Color.magenta);
disgise.setFont(medFont);
disgise.setHorizontalAlignment(JLabel.RIGHT);
bottom_label.add(disgise);
String disguise_answers[]= {"Select a disguise", "A Reporter", "A Psychiatrist", "A Professor",
"A CEO", "A Scientist"};
disguiseList = new JComboBox(disguise_answers);
bottom_label.add(disguiseList);
north_pan.add(bottom_label);
this.add(north_pan, BorderLayout.NORTH);
JLabel west_lab = new JLabel("Select your preferred weapon");
west_lab.setFont(smallFont);
west_lab.setForeground(Color.lightGray);
radBut1 = new JRadioButton("A Lasso");
radBut1.setFont(smallFont);
radBut1.setForeground(Color.red);
radBut1.setBackground(Color.GREEN);
radBut2 = new JRadioButton("A Double-Edged Sword");
radBut2.setFont(smallFont);
radBut2.setForeground(Color.red);
radBut2.setBackground(Color.GREEN);
radBut3 = new JRadioButton("A Gun");
radBut3.setFont(smallFont);
radBut3.setForeground(Color.red);
radBut3.setBackground(Color.GREEN);
radBut4 = new JRadioButton("A Quiver/ Bow");
radBut4.setFont(smallFont);
radBut4.setForeground(Color.red);
radBut4.setBackground(Color.GREEN);
radBut5 = new JRadioButton("A Katana");
radBut5.setFont(smallFont);
radBut5.setForeground(Color.red);
radBut5.setBackground(Color.GREEN);
JPanel west_pan = new JPanel(new GridLayout(6,1));
west_pan.setBackground(Color.GREEN);
west_pan.add(west_lab);
west_pan.add(radBut1);
west_pan.add(radBut2);
west_pan.add(radBut3);
west_pan.add(radBut4);
west_pan.add(radBut5);
this.add(west_pan, BorderLayout.WEST);
group = new ButtonGroup();
group.add(radBut1);
group.add(radBut2);
group.add(radBut3);
group.add(radBut4);
group.add(radBut5);
JLabel center_lab = new JLabel("Check all that apply");
center_lab.setFont(smallFont);
center_lab.setForeground(Color.YELLOW);
box_1 = new JCheckBox("No mercy to those who hurt your friends.");
box_1.setFont(smallFont);
box_1.setForeground(Color.WHITE);
box_1.setBackground(Color.RED);
box_2 = new JCheckBox("Merciless, but refusses to kill an innocent.");
box_2.setFont(smallFont);
box_2.setForeground(Color.WHITE);
box_2.setBackground(Color.RED);
box_3 = new JCheckBox("A strong moral code to not kill anyone.");
box_3.setFont(smallFont);
box_3.setForeground(Color.WHITE);
box_3.setBackground(Color.RED);
box_4 = new JCheckBox("Intelligent and courageous.");
box_4.setFont(smallFont);
box_4.setForeground(Color.WHITE);
box_4.setBackground(Color.RED);
box_5 = new JCheckBox("A high sense of responsibility.");
box_5.setFont(smallFont);
box_5.setForeground(Color.WHITE);
box_5.setBackground(Color.RED);
JPanel center_pan = new JPanel(new GridLayout(6,1));
center_pan.setBackground(Color.RED);
center_pan.add(center_lab);
center_pan.add(box_1);
center_pan.add(box_2);
center_pan.add(box_3);
center_pan.add(box_4);
center_pan.add(box_5);
this.add(center_pan, BorderLayout.CENTER);
JPanel south_pan = new JPanel();
JButton clear_but = new JButton("clear");
JButton submit_but = new JButton("submit");
JButton exit_but = new JButton("exit");
south_pan.add(clear_but);
south_pan.add(submit_but);
south_pan.add(exit_but);
this.add(south_pan, BorderLayout.SOUTH);
clear_but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ac)
{
clearKeyPress();
}
});
exit_but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ac)
{
exitKeyPress();
}
});
submit_but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ac)
{
submitKeyPress();
}
});
center_pan.setBorder(BorderFactory.createEtchedBorder());
north_pan.setBorder(BorderFactory.createEtchedBorder());
south_pan.setBorder(BorderFactory.createEtchedBorder());
west_pan.setBorder(BorderFactory.createEtchedBorder());
}
public void clearKeyPress()
{
if(powerList.getSelectedIndex() == 0)
{
System.out.print("No power is selected or typed\n");
}
if(disguiseList.getSelectedIndex() == 0)
{
System.out.print("No disguise is selected\n");
}
firstName_response.setText("");
lastName_response.setText("");
planet_response.setText("");
choice_field.setSelectedIndex(0);
powerList.setSelectedIndex(0);
disguiseList.setSelectedIndex(0);
group.clearSelection();
box_1.setSelected(false);
box_2.setSelected(false);
box_3.setSelected(false);
box_4.setSelected(false);
box_5.setSelected(false);
}
public void exitKeyPress()
{
System.exit(0);
}
public void submitKeyPress()
{
if(firstName_response.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "You did not enter a first name", "No first name entered", 2);
}else if(lastName_response.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "You did not enter a last name", "No last name entered", 2);
}else if(planet_response.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "You did not enter a height in CM", "No height entered", 2);
}else if(powerList.getSelectedIndex() == 0)
{
JOptionPane.showMessageDialog(null, "You did not select a super power", "No super power selected", 2);
}else if(disguiseList.getSelectedIndex() == 0)
{
JOptionPane.showMessageDialog(null, "You did not select a disguise", "No disguise selected", 2);
}else if(choice_field.getSelectedIndex() == 0)
{
JOptionPane.showMessageDialog(null, "You did not select if you are a hero or villain", "No side selected", 2);
}else if(radioButton() == false)
{
JOptionPane.showMessageDialog(null, "You did not select a weapon", "No weapon selected", 2);
}else if(addCheckBoxes() == false)
{
JOptionPane.showMessageDialog(null, "You did not select your preferred characteristics", "No characteristics selected", 2);
}
resultWindow();
}
public boolean radioButton()
{
if(radBut1.isSelected())
{
return true;
}else if(radBut2.isSelected())
{
return true;
}else if(radBut3.isSelected())
{
return true;
}else if(radBut4.isSelected())
{
return true;
}else if(radBut5.isSelected())
{
return true;
}
return false;
}
public boolean addCheckBoxes()
{
if(box_1.isSelected())
{
return true;
}else if(box_2.isSelected())
{
return true;
}else if(box_3.isSelected())
{
return true;
}else if(box_3.isSelected())
{
return true;
}else if(box_4.isSelected())
{
return true;
}else if(box_5.isSelected())
{
return true;
}
return false;
}
public void resultWindow()
{
if (choice_field.getSelectedIndex() == 1)
{
String result = "Salutations, "+firstName_response+" "+lastName_response+
", you are the chosen Hero of planet"+planet_response+".\n";
if(powerList.getSelectedIndex() == 1)
{
result += "When your not busy saving the planet, you are a reporter "
+ "at the daily plant. You try to keep a low profile, but it is"
+ "difficult when the person you are reporting on is yourself as a hero.";
}else if(powerList.getSelectedIndex() == 2)
{
result += "When you are not busy saving the world you are a "
+ "Psychiatrist. You help people as a pshychiatrist and as a hero.";
}else if(powerList.getSelectedIndex() == 3)
{
result += "When your not busy saving the world you are a profressor at "
+ "the most prestigious college in"+planet_response+". You help students"
+ "with out them catching on to who you really are.";
}
if (radBut1.isSelected())
{
result += " Your weapon is the Lasso of Truth."
+ " With your Lasso you are able to make any villain"
+ " tell you what you want to know.";
}else if(radBut2.isSelected())
{
result +=" Your weapon is a Double_Edge Sword. With your double-edged "
+ "sword you defend those who can't defend themselves";
}else if(radBut3.isSelected())
{
result += " Your weapon is the Gun of Justice. You have named your gun Peacemaker."
+ " You bring justice to those who would do other harm by using PeaceMaker "
+ " to stop them in their tracks.";
}else if (radBut4.isSelected())
{
result = " Your weapon is a bow and arrows. With your bow and arrows"
+ " you help those in need. You are lightning fast wih your"
+ " bow and arrow that the villains don't have enough time to act"
+ " before they are captured by your hands of justice.";
}else if(radBut5.isSelected())
{
result = " Your weapon is a katana. With your Japanese Sword you cut down the vilains"
+ " who attack the innocent.";
}
if(box_1.isSelected())
{
result += "\n You always put your friends first. "
+ " You show no mercy to those who would harms your friends.";
}
if(box_2.isSelected())
{
result += "\nYou are merciless to villains and heros alike, "
+ "however, you refuse to kill an innocent no matter what"
+ "the circumstances are.";
}
if(box_3.isSelected())
{
result += "\nYou have a strong moral code to not kill anyone. "
+ "No matter how evil they are.";
}
if(box_4.isSelected())
{
result += "\nYou are a very intelligent and courageous hero."
+ "You laugh at the face of danger and you have a brain"
+ "that would put einstein to shame.";
}
if(box_5.isSelected())
{
result += "\nYou have a high sense of responsibility and you believe "
+ "that you alone are responisible for the earths safety."
+ "Eventhough, you are not the only hero in the world.";
}
JOptionPane.showMessageDialog(null, result,
"Which hero or villian are you?", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args)
{
new QuizWindowLayout();
}
}