I am typically the type of person to find my own mistakes as i feel that if it takes me hours to do so i will learn the lesson more thoroughly, so i typically spend time thumbing the tabs of Notepad++ holding the applications i have written, but i am at a total loss with Swing. I am in school trying to learn the language . I usually make sense of the matter in time, but the JavaDocs are so ambiguous, and that lack of specificity leaves me at a loss with swing. In my example here i use an array of objects that as far as the code here is concerned is irrelevant. When i attempt to add button functions to my application it returns with an error stating that it is not an abstract and does not overwrite the default. I have a vague understanding what this means but when i researched deeper into the abstract keyword i found that was not where my problem lied either, and if it were my problems are even larger than i had previously assumed. My assumption is that my problem lies in that i have not specified a subclass of the class that extends Jframe to implement ActionListener and yet further specify mehtods to do so however i have seen applications that work without the need for implementation. This is for a final project. I am not asking for the answer but if anyone can point me toward it I would much appreciate it. While i udnerstand the basics of the language syntax, structure, etc but when implementing code into my own (from APIs) i find that with out seeing what i am working with i do not know exactly how to use it as the API documentation is very vague. Any help would be much appreciated.
Thank you in advance,
0.
|
Newbie Member
|
|
| 16Sep2011,08:31 | #2 |
|
Code:
import java.util.*;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InventoryGUI extends JFrame
{
static int myPrintersIndex = 0;
public static JTextArea display (Printer printers, JTextArea myTextArea)
{
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
myTextArea.setText("");
myTextArea.append(printers.toString());
return myTextArea;
/* class EventHandeling implements ActionListener
{
public void actionPerformed(ActionEvent event);
{
}
} */
}
public static void main (String args [])
{
Printer printer1 = new Printer ("St. No. 1", "Hp Laser Jet 2000", 3, 49.85);
Printer printer2 = new Printer ("St. No. 2", "Hp Ink Jet 85", 5, 29.99);
Supplier supplier1 = new Supplier ("St. No. 4", "Lexmark Ink Jet S2300", 2, 29.49, "Lexmark Direct Buy" );
Supplier supplier2 = new Supplier ("St. No. 5", "Lexmark Laser Jet P4300", 7, 65.99, "Lexmark Direct Buy");
Supplier supplier3 = new Supplier ("St. No. 3", "Hp Scanner Printer Combo 65L", 1, 87.65,"HPMegaDeals.com");
Printer myUnsortedPrinters [] = {supplier1, supplier2, supplier3, printer1, printer2};
myUnsortedPrinters = sortArray(myUnsortedPrinters);
final Printer [] myPrinters = new Printer [5];
myPrinters [0] = myUnsortedPrinters[0];
myPrinters [1] = myUnsortedPrinters[1];
myPrinters [2] = myUnsortedPrinters[2];
myPrinters [3] = myUnsortedPrinters[3];
myPrinters [4] = myUnsortedPrinters[4];
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
/* double totalValue = calculateTotalInventory(myPrinters);
String totalValueString = Double.toString(totalValue); */
final JTextArea textBox = new JTextArea(10,20);
textBox.setText("");
textBox.setEditable(false);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout (1,3));
JButton first = new JButton ("First");
buttons.add(first);
first.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
myPrintersIndex = 0;
display(myPrinters[myPrintersIndex],textBox);
}
});
JButton next = new JButton ("Next");
buttons.add(next);
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (myPrintersIndex > 3)
myPrintersIndex = 0;
else
{
myPrintersIndex ++;
display(myPrinters[myPrintersIndex],textBox);
}
}
});
JButton Pervious = new JButton ("Pervious");
buttons.add(Pervious);
Pervious.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (myPrintersIndex < 1)
myPrintersIndex = 4;
else
{
myPrintersIndex --;
display(myPrinters[myPrintersIndex],textBox);
}
}
});
JButton last = new JButton ("Last");
buttons.add(last);
last.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
myPrintersIndex = 4;
display(myPrinters[myPrintersIndex],textBox);
}
});
JLabel companyLogo = new JLabel (new ImageIcon("DigitalNhilism.jpeg"));
JPanel logoPanel = new JPanel();
logoPanel.add(companyLogo);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(display(myPrinters [myPrintersIndex],textBox));
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(companyLogo, BorderLayout.NORTH);
frame.add(buttons, BorderLayout.SOUTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.setDefaultCloseOPeration(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static Printer[] sortArray (Printer myPrinters[])
{
String names [] = new String [5];
Printer mySortedPrinters [] = new Printer [5];
for (int a = 0; a < 5; a++)
{
String pName = myPrinters[a].getProductName();
names [a] = pName;
}
Arrays.sort(names);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
String printerName = myPrinters[j].getProductName();
String nameName = names[i];
if (nameName.equals(printerName))
{
mySortedPrinters[i] = myPrinters[j];
}
}
}
return mySortedPrinters;
}
public static double calculateTotalInventory (Printer myPrinters [])
{//method to calculate total stock value begins
double totalValue = 0;//variable to hold sum of array values
double stockValues [] = new double [5];//array to hold the combind values for each individual items
for ( int c = 0; c < 5; c++)
{//for loop is iterating through stock and copying the combind value to an array of doubles to facilitate a calculation for grand total of all stock
double stockValue = myPrinters[c].findStockValue();
stockValues [c] = stockValue;
}
for (int d = 0; d < 5; d++)
{//grand total for stock value is being calculated
totalValue += stockValues [d];
}
return totalValue;
}
|
|
Newbie Member
|
|
| 16Sep2011,08:37 | #3 |
|
there is obviously more than one class needed for this code to compile but my problem is in my action listener/actionPerformed areas i hope i have tried to follow all the specifications made in the post about asking questions.
|
