主要方法的新实例被忽略

时间:2015-12-16 08:15:35

标签: java swing

大家好,祝你好运。

我在使用这个基于swing的GUI程序时遇到了麻烦。但是,它应该至少编译和运行。不幸的是它没有,我怀疑它是因为主方法调用被忽略了。该错误似乎发生在构建JSliders面板的部分,但它并没有比这更具体。怎么了? >

package charactercreationquest;


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.TitledBorder;

public class CharacterCreationQuest extends JFrame {

    //contains: image, race, raceLabel, job, jobLabel, genderLabel male, and female.  
    private final JPanel picPanel;
    //contains: str, intel, dex, wis, ptsSpent, and ptsRemain.
    private final JPanel sliderPanel;        
    //contains: name, and nameLabel
    private final JPanel namePanel;
    //contains: save and load 
    private final JPanel savePanel;

    private JSlider str;
    private JSlider intel;
    private JSlider dex;
    private JSlider wis;

    private JTextField name;

    private JComboBox race;
    private JComboBox job;

    private JRadioButton male;
    private JRadioButton female;

    private JButton save;
    private JButton load;

    private JLabel image;

    private JLabel genderLabel;
    private JLabel nameLabel;
    private JLabel jobLabel;//Label for lifestyles
    private JLabel raceLabel;

    private JLabel strLabel;
    private JLabel intelLabel;
    private JLabel dexLabel;
    private JLabel wisLabel;
    private JLabel ptsRemain;
    private JLabel ptsSpent;

    //what the heck is OCA look it up dumbo
    public CharacterCreationQuest()
    {
        picPanel = new JPanel();
        sliderPanel = new JPanel();
        namePanel = new JPanel();
        savePanel = new JPanel();
        setTitle("Character Creation Quest");

        setSize(500,500);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        setLayout(new GridLayout(2,2));

        buildPic();
        buildSlider();
        buildName();
        buildSave();

        add(picPanel);
        add(namePanel);
        add(savePanel);
        add(sliderPanel);

        pack();
        setVisible(true);

    }//end c'tor   

    public final void buildPic()
    {
      //contains image, race, raceLabel, job, jobLabel, genderLabel male, female  
      String[] raceList = new String[]{"a","b","c"};
      String[] jobList = new String[]{"a","b","c"};

      image = new JLabel();
      race = new JComboBox();
      job = new JComboBox();
      male = new JRadioButton("Male");
      female = new JRadioButton("Female");


      picPanel.add(image);
      picPanel.add(race);
      picPanel.add(job);
      picPanel.add(male);
      picPanel.add(female);

    }//end buildPic

    public final void buildSlider()
    {
       //contains: str, intel, dex, wis, and ptsRemain.

      int numSpent = (str.getValue() + intel.getValue() 
                        + dex.getValue() + wis.getValue());
      int numRemain = (100 - numSpent);
      String remain;
      String spent;

      remain = ("Points Remaining: " + numRemain);
      spent = ("Points Spent: " + numSpent);

      str = new JSlider();
      intel = new JSlider();
      dex = new JSlider();
      wis = new JSlider();
      ptsRemain = new JLabel(remain);
      ptsSpent = new JLabel(spent);
      strLabel = new JLabel("Strength");
      intelLabel = new JLabel("Intelligence");
      dexLabel = new JLabel("Dexterity");
      wisLabel = new JLabel("Wisdom");



      sliderPanel.setLayout(new GridLayout(5,2));        

      sliderPanel.add(ptsRemain);
      sliderPanel.add(ptsSpent);
      sliderPanel.add(strLabel);
      sliderPanel.add(str); 
      sliderPanel.add(intelLabel);
      sliderPanel.add(intel);
      sliderPanel.add(dexLabel);
      sliderPanel.add(dex);
      sliderPanel.add(wisLabel);
      sliderPanel.add(wis);




      pack();
    }//end buildSlider

    public final void buildName()
    {
      //contains: name and namelabel
      name = new JTextField(10);
      nameLabel = new JLabel();

      namePanel.add(name); 
    }//end buildName

    public final void buildSave()
    {
      //contains: save and load

      save = new JButton("Save");
      load = new JButton("Load");

      TitledBorder title;
      title = BorderFactory.createTitledBorder("SHIN");
      savePanel.setBorder(title);

      savePanel.add(save);
      savePanel.add(load);

    }//end buildSave











    //ignore this for now. get rid of the space above later plz.
    public static void main(String[] args) {

        new CharacterCreationQuest();


    }

}

1 个答案:

答案 0 :(得分:1)

您的问题是 str intel dex wis 尚未实例化。使用以下代码更改 buildSlider()方法:

public final void buildSlider()
{
    str = new JSlider();
    intel = new JSlider();
    dex = new JSlider();
    wis = new JSlider();

    //contains: str, intel, dex, wis, and ptsRemain.
    int numSpent = (str.getValue()+intel.getValue()+dex.getValue()+wis.getValue());
    int numRemain = (100-numSpent);
    String remain;
    String spent;

    remain = ("Points Remaining: "+numRemain);
    spent = ("Points Spent: "+numSpent);

    ptsRemain = new JLabel(remain);
    ptsSpent = new JLabel(spent);
    strLabel = new JLabel("Strength");
    intelLabel = new JLabel("Intelligence");
    dexLabel = new JLabel("Dexterity");
    wisLabel = new JLabel("Wisdom");

    sliderPanel.setLayout(new GridLayout(5, 2));

    sliderPanel.add(ptsRemain);
    sliderPanel.add(ptsSpent);
    sliderPanel.add(strLabel);
    sliderPanel.add(str);
    sliderPanel.add(intelLabel);
    sliderPanel.add(intel);
    sliderPanel.add(dexLabel);
    sliderPanel.add(dex);
    sliderPanel.add(wisLabel);
    sliderPanel.add(wis);

    pack();
}