museumofrussuanicons.org    webdesign@gmail.com Fecha  9/12/2012 07:15 
Host: No mostrado/ Not shown    IP: No mostrado/ Not shown    Sistema: Windows NT


Cut Copy Paste Icons - Download 495 Free Cut Copy Paste Icon (Page 2)   Admin: Borrar mensaje Responder Volver al foro
 
By WebOsPublisher

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.*;
JButton problem in Jpanel
zzfocrender("203", "9479", "651177/651156/651133/651132/581034", "31", "0", "0", "", "", "";
CodeGuru Home
VC++ / MFC / C++
.NET / C#
Visual Basic
Newsletters
--VB Forums
Developer.com
eBook Library
--
Register
Help
Remember Me?
span.ciu_wrapper div padding: 10px;
span.ciu_wrapper div padding: 10px;
Forum
Today's Posts
FAQ
Calendar
Forum Actions
Mark Forums Read
Quick Links
View Forum Leaders
What's New?
Advanced Search
Forum
Java Programming
Java Programming
JButton problem in Jpanel
If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
+ Reply to Thread
Results 1 to 2 of 2
Thread: JButton problem in Jpanel
Thread Tools
Show Printable Version
Email this Page…
Subscribe to this Thread…
Display
Linear Mode
Switch to Hybrid Mode
Switch to Threaded Mode
July 19th, 2010 08:52 PM
#1
sbdk
View Profile
View Forum Posts
Junior Member
Join Date Jul 2010
Posts 1
JButton problem in Jpanel
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<>000];
Point p1<> = new Point<>000];
Point p2<> = new Point<>000];
double x1<>=new double<>00];
double y1<>=new double<>00];
int x<>=new int<>00];
int y<>=new int<>00];
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<>000];
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<n>] = new Point(evt.getX(), evt.getY());n1++;
repaint();


else n1 = 0;
if(n2 < 500 )

if (radio2.isSelected())

p2<n>] = 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++;



Read more: JButton Problem
Reply With Quote
July 20th, 2010 06:41 AM
#2
Londbrok
View Profile
View Forum Posts
Member
Join Date Apr 2007
Posts 442
Re: JButton problem in Jpanel
You use actionListener wrongly. When you press the button, no action is performed on the JPanel itself. Hence, use addActionListener(...) on the BUTTONS or JButton(Action action) constructor. Like so...
Code:
JButton clearButton=new JButton(getClearButtonAction());
Then you would have the get methods defined for the respective actions, like so:
Code:
private Action getClearButtonAction()
return new AbstractAction("Clear"
public void actionPerformed(ActionEvent e)
//do what needs to be done



Hence, the Panel does not need to be a MouseListener at all. You can get the hoodlum to work that way too, check which button, if any, is the source of the mouseEvent and act acoording to that knowledge. In that approach you do not need the actionListener. I tend the prefer the first approach.
Do you really need to treat your radioButtons in any fashion? Simply valuate the conditions, when needed, after user has pressed the clearButton. Make sure to group your radiobuttons in a case such as this (when presumably only one option at a time is applicable), so selecting one deselects any previously selected. For that you have ButtonGroup.class.
--06:44 AM.
Reply With Quote
+ Reply to Thread
Quick Navigation
Java Programming
Top
Site Areas
Settings
Private Messages
Subscriptions
Who's Online
Search Forums
Forums Home
Forums
Visual C++ $ C++ Programming
Visual C++ Programming
Visual C++ FAQs
C++ (Non Visual C++ Issues)
Modern Windows Apps (Metro)
C++ and WinAPI
Managed C++ and C++/CLI
Visual C++ Bugs $ Fixes
Graphics Programming
Multithreading
Network Programming
Driver Development
Visual Basic Programming
Visual Basic 6.0 Programming
Visual Basic .NET
Crystal Reports
VBForums
.NET Programming
C-Sharp Programming
ASP.NET
.NET Framework
ADO.NET
Windows Presentation Foundation (WPF) $ XAML forum
Silverlight
.NET Installation and Configuration Issues
Java Programming
Java Programming
Other Programming
AJAX
Scripting - Client Side
Database
XML
Wireless/Mobile Development
Assembly
Scripting - Server Side (PHP, Perl, etc.)
Proving Ground: New Forum Areas
Python
General Discussion
General Developer Topics
Project Planning, Design, and Management
Testers and Testing
Algorithms $ Data Structures
General Discussion / Chit Chat
Announcements, Press Releases, $ News
CodeGuru Community
Feedback
Articles Suggestions
Testing Area
Programming Projects
C# Game(s) Project
Game Engine Project
C++ Coding Project
Project: Code War
Slow Chat Archives
Slow Chat: Talk with Microsoft Developer Teams
Slow Chat: Developing Multithreaded Applications
Slow Chat: C++0x
Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
Windows Central
Directory Services
General Windows and DNA Programming
Windows OS Issues
Jobs
Open Positions (Jobs)
Looking for Work
CodeGuru Technical FAQs
C++ FAQs
STL FAQs
Windows SDK FAQs
Visual C++ FAQs
MFC FAQs
ATL FAQs
.NET Framework (non-language specific) FAQs
C# FAQs
Visual Basic .NET FAQs
Visual Basic FAQs
CodeGuru Individual FAQs
CodeGuru Individual Visual Basic FAQs
«
Previous Thread
|
Next Thread
»
Bookmarks
Bookmarks
Digg
del.icio.us
StumbleUpon
Google
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
<IMG> code is On
HTML code is Off
Forum Rules
-- vB4 Default Style -- Blue Codeguru
Contact Us
CodeGuru Forums
Privacy Statement
Top
Click Here to Expand Forum to Full Width
View RSS feed
#developerBrand-footer
text-align:center;
margin:0 auto;

#footerlinks
font-size:12px;
margin:10px 0 0 0;
word-spacing:2px;

#footerlinks a
color:#000000;
margin:10px 0 0 0;

#itbeBrand-footer
text-align:center;
margin:0 auto;

#footercopyright
font-size:12px;
margin:5px 0 0 0;

#developerBrandForum-footer
text-align:center;
margin:0 auto;

#itbeBrandForum-footer
text-align:center;
margin:0 auto;

.footerLogoClass
text-align:center;
margin:0 auto;

\/SCR" + "IP
                                                                                                                                                                                                                                                                                                                               
 


Respuestas (0)
 
  • » Cut Copy Paste Icons - Download 495 Free Cut Copy Paste Icon (Page 2) « - museumofrussuanicons.org - 9/12/2012 07:15



Volver Responder
 
Nombre:
E-Mail:
Asunto:
Web:
Enlace a una
imagen:
 





Mensaje