473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem in adding JButtons

Hi All,

I am trying to add buttons and I am not able to see them while running. Please let me know where I am going wrong.

Thanks in advance.

KiranJyothi

<CODE>
//KiranJyothi

import java.awt.GridBagLayout;
import javax.swing.*;
import java.awt.Component;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddButtons extends JFrame implements ActionListener
{
Color bgColor;
public JButton sendPacket, incSpeed, descSpeed, pause, reset, error, kill;

private boolean isError, isKill;

public AddButtons( boolean errorOccur, boolean killed )
{
bgColor = Color.PINK;
JButton sendPacket = new JButton("Transmit Packet");
sendPacket.setBackground(bgColor);
sendPacket.setActionCommand("snp");
sendPacket.addActionListener(this);

JButton kill = new JButton("Kill Packet");
kill.setBackground(bgColor);
kill.setActionCommand("kl");
kill.addActionListener(this);
kill.setEnabled(false);

JButton error = new JButton("Create Error");
error.setBackground(bgColor);
error.setActionCommand("err");
error.addActionListener(this);

JButton pause = new JButton("Pause");
pause.setBackground(bgColor);
pause.setActionCommand("paus");
pause.addActionListener(this);

JButton incSpeed = new JButton("Increase Packet Speed");
incSpeed.setBackground(bgColor);
incSpeed.setActionCommand("fast");
incSpeed.addActionListener(this);

JButton descSpeed = new JButton("Descrease Packet Speed");
descSpeed.setBackground(bgColor);
descSpeed.setActionCommand("slow");
descSpeed.addActionListener(this);


JButton reset = new JButton("Reset Animation");
reset .setBackground(bgColor);
reset.setActionCommand("rst");
reset.addActionListener(this);

add( sendPacket );
add( kill );
add( error );
add( pause );
add( incSpeed );
add( descSpeed );
add( reset );

isError = errorOccur;
isKill = killed;
}

public void setError( boolean errorOccur )
{
isError = errorOccur;
}

public boolean isErrorOccur()
{
return isError;
}

public void setKillPacket( boolean killed )
{
isKill = killed;
}

public boolean ifPacketKilled()
{
return isKill;
}

public void actionPerformed( ActionEvent event )
{//body}
}

//KiranJyothi

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Color;

public class PacketComponent extends JComponent
{
int horizontalMove, verticleMove;

Color senderPacketColor, recieverPacketColor;

AddButtons buttons = new AddButtons( false, false );

public void paintComponent( Graphics g )
{
Graphics2D g2 = (Graphics2D) g;

horizontalMove = 0;
verticleMove = 0;

senderPacketColor = Color. BLUE;
recieverPacketColor = Color.CYAN;

buttons.draw(g2);

g2.drawString("Transmitter ", horizontalMove+10, verticleMove+80);

for( int count = 1; count <=20; count++ )
{
Packet sender = new Packet( false, false, false, false, 0, 0, horizontalMove+80, verticleMove+80, senderPacketColor );
horizontalMove +=40;
sender.draw( g2 );
}

horizontalMove = 0;
verticleMove = 0;

g2.setColor(Color.BLACK);
g2.drawString("Reciever ", horizontalMove+10, verticleMove+500);

for( int count = 1; count <=20; count++ )
{
Packet reciever = new Packet( false, false, false, false, 0, 0, horizontalMove+80, verticleMove+500, recieverPacketColor );
horizontalMove +=40;
reciever.draw( g2 );
}

}
}

//KiranJyothi

import javax.swing.*;

public class PacketTest
{
public static void main( String a[] )
{

JFrame frame = new JFrame();

final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 700;

frame.setSize( FRAME_WIDTH, FRAME_HEIGHT );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

PacketComponent component = new PacketComponent();
frame.add( component );

frame.setVisible( true );
}
}

</CODE>
Aug 22 '08 #1
2 1670
r035198x
13,262 8TB
Read Sun's swing tutorial first. You are doing several things wrong there.

1.) You declare buttons as instance variables in your class, yet in the constructor you declare other buttons with the same variable names. That means your instance buttons are not available in the constructor.
2.) You need to read about and use a Layout manager for your Frame's contentPane. Find out from the tutorial which layout has been automatically used and what the consequences of that are.
3.) Your main method creates a Frame without any buttons on it at all. It should possibly have created an AddButtons instance?

e.t.c
Aug 22 '08 #2
Nepomuk
3,112 Expert 2GB
Hi All,

I am trying to add buttons and I am not able to see them while running. Please let me know where I am going wrong.

Thanks in advance.

KiranJyothi

<CODE>
...
</CODE>
And one little tip concerning this forum: the code tags are
[CODE] ... [/code]
not
<CODE> ... </CODE>
Greetings,
Nepomuk
Aug 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Humpdydum | last post by:
When I run python setup.py bdist --format=zip on Windows 2000, with this setup.py: from distutils.core import setup setup(name="test", version="1.0", description="blabla",
1
by: Anatoly | last post by:
Hi everybody, I am writing a scheduling system that has appointments table. For each appointment I am adding a new record that has EventID and ClientID. Each event has capacity that needs to be...
4
by: smita | last post by:
Hi, I have an xml file as below <root> <table id =1> <user>abc</user> <age>25</age> </table> <table id = 2> <user>xyz</user>
0
by: Rob | last post by:
Hi Visual Studio.NET 2002 v7.0 Windows XP (all service packs up to date) Working on Clearcase MVFS mapped drive. When compiling my ATL control, I always have to hit Rebuild Solution. If I...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
3
by: f1racing24 | last post by:
I am trying to compile this class in the command-line and this error comes up 'Start -> C:\Inetpub\wwwroot\DCReview_Brinkster\code>vbc /t:library /verbose...
2
by: Joe Wedel | last post by:
I have a Solution with 4 Projects, one of which is not loading. I get the error message: Microsoft Development Environment Unable to read the project file 'CouncilAgenda.vbproj'. The project...
9
by: Dave | last post by:
Hi guys, I have just set up a duplicate server running: apache 2.54, mysql 5.04 and php 5.04 This is the same setup as as the server we are using now, apart from the hardware inside. I have...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.