Tweet Follow @LawBlogger1   

Advertisments:


Sponsor Links:

Bar Exam Flashcards
Discount Legal Forms
Discounted Legal Texts

What is wrong with my code?

  
Tweet

What is wrong with my code?

Postby khalid » Mon Oct 31, 2011 9:22 pm

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class actionTest implements ActionListener {
JFrame frame;
JPanel panel;
JTextField textField;
JButton button[];
Container contentPane;

void launchFrame() {
frame = new JFrame("Action Test");
panel = new JPanel();
textField = new JTextField("0", 20);
button = new JButton[2];
contentPane = frame.getContentPane();

button[1] = new JButton("1");
button[2] = new JButton("2");

panel.add(textField);
panel.add(button[1]);
panel.add(button[2]);
contentPane.add(panel);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
if(event.getSource() == button[1]) {
textField.setText("Button 1 pressed.");
}
else if(event.getSource() == button[2]) {
textField.setText("Button 2 pressed.");
}
else
textField.setText("Error.");
}
public static void main(String[]args) {
actionTest at = new actionTest();
at.launchFrame();
}
}
khalid
 
Posts: 0
Joined: Thu Mar 31, 2011 8:42 am
Top

What is wrong with my code?

Postby urian27 » Mon Oct 31, 2011 9:23 pm

(1) Java uses zero-based array indexing.

The valid indexes of button[] are [0] and [1], not [1] and [2]. This needs to be fixed in 3 places.

(2) You make a class which implements ActionListener, but don't make the instance an ActionListener of anything. Try something like:

button[0].addActionListener(this);

... after creating each button.
urian27
 
Posts: 0
Joined: Fri Apr 01, 2011 8:27 pm
Top

What is wrong with my code?

Postby bellden » Mon Oct 31, 2011 9:34 pm

hi

i fixed the code...... hope it works fine......


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class actionTest implements ActionListener {
JFrame frame;
JPanel panel;
JTextField textField;
JButton button[];
Container contentPane;

void launchFrame() {
frame = new JFrame("Action Test");
panel = new JPanel();
textField = new JTextField("0", 20);
button = new JButton[2];
contentPane = frame.getContentPane();

button[0] = new JButton("1");
button[1] = new JButton("2");

button[0].addActionListener(this);
button[1].addActionListener(this);

panel.add(textField);
panel.add(button[0]);
panel.add(button[1]);
contentPane.add(panel);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
if (event.getSource() == button[0]) {
textField.setText("Button 1 pressed.");
} else if (event.getSource() == button[1]) {
textField.setText("Button 2 pressed.");
} else
textField.setText("Error.");
}

public static void main(String[] args) {
actionTest at = new actionTest();
at.launchFrame();
}
}
bellden
 
Posts: 0
Joined: Thu Mar 31, 2011 8:19 pm
Top

What is wrong with my code?

Postby zadok » Mon Oct 31, 2011 9:36 pm

As far as I can tell, the only thing wrong with it is here:
public static void main(String[]args) {

There should be a space before "args", like this:
public static void main(String[] args) {

Otherwise, you might have to tell us any errors you're getting or any logic issues you're having with the code.
zadok
 
Posts: 0
Joined: Fri Apr 01, 2011 4:39 am
Top


Return to Class Action

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests