GUI Not Displaying?
Hi I'm back,
I have no more errors, but I must have forgot somethings, because when I run the program the GUI doesn't display. I instantiate the class its in, shouldn't that work? Thank you.
I also checked that it does enter the class, and since I'm usuing Netbeans, calls initComponents();, so why doesn't it work?
Also, does Netbeans automatically add the objects so they'll be recognized by Actionlistener?
Message was edited by:
Dest
[481 byte] By [
Desta] at [2007-10-2 21:45:57]

post code (inside code brackets) for more detailed answers. However, it sounds like you haven't called setvisible.
/*
* Main.java
*
*
*
*
*/
package woot;
import java.awt.event.*;
public class Main extends NewJPanel {
BattleShip BSHIP;
Work pie;
/** Creates a new instance of Main */
public Main() {
BSHIP = new BattleShip();
pie = new Work();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args){
while(BSHIP.playerShipDamage < 14 && BSHIP.enemyShipDamage < 14) {
}
}
public class Work implements ActionListener {
int ShipCounter = 2;
int HorV = 1;
public void actionPerformed(ActionEvent e) {
BSHIP = new BattleShip();
String info = (String)e.getActionCommand();
System.out.println("Info = " + info);
if(e.getActionCommand()=="NewGame"){
//CALL NEW GAME
BSHIP = new BattleShip();
}
if(e.getActionCommand()=="Vertical"){
HorV=0;
}
if(e.getActionCommand()=="Horizontal"){
HorV=1;
}
if((info.substring(0,2)).equals("jj")){
//Ship Placement
if(ShipCounter>5){
System.out.println("No more ships");
} else {
String r = info.substring(2,3);
String c = info.substring(3,4);
Integer Bill = new Integer(r);
Integer Bob = new Integer(c);
int row = Bill.intValue();
int col = Bob.intValue();
if(row==0)
row = 10;
if(col==0)
col = 10;
BSHIP.makePlayerShip(row,col,ShipCounter,HorV);
}
} else if((info.substring(0,1)).equals("j")){
//Target Button Selected
String r = info.substring(1,2);
String c = info.substring(2,3);
Integer Bill = new Integer(r);
Integer Bob = new Integer(c);
int row = Bill.intValue();
int col = Bob.intValue();
if(row==0)
row = 10;
if(col==0)
col = 10;
BSHIP.shootEnemy(row,col);
BSHIP.enemyShoot();
}
}
}
}
Do you want the other classes?
Desta at 2007-7-14 1:01:32 >

another point. if you want to learn to program, using an IDE (integrated development environment) that does a lot of stuff for you is a bad idea. The stuff that the IDE does for you is stuff that you need to understand.
You should always be able compile your code using a command line interface.
how are you running this prorgam and what do you think is happening?
Netbeans 5.0, and I started learning java on BlueJ, but I needed to make a more compliacted GUI so I was told to use Netbeans. Nothing happens except the system.out statements that tell me it gets to those classes.
I've never even used a command line before, I don't think schools nowadays trust kids enough to know that much.
I really need this to work, I bet if I wrote the GUI by hand it would work but Netbeans confused me, though it does look nice. This projects due soon...thank you to all who help.
Desta at 2007-7-14 1:01:32 >

I cannot help you with the code you have provided other than to say It looks a mess.
If I were you I would start from scratch. break the development down into stages that can be tested along the way. whenever you can write a test class accompanying each class you write that will check that things are working how you expect them to (JUnit test if you have heard of that, but if you are new to java just write a regular class).
However if you post the rest of your code I will try to determine what your specific problem is.
Message was edited by:
Ken_S (some men were born with the ability to spell, and some were born increadably handsom)
Is there something that instaltializes the GUI or that needs to be done for it to work and display?
Desta at 2007-7-14 1:01:32 >

yes, as I mentioned the setvisible method: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#setVisible(boolean)
Ok I tried that, still didn't work. Thanks for your help anyways.
Desta at 2007-7-14 1:01:32 >

Look at my simple application below. Pay attention to lines 21 and 22. Do you have something similar in your program?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
public class TwoOfHearts extends JFrame
{
public TwoOfHearts()
{
super("StaceyQ is like, the best!");
setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.add(new JLabel("Taylor Dayne is wonderful."));
JPanel p2 = new JPanel();
p2.add(new JLabel("So is Tiffany."));
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(p2, BorderLayout.SOUTH);
getContentPane().add(new JLabel("Debbie Gibson, too!"), BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main (String[] args) { new TwoOfHearts(); }
}
One quick thought....
I hope you have at least two source files.The first rule when declaring class is there can pnly be one public class per source code file. If what you posted is in one file, the second public class work will give you problems. Dont know if thats your problem or not, but it's what jumped out at me.
JJ