Hello to all,
I am creating one java applet in which i have to use many JButtons.
Now my program is able to create Jbuttons but it is not able to use ActionListener or ActionCommand....
I am not able to select exactly which part should i send??
so i m sending my full code.
in program the JButtons TE, TEM, TM are working fine. problem is with TE1,TE2,TE3 Jbuttons. while checking the program use distance=100 and frequency = 2.
Thanking you...
Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.border.EmptyBorder; public class wave extends JApplet implements ActionListener { JTextField D1,F1; JButton submit,TE,TM,TEM,buttons[],te1,te2,te3,te4,te5 ; int d1,cnt,temode=0,k; float theta; double f1, inci_ang,fmin,fc,c,freq; //JButton[] buttons= new JButton[temode]; String msg; public void init() { super.init(); JLabel D1p = new JLabel("Distance:",Label.RIGHT); JLabel Fp = new JLabel("Frequency in GHz"); JLabel F1p = new JLabel("Frequency:",Label.RIGHT); D1= new JTextField(); F1= new JTextField(); submit=new JButton("SUBMIT"); TE=new JButton("TEm"); TM=new JButton("TMm"); TEM=new JButton("TEM"); final JPanel customPanel = new JPanel() { Dimension size = new Dimension(1000, 1000); GradientPaint backgradient = new GradientPaint(0, 0, Color.cyan, 0, 800, Color.darkGray); public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(backgradient); g2.fillRect(25, 25, 1000,1000); g2.setPaint(backgradient); g2.setColor(Color.blue); Font font1 = new Font("Arial", Font.BOLD, 16); g2.setFont(font1); int xpoints1[]= {200,500,650,650,500,200,200,350,650,500}; int ypoints1[]= {250,250,150,157,257,257,250,150,150,250}; int num1 = 10; g2.fillPolygon(xpoints1,ypoints1,num1); g2.setColor(Color.darkGray); g2.drawPolygon(xpoints1,ypoints1,num1); DrawSecond_plate(g,1); if(cnt==1){ DrawElectric_field(g,1); } if(cnt==4){ for_te1(g,1); } if(cnt==5){ for_te2(g,1); } if(cnt==6){ for_te3(g,1); } int xpoints3[]= {200,500,650,650,500,200,200}; int ypoints3[]= {258,258,158,150+d1,250+d1,250+d1,258}; int num3 = 7; g2.setColor(Color.lightGray); g2.fillPolygon(xpoints3,ypoints3,num3); } }; JPanel fieldPanel = new JPanel(new GridLayout(0,1,2,2)); JPanel constrainPanel = new JPanel( new BorderLayout() ); JPanel mainGUI = new JPanel(new BorderLayout(3,3)); mainGUI.setBorder(new EmptyBorder(3,3,3,3)); fieldPanel.add(D1p); fieldPanel.add(D1); fieldPanel.add(F1p); fieldPanel.add(F1); fieldPanel.add(Fp); fieldPanel.add(submit); fieldPanel.add(TE); fieldPanel.add(TM); fieldPanel.add(TEM); // compact the fields to the top of the panel constrainPanel.add( fieldPanel, BorderLayout.NORTH ); mainGUI.add(constrainPanel, BorderLayout.WEST); //Register to receive action event submit.addActionListener(this); submit.setActionCommand("submit"); TE.addActionListener(this); TE.setActionCommand("TEm"); TM.addActionListener(this); TM.setActionCommand("TMm"); TEM.addActionListener(this); TEM.setActionCommand("TEM"); mainGUI.add(new JScrollPane(customPanel), BorderLayout.CENTER); setContentPane(mainGUI); validate(); } private void DrawSecond_plate(Graphics g12, int i) { int xpoints2[]= {200,500,650,650,500,200}; int ypoints2[]= {(250+d1),(250+d1),(150+d1),(157+d1),(257+d1),(257+d1)}; int num2 = 6; g12.setColor(Color.blue); g12.fillPolygon(xpoints2,ypoints2,num2); g12.setColor(Color.darkGray); g12.drawPolygon(xpoints2,ypoints2,num2); g12.setColor(Color.blue); g12.drawLine(200, 250+d1, 350, 150+d1); System.out.println("plates are drawn"); } private void DrawElectric_field(Graphics g22, int i){ int m=1; c= 3*Math.pow(10, 8); freq= f1* Math.pow(10,9); do{ fmin = m*c/(2*d1*Math.pow(10,-3)); m++; }while(freq>=fmin); temode = m; fc=fmin; g22.drawString("No.of Modes:" +temode, 800, 200); // Creating JButtons....... if(m==1){ first_te(g22,1); } if(m==2){ first_te(g22,1); second_te(g22,1); } if(m==3){ first_te(g22,1); second_te(g22,1); third_te(g22,1); } } public void first_te(Graphics g22, int i){ JButton te1 = new JButton("TE1"); getContentPane().add(te1); setLayout(null); te1.setBounds(150,600,80,40); te1.setActionCommand("TE1"); te1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ if(e.getActionCommand().equals("TE1")){ System.out.print("first_te"); cnt=4; } }catch(Exception ae){ ae.printStackTrace();} } } ); } public void second_te(Graphics g22, int i){ JButton te2 = new JButton("TE2"); getContentPane().add(te2); setLayout(null); te2.setBounds(250,600,80,40); te2.addActionListener(this); te2.setActionCommand("TE2"); System.out.print("second_te"); } public void third_te(Graphics g22, int i){ JButton te3 = new JButton("TE3"); getContentPane().add(te3); setLayout(null); te3.setBounds(350,600,80,40); te3.addActionListener(this); te3.setActionCommand("TE3"); } public void for_te1(Graphics g22, int i){ g22.setColor(Color.orange); g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/6); g22.setColor(Color.red); g22.fillRect(200,257+(d1/6),(int) (300*(fmin/fc)),4*d1/6); g22.setColor(Color.orange); g22.fillRect(200,257+5*d1/6,(int) (300*(fmin/fc)),d1/6); } public void for_te2(Graphics g22, int i){ g22.setColor(Color.orange); g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/10); g22.setColor(Color.red); g22.fillRect(200,257+(d1/10),(int) (300*(fmin/fc)),3*d1/10); g22.setColor(Color.orange); g22.fillRect(200,257+4*d1/10,(int) (300*(fmin/fc)),d1/10); g22.setColor(Color.cyan); g22.fillRect(200,257+5*d1/10,(int) (300*(fmin/fc)),d1/10); g22.setColor(Color.blue); g22.fillRect(200,257+6*(d1/10),(int) (300*(fmin/fc)),3*d1/10); g22.setColor(Color.cyan); g22.fillRect(200,257+9*d1/10,(int) (300*(fmin/fc)),d1/10); } public void for_te3(Graphics g22, int i){ g22.setColor(Color.orange); g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/15); g22.setColor(Color.red); g22.fillRect(200,257+(d1/15),(int) (300*(fmin/fc)),3*d1/15); g22.setColor(Color.orange); g22.fillRect(200,257+4*d1/15,(int) (300*(fmin/fc)),d1/15); g22.setColor(Color.cyan); g22.fillRect(200,257+5*d1/15,(int) (300*(fmin/fc)),d1/15); g22.setColor(Color.blue); g22.fillRect(200,257+6*(d1/15),(int)(300*(fmin/fc)),3*d1/15); g22.setColor(Color.cyan); g22.fillRect(200,257+9*d1/15,(int) (300*(fmin/fc)),d1/15); g22.setColor(Color.orange); g22.fillRect(200,257+10*d1/15,(int) (300*(fmin/fc)),d1/15); g22.setColor(Color.red); g22.fillRect(200,257+11*(d1/15),(int) (300*(fmin/fc)),3*d1/15); g22.setColor(Color.orange); g22.fillRect(200,257+14*d1/15,(int) (300*(fmin/fc)),d1/15); } public void actionPerformed(ActionEvent ae) { // TODO Auto-generated method stub theta+=0.5; repaint(); try{ if(ae.getActionCommand()!= null){ System.out.println("Event"+ae.getActionCommand()); if(ae.getActionCommand().equals("submit")){ actionPerformed2(); } if(ae.getActionCommand().equals("TEm")){ cnt=1; } if(ae.getActionCommand().equals("TMm")){ cnt=2; } if(ae.getActionCommand().equals("TEM")){ cnt=3; } /* if(ae.getActionCommand().equals("TE1")){ System.out.println("Event"+ae.getActionCommand()); cnt=4; }*/ if(ae.getActionCommand().equals("TE2")){ System.out.println("Event"+ae.getActionCommand()); cnt=5; } if(ae.getActionCommand().equals("TE3")){ System.out.println("Event"+ae.getActionCommand()); cnt=6; } } }catch(Exception e){ e.printStackTrace();} } public void actionPerformed2(){ System.out.println("Into ActionPerformed2"); d1 = Integer.parseInt(D1.getText()); f1 = Double.parseDouble(F1.getText()); } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks