Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

JButton Problem

jbutton setpreferredsize

  • Please log in to reply
6 replies to this topic

#1 sbdk

sbdk

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 19 July 2010 - 04:34 PM

I am new to Java and just wrote a simple program to use button in JApplet. But I can use the mouseAction but Jbutton is not working. I am posting the source code. Pls check and tell me where the error is


package Project;
import java.math.*;
import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class testjapplet extends JApplet implements ActionListener
{
    JButton clearButton;
    JButton RandomPoints;
    JRadioButton radio1;
    JRadioButton radio2;
    ButtonGroup radioGroup; 
    MyPanel panel = new MyPanel();
    int i=0;;
    String output;
    int N,n1=0;
    Point p[] = new Point[1000];
    Point p1[] = new Point[1000];
    Point p2[] = new Point[1000];
    double x1[]=new double[100];
    double y1[]=new double[100];
    int x[]=new int[100];
    int y[]=new int[100];
    int n = 0,n2=0;
      
 
    public void init()
    {
        
        clearButton = new JButton("Clear");
        RandomPoints = new JButton("RGP");
        radioGroup = new ButtonGroup();
        radio1 = new JRadioButton("Red",false); 
        radio2 = new JRadioButton("Blue",false);
        radioGroup.add(radio1);
        radioGroup.add(radio2);
       
        Container c = getContentPane();
        c.setPreferredSize(new Dimension(800,600));
        c.add(panel);
        
        panel.setSize(500, 500);
        panel.add(RandomPoints);
        panel.add(clearButton);
        panel.add(radio1); 
        panel.add(radio2); 
        
        RandomPoints.addActionListener(this);
        clearButton.addActionListener(this);
        
        i = 1;
    }
       
    class MyPanel extends JPanel implements MouseListener
    {


        int x3,y3;
        Point p[] = new Point[1000];
        int n= 0;
        String s="sdhgksagksjhk ";
        int i=0;
        
         MyPanel() {addMouseListener(this);}
        
         public void mousePressed(MouseEvent evt)
         {
            p[n] = new Point(evt.getX(), evt.getY());n++; 
            if(n1 <  500 )
            {
              if (radio1.isSelected())
              {
                 p1[n1] = new Point(evt.getX(), evt.getY());n1++; 
                 repaint();
              }
            }
            else n1 = 0;
            if(n2 <  500 )
            {
              if (radio2.isSelected())
              {
                p2[n2] = new Point(evt.getX(), evt.getY());n2++; 
                repaint();
               }
             }
              else  n2=0;
              repaint();
             
            } 
         
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        //public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}

        
        @Override public void paintComponent(Graphics g) {
             super.paintComponent(g); 
             g.fillRect(1,1,400,500);
         g.fillOval(x3,y3,4,4);
         for(int k=0;k<n;k++)
         {
             g.setColor(Color.GREEN);
             g.fillOval(p[k].x,p[k].y,4,4);
         }
         for(int k=0;k<n1;k++)
         {
            g.setColor(Color.RED);
            g.fillOval(p1[k].x,p1[k].y,4,4);
         }
         for(int k=0;k<n2;k++)
         {
            g.setColor(Color.BLUE);
            g.fillOval(p2[k].x,p2[k].y,4,4);
         }
        
         g.setColor(Color.GREEN);
            
        }

        

    }
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == clearButton)
      {
        if ((i%2)!=0) output = "I is odd";
        else output = "I is even"; 
         i++;
      } 
        
    }
 
}

Edited by ZekeDragon, 25 July 2010 - 11:36 AM.
Please use [code] tags when posting code.

  • 0

#2 seanbp

seanbp

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 19 July 2010 - 10:31 PM

Message removed.
  • 0

#3 seanbp

seanbp

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 19 July 2010 - 10:41 PM

Message removed.
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 19 July 2010 - 10:42 PM

They are working with me.. if you click 1 you go into
public void actionPerformed(ActionEvent e)
	{
	  if (e.getSource() == clearButton)
	  {
		if ((i%2)!=0) output = "I is odd";
		else output = "I is even"; 
		 i++;
	  }

There just isn't happening much there.
  • 0

#5 sbdk

sbdk

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 20 July 2010 - 07:18 AM

Thanks friend for ur reply. I hv already resolved the error. I missed repaint() after i++.
  • 0

#6 sbdk

sbdk

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 21 July 2010 - 04:28 PM

Can anyone tell me how to resize the content pane. I dont want to use the entire monitor screen.
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 22 July 2010 - 01:52 AM

"this.setsize(width , height)" in the init( ) perhpas.
  • 0





Also tagged with one or more of these keywords: jbutton, setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download