The Artima Developer Community
Sponsored Link

Java Answers Forum
Linking two classes

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jimbob

Posts: 3
Nickname: kingpin
Registered: Nov, 2002

Linking two classes Posted: Nov 28, 2002 12:40 PM
Reply to this message Reply
Advertisement
I have two classes....Interface and Coffee. The InterFace is basically an options screen where the user chooses a coffee they want from a ComboBox....once the choice is made they click a JButton and the value is stored in the object String coffeeType.
The second class is where the coffee styles and amount are set.

I want to know how I can take the value in coffeeType into the other class Coffee and put it into its' appropriate instance so I can then deduct the amounts from a container class?




//Import the relevent classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//define the class and set the properties for the Interface
public class InterFace extends JFrame implements ActionListener{

private JComboBox coffeeStyle;
private String names[] = {"Extra Strong", "Black Coffee", "White Coffee"};
private JLabel label;
private JButton selectButton;
private JTextArea textArea;

public InterFace () //set up GUI
{
super( "Coffee Machine" );

Container container = getContentPane();
container.setLayout(new FlowLayout());

coffeeStyle = new JComboBox (names);
coffeeStyle.setMaximumRowCount( 3 );

textArea = new JTextArea ( 5, 15 );
container.add ( textArea );

label = new JLabel( "Choose a Drink" );
container.add( label );

container.add( coffeeStyle);

selectButton = new JButton( "Get Drink?" );
container.add(selectButton);
selectButton.addActionListener(this);

setSize( 400, 200);
setVisible( true );
}

public void actionPerformed(ActionEvent e) {
Object anObject = e.getSource();
if (anObject == selectButton) {
String coffeeType =
(String)coffeeStyle.getSelectedItem();
textArea.setText ("Getting your " + coffeeType);

}
}

public static void main( String args[])
{
InterFace application = new InterFace();

application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );

}
}

public class Coffee{

int coffee;
int milk;
int water;
Coffee(int coffeeRequired, int milkRequired, int waterRequired)
{
coffee=coffeeRequired;
milk=milkRequired;
water=waterRequired;
}
}

public defineCoffeeAmount
{
//amount of coffee, milk and water required for this instance of Coffee
Coffee darkCoffee = new Coffee(30,0,100);
Coffee whiteCoffee = new Coffee(20,40,100);
Coffee extraStrongCoffee = new Coffee(40,20,100);

Topic: testing a delimeter Previous Topic   Next Topic Topic: Java .Net Interoperability

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use