Welcome to Gaia! ::

Reply
Hello and Help. :P

Quick Reply

Enter both words below, separated by a space:

Can't read the text? Click here

Submit

STLeeJay

PostPosted: Sun Nov 05, 2006 3:46 pm


Hello. My name is AxeFalador. I am a newcomer to Java programming, and a newcomer to this guild. I am very grateful to have been accepted to this guild. I'm hoping it will be a good learning experience.

Now, on to what I need help with.

I'm currently working on my very first fully function Java Swing GUI Application, using Matisse on NetBeans 5.0.

I am writing your basic "electronic magic 8-ball" GUI, with the goal that the user types in a yes/no question in a JTextField, and clicks a JButton, and the program displays, in a separate JTextPane, an answer randomly chosen from a pool of pre-composed answers.

I have programmed a few basic functions into the GUI, but I can't get the last few bumps rolled out, so to speak. I have a fully functional exit button, and the "Ask..." button (the JButton that's supposed to generate the random answer in the separate JTextPane) has only one answer it can display in the JTextPane. I can't seem to figure out just how to make the JTextPane display randomly selected answers when the "Ask..." JButton is clicked. I can only figure out how to display a single answer.

Here is what I have for code, so far (most of it was automatically generated by NetBeans' Matisse GUI Builder):

/*
* cyberspaceoracleui.java
*
* Created on October 31, 2006, 1:37 PM
*/

package my.cyberspaceoracleui;

/**
*
* @author falador@eastlink.ca
*/
public class cyberspaceoracleui extends javax.swing.JFrame {

/** Creates new form cyberspaceoracleui */
public cyberspaceoracleui() {
initComponents()
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel()
jLabel2 = new javax.swing.JLabel()
jLabel3 = new javax.swing.JLabel()
jTextField1 = new javax.swing.JTextField()
jButton1 = new javax.swing.JButton()
jScrollPane1 = new javax.swing.JScrollPane()
jTextPane1 = new javax.swing.JTextPane()
jButton2 = new javax.swing.JButton()
jLabel4 = new javax.swing.JLabel()

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
setTitle("The CyberSpace Oracle")
jLabel1.setText("Greetings, my dear.")

jLabel2.setText("What queries dost thou haveth for me this fine day?")

jLabel3.setText("Please ask only yes/no questions.")

jButton1.setText("Ask...")
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt)
}
})

jTextPane1.setEditable(false)
jScrollPane1.setViewportView(jTextPane1)

jButton2.setText("Farewell")
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt)
}
})

jLabel4.setText("Your answer will be displayed below...")

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane())
getContentPane().setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap(10, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2)))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel4))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel2))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel3))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jButton1)))
.addContainerGap())
)

layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL)

layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2)
.addContainerGap())
)

layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.VERTICAL)

pack()
}//


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("")
jTextPane1.setText("I'm sorry, my dear, but I am just not mentally prepared to answer that question, right now. Please ask again later.")
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0)
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cyberspaceoracleui().setVisible(true)
}
})
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration

}



Here's a preview screenshot of my GUI as it looks so far. (thought it might help you help me).:
User Image

Any tips, help or advice you could give me would be greatly appreciated!
PostPosted: Sun Nov 05, 2006 3:57 pm


User Image - Blocked by "Display Image" Settings. Click to show.User Image - Blocked by "Display Image" Settings. Click to show.
User Image - Blocked by "Display Image" Settings. Click to show.

Arg. Java. x_X

Formatted by GaiArch.

WindPowa
Crew


Lieutenant Frost

PostPosted: Tue Nov 07, 2006 7:35 am


Okay...maybe it's just your IDE, but I'm really confused over why there are a bunch of actionPerformed() methods...I usually make only one actionPerformed method, and set actionCommand properties for each component that will need one...so it'll look like this:


JButton button = new JButton("Close")
button.addActionListener(this)
button.setActionCommand("Close")


That way, when the actionPerformed() method is called, you can just extract the actionCommand to a string, and use a series of if statements to decide what to do:


public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand()

if(arg == "Close")
{
System.exit(0)
}

if(arg == "Blah")
{
foo()
}
}


And so on. Now, as far as getting it to display randomly selected answers in your JTextPane, you will first have to import the Random class and construct a random number generator:

import java.util.Random;

//Inside the class itself...
Random randomNum = new Random()


When you're ready to actually get the random number, you will need to call the nextInt method, assuming you're looking for an integer value:


int result;
result = randomNum.nextInt(2)
//If you need a certain range...
//result = randomNum.nextInt(2) + 5;


Notice that you will need to tell the generator the range of random numbers you are looking for; the (2) indicates that it will choose from two random numbers and return either 0 or 1. If necessary, you can seed the value by adding or subtracting from the number before assigning it to result.

Now, if I'm reading you correctly, you want your program to show either "Yes" or "No" in the results window. It's easy enough to accomplish; you will need to create an array of values to select from, and then get a random value from the array when you click your ask button:


String[] answers = {"Yes", "No"}

//After setting up your buttons and getting your random number generator ready, you would place the following code inside your actionPerformed method

int result = randomNum.nextInt(2)
jTextPane1.setText("")
jTextPane1.setText(answers[result])


Note also that by using this method, you can easily expand functionality to make your program a true "Magic 8-Ball" by simply adding more arguments into the answers array and increasing the value used in the nextInt method to match the number of items stored in the array.

At any rate, you might not have to actually perform the first setText call, but I like to do it just to make sure that everything has been cleared out.

That should pretty much do it. If you want, I can post an example of a program that I made which pretty much did the same thing. Hope this helps. surprised
Reply


 
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum