|
-
August 9th, 2009, 01:55 PM
#1
null null null and no components show up at launch
So whenever I click phrasemaker in my program it prints "null null null" in midgui. It should be returning a random String from nouns, adjectives, and verbs. I don't understand. It's supposed to print the contents of currentAra which apparently is null. Why is it null? It has the Strings because it adds the text of all the nounboxes, adjboxes, and verbboxes when you click phrasemaker. I also have a Phrase class I made the other day which could also be causing the problem.
here's the code:
Code:
//Phrasebot is property of shakenearth.com
//Code by Thomas Lisankie
//You may not use any of this code
//you may not distribute this program either
import java.awt.*;//imports java.awt
import java.awt.event.ActionEvent;//imports java.awt.event.ActionEvent
import java.awt.event.ActionListener;//imports java.awt.event.ActionListener
import java.util.*;//imports java.util
import javax.swing.*;//imports javax.swing
import javax.swing.event.ListSelectionEvent;//imports javax.swing.event.ListSelectionEvent
import javax.swing.event.ListSelectionListener;//imports javax.swing.event.ListSelectionListener
import java.io.*;//imports java.io
import java.net.*;//imports java.net
public class Phrasebot{
JTextArea midgui;//Makes a new instance variable of type JTextArea called midgui with a value of null
JButton phrasemaker;//Makes a new instance variable of type Jbutton called phrasemaker with a value of null
Box boxleft;//makes a new instance variable of type Box called boxleft with a value of null
JTextField nounbox1;//makes an instance variable of type JTextField called nounbox1 with a value of null
JTextField nounbox2;//makes an instance variable of type JTextField called nounbox2 with a value of null
JTextField nounbox3;//makes an instance variable of type JTextField called nounbox3 with a value of null
JTextField adjbox1;//makes an instance variable of type JTextField called adjbox1 with a value of null
JTextField adjbox2;//makes an instance variable of type JTextField called adjbox2 with a value of null
JTextField adjbox3;//makes an instance variable of type JTextField called adjbox3 with a value of null
JTextField verbbox1;//makes an instance variable of type JTextField called verbbox1 with a value of null
JTextField verbbox2;//makes an instance variable of type JTextField called verbbox2 with a value of null
JTextField verbbox3;//makes an instance variable of type JTextField called verbbox3 with a value of null
HashSet<String> nouns = new HashSet<String>();//makes new HashSet of type String called nouns
HashSet<String> adjectives = new HashSet<String>();//makes new HashSet of type String called adjectives
HashSet<String> verbs = new HashSet<String>();//makes new HashSet of type String called verbs
JList list;//makes a new instance variable of type JList called list with a value of null
Phrase funny1;//makes a new instance variable of type String called funny1 with a value of null
Phrase funny2;
Phrase funny3;
Phrase funny4;
JTextField savefield;//makes a new instance variable of type JTextField called savefield with a value of null
JButton save;//makes a new instance variable of type JButton called save with a value of null
Phrase currentAra;//makes a new instance variable of type String called p with a value of null
String[] nounarray;
String[] adjarray;
String[] verbarray;
int countnoun;
int countadj;
int countverb;
int ran1;
int ran2;
int ran3;
String a;
String b;
String c;
Phrase funny;
public static void main(String[] args){
try{
Phrasebot bot = new Phrasebot();//makes a new instance of Phrasebot called bot
}catch(Error Ex){
Ex.printStackTrace();
}
}
public Phrasebot(){
nounbox1 = new JTextField("Noun ");//sets nounbox1's value to a new JTextField
nounbox2 = new JTextField("Noun ");//sets nounbox2's value to a new JTextField
nounbox3 = new JTextField("Noun ");//sets nounbox3's value to a new JTextField
adjbox1 = new JTextField("Adjective ");//sets adjbox1's value to a new JTextField
adjbox2 = new JTextField("Adjective ");//sets adjbox2's value to a new JTextField
adjbox3 = new JTextField("Adjective ");//sets adjbox3's value to a new JTextField
verbbox1 = new JTextField("Verb ");//sets verbbox1's value to a new JTextField
verbbox2 = new JTextField("Verb ");//sets verbbox2's value to a new JTextField
verbbox3 = new JTextField("Verb ");//sets verbbox3's value to a new JTextField
a = "noun";
b = "adjective";
c = "verb";
nouns.add(a);//adds the current text of nounbox1 to nouns
adjectives.add(b);//adds the current text of adjbox1 to nouns
verbs.add(c);//adds the current text of verbbox1 to nouns
nounarray = nouns.toArray(new String[nouns.size()]);/*makes an array of Strings called nounarray with the value of whatever
is in nouns at the moment*/
adjarray = adjectives.toArray(new String[adjectives.size()]);/* makes an array of Strings called adjarray with the value of
whatever is in adjectives at the moment*/
verbarray = verbs.toArray(new String[verbs.size()]);/*makes an array of Strings called verbarray with the value of whatever
is in verbs at the moment*/
countnoun = nounarray.length;//makes a new variable of type int called countnoun with the value of nounarray's length
countadj = adjarray.length;//makes a new variable of type int called countadj with the value of adjarray's length
countverb = verbarray.length;//makes a new variable of type int called countverb with the value of verbarray's length
ran1 = (int) (Math.random() * countverb);//makes a new variable of type int called ran1 with a value of a random word from countverb
ran2 = (int) (Math.random() * countadj);//makes a new variable of type int called ran2 with a value of a random word from countadj
ran3 = (int) (Math.random() * countnoun);//makes a new variable of type int called ran3 with a value of a random word from countnoun
funny1 = new Phrase(adjarray[ran2]+ " ", nounarray[ran3]+ " ", verbarray[ran1]);/*sets funny1's value to a random word from adjarray,
nounarray, and verbarray*/
funny2 = new Phrase(nounarray [ran3]+ " ",adjarray [ran2]+ " ",verbarray [ran1]);
funny3 = new Phrase(adjarray [ran2]+ " ",verbarray [ran1]+ " ",nounarray [ran3]);
funny4 = new Phrase(verbarray[ran1]+ " ",adjarray[ran2]+ " ",nounarray[ran3]);
JPanel pcenter = new JPanel();//makes a new variable of type JPanel called pcenter with a value of a new JPanel()
JFrame frame = new JFrame("Phrasebot");//makes a new JFrame called "Phrasebot"
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets the default close operation to EXIT_ON_CLOSE
boxleft = new Box(BoxLayout.Y_AXIS);//sets boxleft's value to a new box on the y_axis
JPanel bigpanel = new JPanel();//makes a new JPanel called bigpanel
bigpanel.setLayout(new BoxLayout(bigpanel,BoxLayout.Y_AXIS));//sets bigpanel's default layout to BoxLayout
JPanel spacer = new JPanel();//makes a new JPanel called spacer
spacer.setLayout(new BoxLayout(spacer, BoxLayout.Y_AXIS));//sets spacer's layout to BoxLayout
JLabel label = new JLabel(" ");//makes a new JLabel
spacer.add(label);//adds label to spacer
bigpanel.add(spacer);//adds spacer to bigpanel
JPanel peast = new JPanel();//makes a new panel called peast
bigpanel.add(peast);//bigpanel adds peast
peast.setLayout(new BoxLayout(peast,BoxLayout.Y_AXIS));//sets peast's layout to boxlayout
JButton morenoun = new JButton("Add a Noun");//makes a new button called more noun with the text "More Nouns"
morenoun.addActionListener(new morenounListener());//morenoun adds an actionlistener which is a morenounlistener()
peast.add(morenoun);//peast adds morenoun
JLabel spacer2 = new JLabel(" ");//makes a new label called spacer2
peast.add(spacer2);//peast adds spacer2
JButton moreadj = new JButton("Add an Adjective");//makes a new button called moreadj with the text "More Adjectives"
moreadj.addActionListener(new moreadjListener());//moreadj adds an actionlistener which is a moreadjlistener()
peast.add(moreadj);//peast adds moreadj
JLabel spacer3 = new JLabel(" ");//makes a new label called spacer3
peast.add(spacer3);//peast adds spacer3
JButton moreverb = new JButton("Add a Verb");//makes a new button called moreverb with the text "More Verbs"
moreverb.addActionListener(new moreverbListener());//moreverb adds an actionlistener which is a moreverblistener()
peast.add(moreverb);//peast adds moreverb
JLabel savespacer = new JLabel(" ");//makes a new label called savespacer
peast.add(savespacer);//peast adds savespacer
JButton savewords = new JButton("Save Words");//makes a new button called save words
savewords.addActionListener(new savewordsListener());//savewords adds an actionlistener which is a savewordsListener()
//peast adds savewords
JButton savesession = new JButton("Save Phrases");//creates a new button called savesession with the text "Save Phrases"
savesession.addActionListener(new savesessionListener());//savesession adds an actionlistener which is a savesessionListener()
peast.add(savesession);//peast adds savesession
JLabel quickspacer = new JLabel(" ");//makes a new label called quickspacer
peast.add(quickspacer);//peast adds quickspacer
JButton quick1 = new JButton("Arrangements");//makes a new button called quick1 with the text "Quickie Button 1"
quick1.addActionListener(new quick1Listener());//quick1 adds an actionListener which is a quick1Listener()
peast.add(quick1);//peast adds quick1
JButton restart = new JButton("Restart");//makes a new variable of type JButton called restart with a value of a new JButton
restart.addActionListener(new quick2Listener());//restart adds an actionListener which is a quick2Listener()
peast.add(restart);//peast adds restart
JPanel pwest = new JPanel();//makes a new variable of type JPanel called pwest with a value of a new JPanel
pwest.setLayout(new BoxLayout(pwest,BoxLayout.Y_AXIS));//pwest set's its layout to the y axis
JPanel bigpanel2 = new JPanel();//makes a new variable of type JPanel called bigpanel2 with a value of a new JPanel
bigpanel2.add(pwest);//bigpanel2 adds pwest
Font midfont = new Font("Comic Sans MS", Font.BOLD, 18);//makes a new variable of type Font called mid font with a value of a new Font
midgui = new JTextArea(10,20);//sets midgui's value to a new JTextArea
midgui.setEditable(false);
midgui.setLineWrap(true);
midgui.setFont(midfont);
JScrollPane scroll = new JScrollPane(midgui);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pcenter.add(scroll);
currentAra = funny1;
JLabel spacer4 = new JLabel(" ");
pwest.add(nounbox1);
pwest.add(nounbox2);
pwest.add(nounbox3);
pwest.add(spacer4);
JLabel spacer5 = new JLabel(" ");
pwest.add(adjbox1);
pwest.add(adjbox2);
pwest.add(adjbox3);
pwest.add(spacer5);
pwest.add(verbbox1);
pwest.add(verbbox2);
pwest.add(verbbox3);
JPanel psouth = new JPanel();
JPanel bigpanel3 = new JPanel();
bigpanel3.add(psouth);
phrasemaker = new JButton("Make A Phrase!");
phrasemaker.addActionListener(new phrasemakerListener());
psouth.add(phrasemaker);
boxleft.add(nounbox1);
boxleft.add(nounbox2);
boxleft.add(nounbox3);
Box.createVerticalGlue();
boxleft.add(adjbox1);
boxleft.add(adjbox2);
boxleft.add(adjbox3);
Box.createVerticalGlue();
boxleft.add(verbbox1);
boxleft.add(verbbox2);
boxleft.add(verbbox3);
frame.setSize(1000,600);//sets the size of the JFrame
frame.setVisible(true);//sets the ability to see the JFrame
frame.getContentPane().add(BorderLayout.EAST, bigpanel);
frame.getContentPane().add(BorderLayout.WEST, boxleft);
frame.getContentPane().add(BorderLayout.SOUTH, bigpanel3);
frame.getContentPane().add(BorderLayout.CENTER, scroll);
}
class phrasemakerListener implements ActionListener{
public void actionPerformed(ActionEvent ev) {
try{
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
midgui.append(currentAra + "\n ");
phrasemaker.setText("Make Another!");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
class morenounListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.add(nounbox1.getText());
nounbox1.setText("");
}
}
class moreadjListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
adjectives.add(adjbox1.getText());
adjbox1.setText("");
}
}
class moreverbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
verbs.add(verbbox1.getText());
verbbox1.setText("");
}
}
class savewordsListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
FileOutputStream fileout = new FileOutputStream("words.pb");
ObjectOutputStream out = new ObjectOutputStream(fileout);
out.writeObject(nouns);
out.close();
midgui.append("words saved");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
class savesessionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JFrame frame = new JFrame();
savefield = new JTextField("Untitled.txt");
JButton save = new JButton("Save");
save.addActionListener(new savebuttonListener());
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(savefield);
frame.add(save);
frame.setSize(400,75);
}catch(Exception x){
x.printStackTrace();
}
}
}
class quick1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("Arrangements");
list = new JList();
DefaultListModel dlm = new DefaultListModel();
ListItem item1 = new ListItem("ANV", funny1);
ListItem item2 = new ListItem("NAV", funny2);
ListItem item3 = new ListItem("AVN", funny3);
ListItem item4 = new ListItem("VAN", funny4);
for(int i = 0; i<1;i++){
// add a new ListItem containing display text and a new value
dlm.addElement(item1);
dlm.addElement(item2);
dlm.addElement(item3);
dlm.addElement(item4);
}
list.setModel(dlm);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.addListSelectionListener(new listListener());
JButton set = new JButton("Done");
set.addActionListener(new setListener());
frame.getContentPane().add(BorderLayout.CENTER, list);
frame.getContentPane().add(BorderLayout.SOUTH, set);
frame.setSize(250,250);
frame.setVisible(true);
}
}
class quick2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.clear();
adjectives.clear();
verbs.clear();
nounbox1.setText("");
nounbox2.setText("");
nounbox3.setText("");
adjbox1.setText("");
adjbox2.setText("");
adjbox3.setText("");
verbbox1.setText("");
verbbox2.setText("");
verbbox3.setText("");
midgui.setText("");
}
}
class quick3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class midguiListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class savebuttonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String fileName = savefield.getText();
FileWriter writer = new FileWriter(fileName);
writer.write(midgui.getText());
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public void establishConnection(){
try{
Socket sock = new Socket("192.168.0.195", 5620);
}catch(Exception ex){
midgui.append("Couldn't get connection");
}
}
class setListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class listListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
ListItem selection = (ListItem) list.getSelectedValue();
setCurrentAra(selection.getValue());
}else{
}
}
}
public void setCurrentAra(Phrase thingy){
currentAra = thingy;
}
}
Code:
public class Phrase {
String noun;
String adjective;
String verb;
String all =adjective + " "+noun+" "+verb;
public Phrase(String n,String a,String v){
noun = n;
adjective = a;
verb = v;
}
String getNoun(){
return noun;
}
String getAdj(){
return adjective;
}
String getVerb(){
return verb;
}
public String toString(){
return all;
}
}
Another problem I'm having is that no components show up when I launch my program. All that shows up is the JFrame. To fix this I usually make the window a bit bigger and then the components show up. I don't know what could be causing this! HELP!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|