LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2006, 11:56 AM   #1
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Rep: Reputation: 0
Smile Need Help In Oragainzing The Java Applets


face difficulty in organizing the design of my program and also I had another difficultly in labeling 3 or 4 sentences each one in a new line


code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;


public class answer extends Applet implements ActionListener
{
Button b1=new Button("red");
Button b2=new Button("pink");
Button b3=new Button("Black");
Button b4=new Button("blue");
Button b5=new Button("brown");


public void init ()
{
Label L1 = new Label(" button programs")
Label L2 = new Label(" This is My Program")

add(b1);
add(b2);
add(b3);
add(b4);
add(b5);


b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);


public void paint (Graphics g)
{
g.setColor(blue);
g.drawOval(30, 50, 200, 200);
g.drawOval(19, 100, 200, 200);
}

}


}

button programs (This should be center)
This is My Program (This should be new line +center)

circles
should be down and center
 
Old 05-01-2006, 12:31 PM   #2
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
to manually place your buttons in the center you will need to do something like

b1.setBounds(x,y,width,height);//x,y is the top left corner of the object

you could also use a LayoutManager.So in your constructor you would need to do

this.setLayout(new LayoutType());

But I am not sure what type of layout manager would be appropriate.
For information look at http://java.sun.com/j2se/1.5.0/docs/api/
and find the LayoutManager interface on the bottom left frame.
 
Old 05-01-2006, 12:43 PM   #3
mema_UAE
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks -- jdwilder


Your code is quite different than my code and I’m afraid to use it because I know that I will make many mistakes. I visit the link which you provided but, honestly I still didn’t know how to use the Grid Layout or Panel to organize the codes. Overall, Thanks a lot for answer.
 
Old 05-01-2006, 12:56 PM   #4
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
The link is the official Sun API of Java, so that is where I always go with all of my questions, unless I cannot figure it out there either.

Here is a sample of a Grid Layout, and it should be well commented. If you don't have time to read it that is fine (I don't want to waste your time if you think this is a different direction than you would like to go), but it might help you understand what is going on with Layouts.So if you have time, check out this code.
Quote:
/*
* myJFrame.java
*
* Created on August 2, 2003, 5:30 AM
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
*
* This is an example class to show you how to use ActionListeners,
* JButtons, two dimensional arrays, and GridLayoutManagers.
*
* @author lziegler
*/
public class MyJFrame extends JFrame implements ActionListener {

/** Creates a new instance of myJFrame.
*
* Notice that we use a constructor where we pass the number of rows
* and columns so we can lay out a grid of whatever size we want.
*
*/
public MyJFrame(int rows, int columns) {
this.rows=rows;
this.columns=columns;

// This AddWindowListener statement makes it so that when you click
// on the x in the upper right corner of the window it will kill the
// program.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);

// We set the size to whatever we want, but we want it to be proportional
// to the size of the grid. Note it gives WIDTH first.
setSize(80*columns,80*rows);

// This statement creates a "Layout Manager" to hold the grid of buttons
// we are going to add. It divides the window into rows by columns equal
// size boxes.
getContentPane().setLayout(new GridLayout(rows,columns));

// Note that near the bottom of the program we declare a two-dimensional
// array of JButtons called buttons. Here we actually create the array.
buttons=new JButton [rows] [columns];

// This loop actually creates the buttons we will use and adds them to
// the JFrame. Every time we do the getContentPane().add statement it
// adds to the next spot in the grid, working across each row and, then,
// going to the next row.
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {
// This statement actually creates buttons[i][j] with a label of
// i,j.
buttons[i][j]=new JButton(""+i+","+j);
// The addActionListener(this) says that when the button is clicked,
// it will call the method called actionPerformed defined below.
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.red);
// This add statement actually adds button[i][j] to the grid.
getContentPane().add(buttons[i][j]);
}
}

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MyJFrame J=new MyJFrame(5,3);
J.show();
}

public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
// Every ActionEvent has associated with it the component that the
// action occured on. In our case, we are interested in the effect
// of left clicks on the buttons. The method getSource() attached to
// the event, returns the object the event occurred on.
Object O=actionEvent.getSource();

//This double loop now checks to see which of the buttons (if any) were
// the source of the action.
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {

// This if checks to see if buttons[i][j] was the button clicked.
// If it is, it changes its color. (You will want to do something
// different with your buttons.
if(O.equals(buttons[i][j])) {
if(buttons[i][j].getBackground().equals(Color.red)) {
buttons[i][j].setBackground(Color.blue);
}
else {
buttons[i][j].setBackground(Color.red);
}
}

}
}
}

private int rows,columns;
private JButton [] [] buttons;
}
 
  


Reply



Posting Rules
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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I use java applets? brooks_45 Linux - Newbie 6 02-01-2006 05:47 PM
slow java applets Saquel Linux - Software 8 10-18-2005 11:08 AM
java applets nOOb1kanobee Linux - Newbie 1 02-25-2004 02:47 AM
Firebird + Java Applets chrisk5527 Linux - Software 2 02-09-2004 04:25 PM
java applets and cache neverender Programming 4 05-27-2003 04:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration