jaskali
November 7th, 2006, 05:29 AM
The CODE is:
//testi.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testi extends JPanel{
/**
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() { createAndShowGUI(); }
} );
}
static JTextField tx01=new JTextField();
static JTextField tx02=new JTextField();
static JTextField tx03=new JTextField();
public class PushButtonActionListener implements ActionListener{
JFrame frame = null;
public PushButtonActionListener(JFrame af) {
this.frame = af;
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("count")){
try
{
int value1 = Integer.valueOf(tx01.getText()).intValue();
int value2 = Integer.valueOf(tx03.getText()).intValue();
tx02.setText(Integer.toString(value1*value2));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Error");
}
}
}
}
public static void createAndShowGUI(){
JFrame frame = new JFrame("Area");
//frame.getContentPane().add(paneeli);
frame.setLayout(new GridLayout(4,2));
JButton bt01=new JButton("Count");
bt01.setActionCommand("count");
JPanel paneeli = new JPanel();
paneeli.setPreferredSize(new Dimension(200,100));
JLabel lb01=new JLabel("Width");
JLabel lb02=new JLabel("Height");
JLabel lb03=new JLabel("Area");
JLabel lb04=new JLabel(" ");
frame.getContentPane().add(lb01);
frame.getContentPane().add(lb03);
frame.getContentPane().add(tx01);
frame.getContentPane().add(tx02);
frame.getContentPane().add(lb02);
frame.getContentPane().add(lb04);
frame.getContentPane().add(tx03);
frame.getContentPane().add(bt01);
//----------------------------------------------------
bt01.addActionListener(new PushButtonActionListener(frame)); //THIS IS THE LINE where both errors occur
//-----------------------------------------------------
frame.pack();
frame.setVisible(true);
}
}
i get this error message in eclipse 3.2:
"No enclosing instance of type testi is accessible. Must qualify the allocation with an enclosing instance of type testi (e.g. x.new A() where x is an instance of testi). "
and if i create new interface file Actionlistener.java i get error:
"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (testi.PushButtonActionListener)"
//testi.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testi extends JPanel{
/**
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() { createAndShowGUI(); }
} );
}
static JTextField tx01=new JTextField();
static JTextField tx02=new JTextField();
static JTextField tx03=new JTextField();
public class PushButtonActionListener implements ActionListener{
JFrame frame = null;
public PushButtonActionListener(JFrame af) {
this.frame = af;
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("count")){
try
{
int value1 = Integer.valueOf(tx01.getText()).intValue();
int value2 = Integer.valueOf(tx03.getText()).intValue();
tx02.setText(Integer.toString(value1*value2));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Error");
}
}
}
}
public static void createAndShowGUI(){
JFrame frame = new JFrame("Area");
//frame.getContentPane().add(paneeli);
frame.setLayout(new GridLayout(4,2));
JButton bt01=new JButton("Count");
bt01.setActionCommand("count");
JPanel paneeli = new JPanel();
paneeli.setPreferredSize(new Dimension(200,100));
JLabel lb01=new JLabel("Width");
JLabel lb02=new JLabel("Height");
JLabel lb03=new JLabel("Area");
JLabel lb04=new JLabel(" ");
frame.getContentPane().add(lb01);
frame.getContentPane().add(lb03);
frame.getContentPane().add(tx01);
frame.getContentPane().add(tx02);
frame.getContentPane().add(lb02);
frame.getContentPane().add(lb04);
frame.getContentPane().add(tx03);
frame.getContentPane().add(bt01);
//----------------------------------------------------
bt01.addActionListener(new PushButtonActionListener(frame)); //THIS IS THE LINE where both errors occur
//-----------------------------------------------------
frame.pack();
frame.setVisible(true);
}
}
i get this error message in eclipse 3.2:
"No enclosing instance of type testi is accessible. Must qualify the allocation with an enclosing instance of type testi (e.g. x.new A() where x is an instance of testi). "
and if i create new interface file Actionlistener.java i get error:
"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (testi.PushButtonActionListener)"