Hi ragonz, I am no expert, but will try to answer your question, along with a few suggestions. The suggestions are my honest opinion only.
Hi, i'm kind of newbie in java programming. Now, i'm working on my task, n i hav a problem in using variable from other method. Here goes the code..
-
-
private class Pilih implements ActionListener{
-
public Pilih(int c){
-
// int is redundant, it creates a local variable,
-
int idx=c; // Class variable defined below is not changed
-
}
-
public void actionPerformed(ActionEvent event){
-
//here's the problem
-
Titik.run(); // no run() method defined in class
-
}
-
private int idx; // variable never changed
-
}
-
user will be given four choices in button, whether to create node,line,circle or ellipse. what i want is that when the user click the node button, it goes to Titik.java and so does the other. but i don't know what to do. i wonder how to use (idx varable) from the constructor so that i can use switch condition in the actionPerformed method.
thx in advance...
Suggestions:
1. Do not use function/method names like
run() , it has special meaning in threads.
2. Try to design the class in an object oriented fashion - do not declare arbitrary inner classes if not necessary
3. Do not use standard awt class names like
Menu, or methods like
paint(), it will create a lot of confusion.
From your problem description, I created the following class - feel free to modify, discard, or continue from here. The inner class, you see, is not really required here - you could implement the ActionListener interface in the top level, getActionCommand() gives you where it is coming from. Hope this helps.
Btw, Welcome to the world of Java programming.
-
import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
public class PaintMenu extends JFrame {
-
-
protected Pilih pilih[];
-
-
public static void main (String[] args) {
-
PaintMenu paintMenu = new PaintMenu();
-
paintMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
-
paintMenu.pack();
-
paintMenu.setVisible(true);
-
}
-
-
public PaintMenu () {
-
super ("Let's Paint (pilihan mode)");
-
// setSize(300,200); Let the Layout manager do this
-
addButtonsToContainer (getContentPane());
-
}
-
-
private void addButtonsToContainer (Container cp) {
-
JPanel bp = new JPanel();
-
int n = buttNames.length;
-
pilih = new Pilih[n];
-
cp.setLayout (new GridLayout(1,n));
-
for (int i = 0 ; i < buttNames.length ; i++) {
-
JButton b = new JButton(buttNames[i]);
-
pilih[i] = new Pilih (i+1);
-
b.addActionListener (pilih[i]);
-
cp.add (b);
-
}}
-
-
protected final static String[] buttNames = {
-
"Titik", "Garis", "Lingkaran", "Ellipse"};
-
-
private class Pilih implements ActionListener {
-
private int idx;
-
-
public Pilih (int c) {
-
idx = c;
-
}
-
-
public void actionPerformed(ActionEvent event) {
-
pilihAction(event.getActionCommand());
-
}
-
-
public void pilihAction (String cmd) {
-
JOptionPane.showMessageDialog (PaintMenu.this,
-
"Running Pilih, internal variable idx = "+ idx +
-
"\nresponding to the command " + cmd,
-
"Using inner class variable",
-
JOptionPane.INFORMATION_MESSAGE);
-
// Do whatever else ...
-
}}}
-