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..
user will be given four choices in button, whether to create node,line,circl e 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...
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main{ public static void main(String[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ Menu frame=new Menu(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class Menu extends JFrame{ public Menu(){ setTitle("Let's Paint (pilihan mode)"); setSize(300,200); JButton titik=new JButton("Titik"); JButton garis=new JButton("Garis"); JButton lingkaran=new JButton("Lingkaran"); JButton ellipse=new JButton("Ellipse"); buttonPanel=new JPanel(); buttonPanel.add(titik); //node buttonPanel.add(garis); //line buttonPanel.add(lingkaran); //circle buttonPanel.add(ellipse); //ellipse add(buttonPanel); Pilih actTitik=new Pilih(1); Pilih actGaris=new Pilih(2); Pilih actLingkaran=new Pilih(3); Pilih actEllipse=new Pilih(4); titik.addActionListener(actTitik); garis.addActionListener(actGaris); lingkaran.addActionListener(actLingkaran); ellipse.addActionListener(actEllipse); } private class Pilih implements ActionListener{ public Pilih(int c){ int idx=c; } public int setIndex(){ return idx; } public void actionPerformed(ActionEvent event){ //here's the problem Titik.run(); } private int idx; } private JPanel buttonPanel; }
thx in advance...
Comment