This is my first time taking a java class and I'm understand as far as now of course. This is my requirements for the project:
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display. Add a company logo to the GUI using Java graphics classes.I can create a GUI window with texts, pictures, and buttons, but I don't know how to make the "Next" and "Previous" button work so another frame, GUI window, or whatever it is to display the next or previous list.
This can compile and run. I am suppose to have an array setup for this but I don't quite understand the use of array and handler/listener. Please help me if you can. This is my first time learning to write java and all. I've been using the book, but it's not much help.
CODE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Test
{
public static void main( String args[] )
{
Try a = new Try();
a.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
a.setSize( 220,250 );
a.setLocation( 500,500);
a.setVisible( true );
}
}
class Try extends JFrame
{
private JLabel text1;
private JButton previousB;
private JButton nextB;
// create constructor for Try
public Try()
{
super( "Game Inventory" );
setLayout( new FlowLayout() );
// construct JLabel with text
text1 = new JLabel();
text1.setText( "Game List Number: 1" );
add( text1 ); // add text1 to JFrame
JLabel name = new JLabel( "Name of Game: Zelda" );
add( name );
JLabel unit = new JLabel( "Numbers of Unit: 2" );
add( unit );
JLabel price = new JLabel( "Price of Game: $19.99" );
add( price );
previousB = new JButton( "Previous" );
previousB.setActionCommand( "enable" );
previousB.setEnabled( true );
add( previousB ); // add previousB to JFrame
nextB = new JButton( " Next " );
nextB.setActionCommand( "enable" );
nextB.setEnabled( true );
add( nextB ); // add nextB to JFrame
Handler handle = new Handler();
previousB.addActionListener( handle );
Handler handle2 = new Handler();
nextB.addActionListener( handle2 );
}
private class Handler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
Try aFrame = new Try();
JLabel text2 = new JLabel();
text2.setText( " " );
aFrame.add( text2 );
aFrame.setVisible( true );
aFrame.setSize( 220,250 );
}
}
}