-
Me again! This time its a loop problem
I am trying to get my head around loops now!
Is there a way to count down from 60 seconds in a text box??
Would I be able to use a normal loop code??
i.e.
int count = 10;
System.out.println("Start!");
while (count > 0) {
System.out.println(count + "!");
count = count - 1;
}
System.out.println("Finish!");
}
}
Thanks!
Steve
-
-
My program runs fine without that piece of code but when I put it in and try to run it I get a message saying I have no main class selected?!?!
-
-
Are you doing a simulation of this countdown or do you really want this to be "60 seconds".
If this is a countdown, you can do this in a while loop, and it can be coded in a "for" loop:
for( count = 60; count > 0; count--)
{
continue;
}
System.out.println( "Finish");
If you are looking for real time, look at the compareTo() method of the Calendar class, or use the System.currentTimeMillis() static method.
-
I took the code out and i've tinkered with something else that has stopped my program working. I am getting the following errors...
init:
deps-jar:
Compiling 2 source files to Y:\CO2204\0402900\build\classes
Y:\CO2204\0402900\src\Mars.java:27: invalid method declaration; return type required
public Mars() {
Y:\CO2204\0402900\src\Mars.java:31: illegal start of type
try {
Y:\CO2204\0402900\src\Mars.java:244: <identifier> expected
}
3 errors
BUILD FAILED (total time: 0 seconds)
I know they are quite unspecific but can anybody see a problem with this coding??
I left out the variable declaration
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class Mars extends javax.swing.JFrame {
public class Database extends javax.swing.JFrame {
Statement statement = null;
String database = "jdbc:mysql://devcgi.compsci.chester.ac.uk/0402900";
//network version ******* = your student number
// String database = "jdbc:mysql://localhost/test"; // syntax for own computer
public Mars() {
initComponents();
}
try {
// Step 1: Load the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Establish the connection to the database.
Connection conn = DriverManager.getConnection(database,"0402900","0402900");
// ******* is your student number
System.out.println("Step 2 ... fine");
statement = conn.createStatement();
} catch (Exception e) {
System.err.println("Got an exception at Step 1 or 2! ");
System.err.println(e.getMessage());
/** Creates new form Mars */
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jDialog1 = new javax.swing.JDialog();
jPanel1 = new javax.swing.JPanel();
lblTitle = new javax.swing.JLabel();
lblMission = new javax.swing.JLabel();
lblJob = new javax.swing.JLabel();
lblName = new javax.swing.JLabel();
cmdCancel = new javax.swing.JButton();
cmdOk = new javax.swing.JButton();
txtMission = new javax.swing.JTextField();
txtJob = new javax.swing.JTextField();
txtName = new javax.swing.JTextField();
lblEmblem = new javax.swing.JLabel();
cmdCreate = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
cmdMission = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
txtCount = new javax.swing.JTextField();
jDialog1.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
lblTitle.setFont(new java.awt.Font("Century Gothic", 1, 14));
lblTitle.setText("Mission Information");
jPanel1.add(lblTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 10, -1, -1));
lblMission.setFont(new java.awt.Font("Century Gothic", 0, 12));
lblMission.setText("Mission Number");
jPanel1.add(lblMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 180, -1, -1));
lblJob.setFont(new java.awt.Font("Century Gothic", 0, 12));
lblJob.setText("Job Title");
jPanel1.add(lblJob, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 150, -1, -1));
lblName.setFont(new java.awt.Font("Century Gothic", 0, 12));
lblName.setText("Name");
jPanel1.add(lblName, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 120, -1, -1));
cmdCancel.setText("Cancel");
cmdCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdCancelActionPerformed(evt);
}
});
jPanel1.add(cmdCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 220, 80, -1));
cmdOk.setText("Ok");
cmdOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdOkActionPerformed(evt);
}
});
jPanel1.add(cmdOk, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 220, 80, -1));
jPanel1.add(txtMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 180, 70, -1));
jPanel1.add(txtJob, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 150, 70, -1));
txtName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNameActionPerformed(evt);
}
});
jPanel1.add(txtName, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 120, 70, -1));
jPanel1.add(lblEmblem, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, -1, -1));
cmdCreate.setText("Create");
cmdCreate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdCreateActionPerformed(evt);
}
});
jPanel1.add(cmdCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 250, -1, -1));
jDialog1.getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 240, 270));
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
cmdMission.setText("Mission Info");
cmdMission.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdMissionActionPerformed(evt);
}
});
jPanel2.add(cmdMission, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 340, 110, 20));
jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 480, 280));
txtCount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCountActionPerformed(evt);
}
});
jPanel2.add(txtCount, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 340, 30, -1));
getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 490, 360));
pack();
}
// </editor-fold>
private void cmdCreateActionPerformed(java.awt.event.ActionEvent evt) {
String str = "CREATE TABLE " + txtTableName.getText()+ " (Name VARCHAR(20), Job Title VARCHAR(20). Mission Number(20))";
try{
statement.executeUpdate(str);
System.out.println("Table " + txtTableName.getText() + " Created !");
} catch(Exception e){
e.printStackTrace();
}
}
private void txtCountActionPerformed(java.awt.event.ActionEvent evt) {
}
private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {
String str = "INSERT INTO " + txtTableName.getText()+ " VALUES('" + txtName.getText() + "','" + txtJob.getText() + "','" + txtMission.getText() + "')";
try{
statement.executeUpdate(str);
System.out.println("Insert Performed !");
} catch(Exception e){
e.printStackTrace();
}
}
setVisible(false);
confirm = true;
}
private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
}
private void cmdMissionActionPerformed(java.awt.event.ActionEvent evt) {
Dialog d = new Dialog(this,true); // the modal argument is set to true
d.setVisible(true);
txtName.setText(d.getName());
}
public void handleButton(ActionEvent evt) {
Icon icon = new ImageIcon(getClass().getResource("PE7401.jpeg"));
lblEmblem.setIcon(icon);
}
/**
* @param args the command line arguments
*/{
boolean confirm = false;
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mars().setVisible(true);
}
});
}
-
Why do you have a Database class nested inside your Mars class.
You have a lot of bracket errors. Go through all your brackets to make sure everything is in a method.
Also, you have spaces in the word ActionEvent.. you might wanna get rid of them.
One more thing.. I'm hoping you have more code than that because I think every single one of your variables has not been defined.
Last edited by destin; 01-16-2006 at 10:10 AM.
-
Because I have a form that is using SQL
Yeah i didnt post my variables.
Thanks!
-
Ok I ahve sorted all of my problems out and Im back to the loop!
When I put that code in for the text box and run the program I still have a blank text box ??
 Originally Posted by destin
-
A basic ticker...
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CountDownTF extends JTextField implements ActionListener {
Timer t=null;
private int secondsLeft=-1;
public CountDownTF () {
t=new Timer(1000,this);
}
public void setCountdownTime(int secondsLeft) {
setText(Integer.toString(secondsLeft));
this.secondsLeft=secondsLeft;
}
public void start() {
if (!t.isRunning())
t.start();
}
public void stop() {
if (t.isRunning()) t.stop();
}
public void actionPerformed(ActionEvent e) {
secondsLeft--;
if (secondsLeft == 0)
stop();
setText(Integer.toString(secondsLeft));
}
}
eschew obfuscation
-
Code:
Thread t = new Thread();
t.start();
for (int i = 60; i >= 0; i--) {
System.out.println(i);
try {
t.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Does that code even work? Don't you need to assign the thread to the main class, or implement or extend Runnable? It looks like that would create a new thread, run stuff and tell that thread to sleep. However, the action is going on outside of the thread, so I would think it would continue while the thread sleeps, and then it will try to make an already sleeping thread sleep, probably throwing an exception...
I'm pretty sure you can simply control the main thread by reffering to it as Thread... ie:
Code:
try {
for (int i=0; i < 5; i++) {
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
Alternatively you could keep how you have t sleeping, but instead of creating a new thread, you can assign it to the main thread by:
Code:
Thread t = Thread.currentThread();
-
Thread.sleep is static. You can create a bazillion instances of thread, and call any of their .sleep() methods, and you'll still be calling the Thread.sleep() method in the class, not in that instance.
Calling Thread.sleep() tells the CURRENT thread to sleep. In the example of making a new Thread and putting it into t, then calling t.sleep(), this has no affect whatsoever on how t runs.
Last edited by Laszlo; 01-25-2006 at 03:31 PM.
-
 Originally Posted by smartr
Does that code even work?
Well, there's an extra bracket, but w/o that, yeah it works.
But yeah, it would be better to just do Thread.sleep; and get rid of Thread t.
Last edited by destin; 01-25-2006 at 03:37 PM.
Similar Threads
-
By Code_Writer in forum VB Classic
Replies: 11
Last Post: 08-14-2005, 08:50 PM
-
Replies: 0
Last Post: 10-30-2002, 04:40 AM
-
Replies: 0
Last Post: 10-30-2002, 04:39 AM
-
By Ofir in forum oracle.general
Replies: 0
Last Post: 06-24-2002, 05:28 AM
-
By Vicki in forum Enterprise
Replies: 2
Last Post: 11-30-2000, 03:23 AM
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks