Author Topic: Needs some help with Java (NOT Script)  (Read 1237 times)

0 Members and 1 Guest are viewing this topic.

Offline aldo_14

  • Gunnery Control
  • 213
Needs some help with Java (NOT Script)
Um... need a little help.  Just spent about an hour refreshing my memory on the awt stuff, and I've hit a snag... the window isn;t showing up.  no error output, compiles fine - all that happens is that it 'freezes' at the command line (i.e. no window popping up, just acts as if it's in an infinite loop)and I have to hit control-C to quit it.

It's eventually going to be a little program used to update a (html) news page.. at the mo it's very basic, but i may get round to incorporating stuff to save and use set styles, preview colours, a preview bit (prob just create a temp  file and pop up a browser or summat...) and soforth.  May be of use to people once it's done.

Code: [Select]

/*
* Alan White 25/11/2002
* Program to append data to an html file
*
* 25/11/02
- Initial GUI code (no functionality)
- NB:  Error - window not showing!!
*/

public class Driver
{
public static void main(String args[])
{
if(args.length==0)
{
System.out.println("Error!");
System.out.println("Input format;   java Driver ");
System.out.println("  = name of the html file");
}
else //good to go!
{
/*
Create text entry window  -args[0] is the name
*/
AddNewsDataWindow mainWin=new AddNewsDataWindow(args[0]);
}
}

}


This bit 'ere is the meatiest part...where the functionality will actually be implemented.  ?Once it works :(

Code: [Select]

/*
* Alan White 25/11/02
* Takes data in to add to the html file
*
TBC;
- Preview screen
- colour preview
*
* 21/11/02
- Why the stupid name?
- Added objects for standard operation, with 'dummy' vars.
- No output writing stuff yet!
*/
import java.awt.*;
import java.awt.event.*;

public class AddNewsDataWindow extends Frame implements ActionListener

{
//////variables//////
protected Button ok;
protected Button cancel;
protected Label title; //of program
protected Label header; //input header data
protected Label newsItem;//input news data
protected Label headerTblColour;
protected Label bodyTblColour;
protected Label bodyFont;
protected Label bodyColour;
protected Label itemFont;
protected Label itemColour;
/*
Allows user to set;
Title and text for news
Table background colours
Text colour and fonts

Input items;
*/
protected Checkbox[] border; //does table have a border?
protected CheckboxGroup Boxes;
protected TextField inHeader;
protected TextField inNewsItem;
protected TextField inHeaderTblColour;
protected TextField inBodyTblColour;
protected TextField inBodyFont;
protected TextField inBodyColour;
protected TextField inItemFont;
protected TextField inItemColour;

//panels
protected Panel datapanel; //panel for data
protected Panel buttonspanel; //panel for buttons

private String editFile;//file to be edited (assume html)

private static String WINDOW_TITLE="News Thing";

private int checker; //used for error checking
//////constructor//////
/*
Constructs the window into which data is added.
*/
public AddNewsDataWindow(String fname)
{
int checker=0;
try{
editFile=fname;

Font f1 = new Font("Arial", Font.PLAIN, 16);
    this.setFont(f1);

this.setTitle(WINDOW_TITLE);

ok = new Button("Append News");
ok.addActionListener(this);

cancel = new Button("Close window");
ok.addActionListener(this);

checker++; //1
//create labels
title = new Label("News Appender");
header = new Label("News title header text");
newsItem = new Label("News item text");
headerTblColour = new Label("Header bg colour");
bodyTblColour = new Label("body bg colour");
bodyFont = new Label("Body text font");
bodyColour = new Label("Body text colour");
itemFont = new Label("Item text font");
itemColour = new Label("Item text colour");

checker++; //2
//create input fields
// Font f2 = new Font("Arial", Font.PLAIN, 12);
inHeader = new TextField(100);//100 chars
  //   inHeader.setFont(f2);
inNewsItem = new TextField(1000);//1000 chars
    // inNewsItem.setFont(f2);
inHeaderTblColour = new TextField(20);//20 chars
    // inHeaderTblColour.setFont(f2);
inBodyTblColour = new TextField(20);//20 chars
    // inBodyTblColour.setFont(f2);    
inBodyFont= new TextField(20);//20 chars
    // inBodyFont.setFont(f2);
inBodyColour= new TextField(20);//20 chars
    // inBodyColour.setFont(f2);
inItemFont= new TextField(20);//20 chars
    // inItemColour.setFont(f2);
inItemColour= new TextField(20);//20 chars
    // inItemColour.setFont(f2);

checker++;//3
   
//create border checkbox

Boxes=new CheckboxGroup();
border=new Checkbox[1];
border[0]= new Checkbox("Border",Boxes,false);
checker++;//4

//now create panels and add items
datapanel=new Panel();
datapanel.add(title);
datapanel.add(header);
datapanel.add(inHeader);
datapanel.add(newsItem);
datapanel.add(inNewsItem);
datapanel.add(headerTblColour);
datapanel.add(inHeaderTblColour);
datapanel.add(bodyTblColour);
datapanel.add(inBodyTblColour);
datapanel.add(bodyFont);
datapanel.add(inBodyFont);
datapanel.add(bodyColour);
datapanel.add(inBodyColour);
datapanel.add(itemFont);
datapanel.add(inItemFont);
datapanel.add(itemColour);
datapanel.add(inItemColour);
buttonspanel=new Panel();
buttonspanel.add(ok);
buttonspanel.add(cancel);

checker++;//5

this.add("North",datapanel);
this.add("South",buttonspanel);
this.pack();
}
catch(Exception e)
{
System.out.println("(Const "+checker+") An Exception was caught in AddNewsDataWindow constructor;");
System.out.println(e);
System.exit(0);
}
}

//////methods//////

//actionlistener
public void actionPerformed(ActionEvent event)
{
//controls buttons!
if ((event.getSource() == cancel))
{
    this.dispose();
    System.exit(0); // quits the program
}
else if ((event.getSource()== ok))
{

}/*
else
{
//eh?  how this happen?
System.out.println("EEEPPP - Error ANDW-aP-else");
this.dispose();
    System.exit(0);
  }*/

}



NB:  This is literally 1 hour of work, so no style criticisms please... 'spech on the font bits which I've commented out for the mo.

 

Offline vyper

  • 210
  • The Sexy Scotsman
Needs some help with Java (NOT Script)
Give a fool a chance to help and let me see your main method... :)
"But you live, you learn.  Unless you die.  Then you're ****ed." - aldo14

 

Offline RandomTiger

  • Senior Member
  • 211
Needs some help with Java (NOT Script)
this.setVisible(true); at the end of your constructor.
On a side note I cant seem to quit your program!

 

Offline aldo_14

  • Gunnery Control
  • 213
Needs some help with Java (NOT Script)
Quote
Originally posted by RandomTiger
this.setVisible(true); at the end of your constructor.
 


Cheers

Quote
Originally posted by RandomTiger
On a side note I cant seem to quit your program!


That's because it's unreservedly pish.

  

Offline aldo_14

  • Gunnery Control
  • 213
Needs some help with Java (NOT Script)
Code: [Select]

[color="red"]
   cancel = new Button("Close window");                ok.addActionListener(this);
[/color]


Excuse me, I'm off to headbutt the wall......