473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in adding JButtons

45 New Member
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.GridBa gLayout;
import javax.swing.*;
import java.awt.Compon ent;
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("Transm it Packet");
sendPacket.setB ackground(bgCol or);
sendPacket.setA ctionCommand("s np");
sendPacket.addA ctionListener(t his);

JButton kill = new JButton("Kill Packet");
kill.setBackgro und(bgColor);
kill.setActionC ommand("kl");
kill.addActionL istener(this);
kill.setEnabled (false);

JButton error = new JButton("Create Error");
error.setBackgr ound(bgColor);
error.setAction Command("err");
error.addAction Listener(this);

JButton pause = new JButton("Pause" );
pause.setBackgr ound(bgColor);
pause.setAction Command("paus") ;
pause.addAction Listener(this);

JButton incSpeed = new JButton("Increa se Packet Speed");
incSpeed.setBac kground(bgColor );
incSpeed.setAct ionCommand("fas t");
incSpeed.addAct ionListener(thi s);

JButton descSpeed = new JButton("Descre ase Packet Speed");
descSpeed.setBa ckground(bgColo r);
descSpeed.setAc tionCommand("sl ow");
descSpeed.addAc tionListener(th is);


JButton reset = new JButton("Reset Animation");
reset .setBackground( bgColor);
reset.setAction Command("rst");
reset.addAction Listener(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.Graphi cs;
import java.awt.Graphi cs2D;
import javax.swing.JCo mponent;
import java.awt.Color;

public class PacketComponent extends JComponent
{
int horizontalMove, verticleMove;

Color senderPacketCol or, recieverPacketC olor;

AddButtons buttons = new AddButtons( false, false );

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

horizontalMove = 0;
verticleMove = 0;

senderPacketCol or = Color. BLUE;
recieverPacketC olor = 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 , senderPacketCol or );
horizontalMove +=40;
sender.draw( g2 );
}

horizontalMove = 0;
verticleMove = 0;

g2.setColor(Col or.BLACK);
g2.drawString(" Reciever ", horizontalMove+ 10, verticleMove+50 0);

for( int count = 1; count <=20; count++ )
{
Packet reciever = new Packet( false, false, false, false, 0, 0, horizontalMove+ 80, verticleMove+50 0, recieverPacketC olor );
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.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );

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

frame.setVisibl e( true );
}
}

</CODE>
Aug 22 '08 #1
2 1683
r035198x
13,262 MVP
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 Recognized Expert Specialist
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
1231
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
4600
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 checked before adding new appointments, if it is reached no new appointments could be added. This logic is implemented as a stored procedure. Problem that I am trying to solve is concurrent scheduling -- when two or more clients are trying to...
4
2519
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
2289
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 just build, I get a "ExportText fatal error LNK1000: Internal error during ProcessResFiles" error. Setting verbose output on the linker gives the attached output. I have two resource files, One is project specfic and the
3
2339
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 page. So whenever the Admin person comes to know about the new category in the market he will be adding as different Sub-Categories for example ABAP, BDC etc..etc.. on every click event as Checkboxes. And these controls(checkboxes) should remain...
3
4583
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 /r:System.dll,System.Data.dll,DBUtilities.dll CountryLib.vb Microsoft (R) Visual Basic .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.00.3705 Copyright (C) Microsoft Corporation 1987-2001. All rights reserved.
2
414
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 file '' has been renamed or is no longer in the solution. I removed the project from the solution, then tried to add it back in, but I get the message above. The .vbproj file
9
1689
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 copied across the database and website, with exact same permissions as the first server. The problem is that part of the php code is executing but others
2
2273
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
1
3187
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.