![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | ||
|
Expert Programmer
|
Quote:
Quote:
|
||
|
|
|
|
|
#12 |
|
Expert Programmer
|
GRRR java annoys me.. it is nothing like c++!
[php] import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.BorderLayout; import java.io.*; public class lettercount extends JFrame { public lettercount() { this(""); } public lettercount(String title) { super(title); scanButton fileButton=new scanButton(); fileButton.setBounds(220,20,72,20); // x, y, width, height fileButton.setVisible(true); JTextField filePath=new JTextField(); filePath.setBounds(20,20,200,20); filePath.setVisible(true); getContentPane().setLayout(null); getContentPane().add(fileButton); getContentPane().add(filePath); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(450,250); setVisible(true); } public static void main(String args[]) { new lettercount("Letter Counter"); char[] letterArray=new char[26]; int[] letterCount=new int[26]; int count=0; for(int i=97;i<=122;i++) { letterArray[count]=(char)i; System.out.printf("%d = %s\n",count,letterArray[count]); count++; } } private static void scanFile(String tempFile) { //RandomAccessFile f = new RandomAccessFile ("file.in", "r"); //PrintWriter out = new PrintWriter( // new BufferedWriter( // new FileWriter("file.out"))); //String firstLine = f.readLine(); //out.print(firstLine); //out.close(); } } class scanButton extends JButton implements ActionListener { scanButton() { super("Scan File"); addActionListener(this); } public void actionPerformed(ActionEvent arg0) { scanFile(filePath.getText()); } } [/php] i need my scanFile function to be called from my button class! I understand access modifers and such but don't understand how to make this possible in Java.
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#13 |
|
Expert Programmer
|
You need to put the ScanButton class inside the lettercount class (BTW, all public classes should start with a capital letter).
Alternatively you could put the ScanButton class in another file (ScanButton.java) and make the lettercount class recognize it making package package_name; the first line in both files. |
|
|
|
|
|
#14 |
|
Expert Programmer
|
still getting the same access errors "Cannot find symbol"
[php] import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.BorderLayout; import java.io.*; public class lettercount extends JFrame { public lettercount() { this(""); } public lettercount(String title) { super(title); ScanButton fileButton=new ScanButton(); fileButton.setBounds(220,20,72,20); // x, y, width, height fileButton.setVisible(true); JTextField filePath=new JTextField(); filePath.setBounds(20,20,200,20); filePath.setVisible(true); getContentPane().setLayout(null); getContentPane().add(fileButton); getContentPane().add(filePath); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(450,250); setVisible(true); } public static void main(String args[]) { new lettercount("Letter Counter"); char[] letterArray=new char[26]; int[] letterCount=new int[26]; int count=0; for(int i=97;i<=122;i++) { letterArray[count]=(char)i; System.out.printf("%d = %s\n",count,letterArray[count]); count++; } } private static void scanFile(String tempFile) { //RandomAccessFile f = new RandomAccessFile ("file.in", "r"); //PrintWriter out = new PrintWriter( // new BufferedWriter( // new FileWriter("file.out"))); //String firstLine = f.readLine(); //out.print(firstLine); //out.close(); } class ScanButton extends JButton implements ActionListener { ScanButton() { super("Scan File"); addActionListener(this); } public void actionPerformed(ActionEvent arg0) { scanFile(filePath.getText()); } } } [/php]
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#15 | |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Quote:
Your biggest obstacle at this point seems to be OOP in Java. Since you're just learning Java, I would highly recommend that you create each class in a separate file. I think this will help you conceptually. In order to invoke a non-static method in a different class, you must first obtain an object of that class (by creating a new object or passing an instance to the constructor). Once you have an object, you simply use the dot operator to call the method: object.method(); |
|
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
It might help if you had a quick rundown of blocks and scopes in Java, Kilo. A block is any piece of code surrounded by brackets {}. A variable can be accessed only within the block in which it was defined. This is known as the variable's scope.
Some examples might help make it clear: // the string 'foo' is in the same block as the println method,
// so everything works okay
{
String foo = "Hello";
System.out.println(foo);
}
// The same applies here. Inner blocks can access the variables decared
// in outer blocks:
{
String foo = "Hello";
{
System.out.println(foo);
}
}{
{
String foo = "Hello";
}
// foo is now out of scope
System.out.println(foo); // will cause an error
}class Foo
{
// class scope
// variables defined here can be accessed by any method in the class
void bar()
{
// method scope
// variables defined here can only be accessed inside the method
if (...)
{
// inner scope
// variables defined here can only be accessed within the
// if statement
}
}
}public class lettercount extends JFrame
{
JTextField filePath;
...
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|