PDA

View Full Version : Button not working in java...



rj
01-16-2013, 04:09 AM
I made a button in java and when I click it it is supposed to show the message in the debug box.





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

public class Mypanel extends JPanel {
private JButton Button;

public Mypanel() {
//construct components
Button = new JButton ("DOmath");

//adjust size and set layout
setPreferredSize (new Dimension (944, 585));
setLayout (null);

//add components
add (Button);

//set component bounds (only needed by Absolute Positioning)
Button.setBounds (370, 260, 140, 20);
}


public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Mypanel());
frame.pack();
frame.setVisible (true);
}
private void jDOmathActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Hello");
}


}



I also tried:


public class Mypanel extends JPanel {
private JButton Button;

public Mypanel() {
//construct components
Button = new JButton ("DOmath");

//adjust size and set layout
setPreferredSize (new Dimension (944, 585));
setLayout (null);

//add components
add (Button);

//set component bounds (only needed by Absolute Positioning)
Button.setBounds (370, 260, 140, 20);
}


public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Mypanel());
frame.pack();
frame.setVisible (true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}


}









I have also tried this but I get an error:


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

public class Mypanel extends JPanel {
private JButton Button;

public Mypanel() {
//construct components
Button = new JButton ("DOmath");

//adjust size and set layout
setPreferredSize (new Dimension (944, 585));
setLayout (null);

//add components
add (Button);

//set component bounds (only needed by Absolute Positioning)
Button.setBounds (370, 260, 140, 20);
}


public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Mypanel());
frame.pack();
frame.setVisible (true);

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello");
}


});
}
}

"button cannot be resolved' No matter what I change the variable to it sais variable + cannot be resolved
Don't see whats wrong...

Brandon
01-16-2013, 05:05 PM
"button" is not defined.. You need "Button" for your last example/try.

Java is case-sensitive. Be careful. It's Button.add... not button.add...

rj
01-16-2013, 08:47 PM
"button" is not defined.. You need "Button" for your last example/try.

Java is case-sensitive. Be careful. It's Button.add... not button.add...

Eclipse did a quick fix that made something static so it would work. now I need help with j text feilds

rj
01-16-2013, 09:15 PM
yup now im having trouble adding muiltiple buttons