Java Swing Frame Can't Call On Other Methods From Main

Mar 25, 2015

I am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:

import javax.swing.*;
import java.awt.*;
 public class BelishaBeacon {
  public void BeaconFrame() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();

[code]....

View Replies


ADVERTISEMENT

Unable To Call / Test All Of The 5 Sorting Methods At Same Time In Main Class

Dec 19, 2014

For reference I am programming Java in BlueJ. I am fairly new to the language and I am having trouble with sorting.

I am trying to call / test all of the 5 sorting methods (at the same time) in the main class. To be specific, the sorted list has to technically outputted 5 times.

I figured out how to call / test Quicksort:

Sorting.quickSort(friends, 0, friends.length-1);

But the others are not working correctly. Specifically these:

Sorting.mergeSort(friends, 0, friends.length-1);
Sorting.PbubbleSort(friends, 0, friends.length-1);
Sorting.PinsertionSort(friends, 0, friends.length-1);
Sorting.selectionSort(friends, 0, friends.length-1);

For reference, this is the output when it is not sorted:

Smith, John 610-555-7384
Barnes, Sarah215-555-3827
Riley, Mark 733-555-2969
Getz, Laura 663-555-3984
Smith, Larry464-555-3489
Phelps, Frank322-555-2284
Grant, Marsha243-555-2837

This is the output when it is sorted:

Barnes, Sarah215-555-3827
Getz, Laura 663-555-3984
Grant, Marsha243-555-2837
Phelps, Frank322-555-2284
Riley, Mark 733-555-2969
Smith, John 610-555-7384
Smith, Larry464-555-3489

This is the class Sorting, which I should note is all correct:

public class Sorting{
/**
* Swaps to elements in an array. Used by various sorting algorithms.
*
* @param data the array in which the elements are swapped
* @param index1 the index of the first element to be swapped
* @param index2 the index of the second element to be swapped
*/
private static <T extends Comparable<? super T>> void swap(T[] data,
int index1, int index2){
T temp = data[index1];
data[index1] = data[index2];

[Code]...

This is the Main class in which I am supposed to call the sorting methods, SortPhoneList:

public class SortPhoneList{
/**
* Creates an array of Contact objects, sorts them, then prints
* them.
*/
public static void main (String[] args){
Contact[] friends = new Contact[7];
friends[0] = new Contact ("John", "Smith", "610-555-7384");
friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827");

[Code]...

View Replies View Related

Java Game - Breaking Down A Method To Smaller Methods And Where To Call

Aug 6, 2014

I have a question about a method I have. In my game, I had a move method to move my player left and right, and originally this method was huge, as it also took care checking collisions and a few other things. I broke it up a bit and put the collisions in their own methods and call them from again another method... Here is an extract which I hope illustrates my point:

private static final double MOVE_SPEED = 0.2;
private static final double MAX_MOVE_SPEED = 3.5;
private static final double STOP_SPEED = 0.18;
private double xPos;
private double yPos;

[Code] .....

Something I thought might be a good idea is to check the direction collision when im doing the calculations for that direction:

if(moveLeft) {
dx -= MOVE_SPEED;
(dx < -MAX_MOVE_SPEED) {
dx = -MAX_MOVE_SPEED;
}
checkLeft();
}

But then I would also need to check it when I'm slowing down the left movement:

if(dx < 0.0) {
dx += STOP_SPEED;
if(dx > 0.0) {
dx = 0.0;
}
checkLeft();
}

Then I thought instead i can check it after both of these steps:

if(moveLeft || dx < 0.0) {
checkLeft();
}

I guess my question is quite general: How much is acceptable to break up a method? How many chains of method calls is acceptable? Is it ok to call the same method from different nearby places?

View Replies View Related

How To Call Java Methods From Different Java File

Apr 14, 2015

I create 2 files:

CircleCalculationMethod.javaMain.java 

In Main.java, How can i call method in CircleCalculationMethod.java ?

Should I put everything in same folder ??Should i do something like "import CircleCalculationMethod.java"Should i do something like create a package ...

I use Eclipse software

View Replies View Related

Swing/AWT/SWT :: Java Frame Is Showing Blank?

Jul 5, 2014

i have problem with the following two java classes, driver class Reservations and Room class. When i run, it just show blank frame, tell me where i gone wrong.

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class Reservations extends Frame implements ActionListener {
Color lightRed=new Color(255,90,90);
Color lightGreen=new Color(140,215,40);
Rooms room=new Rooms(5,3);

[code]...

View Replies View Related

Swing/AWT/SWT :: Jinternalframe Active Frame Different With Java 7?

Jun 24, 2014

I am in the progress of updating my code and re-testing after switching from Java 6 to Java 7. When I open multiple JInternalFrames in my application under java 6 I am used to closing the top most internal frame and having the frame immediately under it become the next active frame. When I run the same code under Java 7 I see a different behaviour in as much as when I close the last frame I opened, the next one to become active is the first one.

To illustrate this another way, lets say I open 5 internal frames, 1,2,3,4 & 5

In java 6 when I close frame 5, frame 4 becomes the active frame.

In java 7 when I close frame 5, frame 1 becomes the active frame.

View Replies View Related

How To Get All Methods On One World Frame

Oct 17, 2014

I'm pretty new at java and I was wondering on how to get all my methods on one world frame?

The code is this:

public class TurtleTester
{
public static void main (String[] args)
{
World world= new World();
Turtle turtle= new Turtle(world);
turtle.drawRectangle(50,100);
turtle.drawHexagon(100);

[Code]...

When I run the main method, it would give me a bunch of world frames with one method in each one. I'm using BlueJ btw.

View Replies View Related

How To Call A Class Within The Main

Apr 17, 2014

How do I call a class within the main. For example, this is what i have in my code right now. I am trying to call the class BlackJackGame.

package blackjack;
public class BlackJack {
public BlackJack() {
BlackJackGame();
}
}

View Replies View Related

How To Call A Constructor In Main

Jul 3, 2014

I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.

As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.

public class PrepaidCard
{
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
}
public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
}

Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class. Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.

public class PrepaidCardTest
{
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
}

How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?

card2.setCardID(id, token);

View Replies View Related

Cannot Call Child Method From Main

May 7, 2015

I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instinate an object like Assessment a = new test(); and call a method in test.I know the method can be called if I instinate the test t = new test() but that defeats the purpose of inheritance which I'm learning in class.

View Replies View Related

Prepaid Card - How To Call A Constructor In Main

Jul 3, 2014

I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.

As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.

Java Code:

public class PrepaidCard {
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
} public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
} mh_sh_highlight_all('java');

Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class.

Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.

Java Code:

public class PrepaidCardTest {
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
} mh_sh_highlight_all('java');

How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?

Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?

Java Code: card2.setCardID(id, token); mh_sh_highlight_all('java');

View Replies View Related

Add New Field And Call The Display Method In Main

Nov 27, 2014

So I have been given this code, below:

public class Person{ 
/*Complete*/
public String format(){
return String.format( "Name: %s", name );

[Code] ....

And I have been asked to add a new field called name, and call the display method in the main. I need to use an appropriate type and privacy for the name field, and once the code is finished, I should see 'Harry Potter' appear on the command line. However I don't know what it means by field. I've tried googling it and one search result said it is a field parameter, and another search result said it was something completely different, and I don't know which one it is. I've created a parameter 'Private String name;' but it only prints out 'name: null'

This is the code I wrote but it obviously doesn't work?

package Practical8;
 
public class Person{
Private String name;
public String format(){
return String.format( "Name: %s", name );

[Code] ....

View Replies View Related

How To Call String From Main Method In Different Class

Dec 9, 2014

The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below. How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I get the other class to initiate the code below which is loacted in my main class.
 
public static String escapeDN(String name) {
  StringBuilder sb = new StringBuilder();
  // space or # character at the beginning of a string
  if ((name.length() > 0) &&
        ((name.charAt(0) == ' ') ||
             (name.charAt(0) == '#'))) {

[Code] .....

View Replies View Related

Call Variables From Main To Write To External File

Apr 27, 2015

I have this program, I am wondering if it is possible to call files from the main method and sort them into my saveOneRocord method? If so, how would that look?

my orderProcess Class

package stu.paston.finalprogram;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;

[Code] .....

View Replies View Related

Default Values Passed By JVM In Order To Call Main Method

Aug 27, 2014

For the below program what are the default values passed by the JVM in order to call main() method

class program
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}

View Replies View Related

How To Call Non Static Methods From Other Classes

Apr 16, 2015

how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??

example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??

class test{
public static void main(String [] args){
Scanner input = new Scanner(System.in); //create new Scanner object
//for input
int number1;
int number2;

[Code]...

View Replies View Related

Swing/AWT/SWT :: How To Add Setbounds Inside Static Void Main In Java

Dec 31, 2014

I am making an application in java, inside static void main, i want to customize all buttons, text areas and want to put them on desired location inside application. I have tried to use setbounds but can not use it, how can i use it, or is there any other way or layout to make my application components customized layout.

View Replies View Related

JSF :: Call Variables Instead Of Methods From A Xhtml Page?

Jun 2, 2014

If I had this code:

<h:dataTable value="#{customer.customerList}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>

And in the server getCustomerList() accessed to database, how many times getCustomerList() would be called from I request the xhtml page?. I have read this would be called several times because of JSF internals and It would be better to store it in a variable and access this variable.

1. Is this true this would be called several times? why?

2. If the previous statement was true, how to avoid it, I mean not call the method from a service?

View Replies View Related

EJB / EE :: Life Cycle Call Back Methods

Apr 17, 2014

Life Cycle Call back methods(init(), destroy(),...) are not transactional by default and expecting this in coming EJB releases (EJB 3.x / EJB 4.x). I was expecting next EJB release along with Java 8, but it stays at 3.2

View Replies View Related

When To Use Multiple Main Methods

Apr 7, 2014

I am confused when it is proper to use an application with multiple entry points, or I guess an application with multiple interconnected modules. I have a network application (Netty) as well as a web application (spring). I can bundle them together, in effect tightly coupling them together, or I can modularize them to operate interdependently of each other while still working together to make the application whole.

Is there any specific reason for making an application a single entity vs multiple entities? Is it "desired" to have a self contained application (eg. One main method)?

View Replies View Related

Calling Child Methods From Main

May 7, 2015

I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instantiate an object like Assessment a = new test(); and call a method in test.

I know the method can be called if I instantiate the test t = new test() but that defeats the purpose of inheritance...

View Replies View Related

Calling Multiple Methods Into Main Method

Sep 30, 2014

This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.

import java.util.Scanner ;
//main method
public class TitleRemoved {
public static void main(String[] args) {
double airSpeedKm = airSpeedOts () ;
System.out.println("That speed is " + airSpeedKm + " km/hr.") ;

[code].....

I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.

View Replies View Related

Arithmetic Methods Whose Main Method Holds Two Integer Variables

Jan 15, 2015

a. Create an application named ArithmeticMethods whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayNumberPlus10(), displayNumberPlus100(), and displayNumberPlus1000(). Create each method to perform the task its name implies.

b. Modify the ArithmeticMethods class to accept the values of the two integers from a user at the keyboard.

View Replies View Related

No Main Methods / Applets / Or MIDlets Found In File Error Message

Apr 17, 2015

I am assigned to create a program "Simpletron" that the only language understands it is Simpletron Machine Language or SML. I figured out the most but I get the message "No main methods, applets, or MIDlets found in file" when compiling the program. I pasted my program so that you can see.

//A Simpletron computer simulator */
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;

[code]....

View Replies View Related

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

public class locker { 
public static void main(String[] args) {
CombinationLock();

[code]....

View Replies View Related

Swing/AWT/SWT :: Frame And Panels

Nov 30, 2014

I wrote the following code to get a frame,button in south and a red color circle,which i got.In the "actionPerformed" method i wrote "setContentPane(mp2)" thinking it would put another panel on the frame with a blue circle but when i pushed the button,the button got stuck and the circled did not change.

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Fra implements ActionListener
{
MyPanel2 mp2;
JFrame frame;
JButton button;
public static void main(String[] args)

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved