/* -can someone show me what iam doing wrong, iam trying to make the page go bac
ID: 3688622 • Letter: #
Question
/*
-can someone show me what iam doing wrong, iam trying to make the page go back and forth in a GUI, so i want my users to go from category to another panel and from the second panel to the first panel
*/
package GUI;
/*
-A class to make a GUI to allow users to keep track of their daily expense
*/
//importing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//the class expense Tracker
public class ExpenseTracker{
//attributes
private JFrame framecr;
private JPanel panel1;
private JLabel menu;
private JButton menu1;
private JLabel name;
private JTextField name1;
private JButton addortake;
private JButton category;
private JButton showdebt;
private JButton paydebt;
private JPanel panelcont;
private CardLayout cr;
//attributes for the second panel
private JPanel panel2;
private JLabel category1;
private JButton category2;
private JButton misc;
private JButton rent;
private JButton food;
private JButton utilities;
private JButton other;
private JButton goback;
//attributes for thee third panel
//attributes for the fifth panel
//constrctuor
public ExpenseTracker(){
/*
-first panel
-this is what we did in this program
-Creating the attributes
-adding color
-adding it to the panel
-adding image
*/
panel1 = new JPanel(new GridLayout(5,1));
menu = new JLabel("");
menu1 = new JButton("");
menu1.setLayout(new GridLayout(1,1,3,3));
menu1.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project0.png"));
menu.add(menu1);
panel1.add(menu1);
menu1.setBackground(Color.RED);
menu1.setOpaque(true);
menu1.setBorderPainted(false);
addortake = new JButton("");
addortake.setLayout(new GridLayout(2,1,3,3));
addortake.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project.png"));
menu.add(addortake);
panel1.add(addortake);
addortake.setBackground(Color.RED);
addortake.setOpaque(true);
addortake.setBorderPainted(false);
category = new JButton("");
category.setLayout(new GridLayout(3,1,3,3));
category.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project2.png"));
menu.add(category);
category.setBackground(Color.RED);
category.setOpaque(true);
category.setBorderPainted(false);
panel1.add(category);
showdebt = new JButton("");
showdebt.setLayout(new GridLayout(4,1,3,3));
showdebt.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project4.png"));
menu.add(showdebt);
showdebt.setBackground(Color.RED);
showdebt.setOpaque(true);
showdebt.setBorderPainted(false);
panel1.add(showdebt);
paydebt = new JButton("");
paydebt.setLayout(new GridLayout(5,1,3,3));
paydebt.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project3.png"));
paydebt.setBackground(Color.RED);
paydebt.setOpaque(true);
paydebt.setBorderPainted(false);
panel1.add(paydebt);
panelcont.setLayout(cr);
panelcont.add(panel1, "1");
panelcont.add(panel2, "2");
cr.show(panelcont, "1");
//action listener
menu1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==menu1){
JOptionPane.showMessageDialog(null,"GUID: -This is the Shared Expense Tracker -The First thing you want to do is to add the names of each person -The second thing you want to do is to go in category and click on category for more info -After completing category go to 'show debt' and see who needs to pay who -Then go to 'pay debt' to clear the debt of each user");
}
}
});
addortake.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
category.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cr.show(panelcont, "2");
}
});
showdebt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
paydebt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
//panel2
panel2 = new JPanel(new GridLayout(7,1));
category1 = new JLabel("");
category2 = new JButton("");
category2.setLayout(new GridLayout(1,1,3,3));
category2.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/project2.png"));
category1.add(category2);
category2.setBackground(Color.RED);
category2.setOpaque(true);
category2.setBorderPainted(false);
panel2.add(category2);
misc = new JButton("");
misc.setLayout(new GridLayout(2,1,3,3));
misc.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projectm.png"));
category1.add(misc);
misc.setBackground(Color.RED);
misc.setOpaque(true);
misc.setBorderPainted(false);
panel2.add(misc);
rent = new JButton("");
rent.setLayout(new GridLayout(3,1,3,3));
rent.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projectr.png"));
category1.add(rent);
rent.setBackground(Color.RED);
rent.setOpaque(true);
rent.setBorderPainted(false);
panel2.add(rent);
food = new JButton("");
food.setLayout(new GridLayout(4,1,3,3));
food.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projectf.png"));
category1.add(food);
food.setBackground(Color.RED);
food.setOpaque(true);
food.setBorderPainted(false);
panel2.add(food);
utilities = new JButton("");
utilities.setLayout(new GridLayout(5,1,3,3));
utilities.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projectu.png"));
category1.add(utilities);
utilities.setBackground(Color.RED);
utilities.setOpaque(true);
utilities.setBorderPainted(false);
panel2.add(utilities);
other = new JButton("");
other.setLayout(new GridLayout(6,1,3,3));
other.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projecto.png"));
category1.add(other);
other.setBackground(Color.RED);
other.setOpaque(true);
other.setBorderPainted(false);
panel2.add(other);
goback = new JButton("");
goback.setLayout(new GridLayout(7,1,3,3));
goback.setIcon(new ImageIcon("/Users/alihosseinfazeli/Desktop/projectg.png"));
category1.add(goback);
goback.setBackground(Color.RED);
goback.setOpaque(true);
goback.setBorderPainted(false);
panel2.add(goback);
category.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
misc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
rent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
food.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
utilities.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
other.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cr.show(panelcont, "1");
}
});
framecr.add(panelcont);
framecr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
framecr.pack();
framecr.setVisible(true);
}
//main method for both panel
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ExpenseTracker();
}
});
}
Explanation / Answer
Thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
Please see the belwocode
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//the class expense Tracker
public class ExpenseTracker{
//attributes
private JFrame framecr;
private JPanel panel1;
private JLabel menu;
private JButton menu1;
private JLabel name;
private JTextField name1;
private JButton addortake;
private JButton category;
private JButton showdebt;
private JButton paydebt;
private JPanel panelcont;
private CardLayout cr;
//attributes for the second panel
private JPanel panel2;
private JLabel category1;
private JButton category2;
private JButton misc;
private JButton rent;
private JButton food;
private JButton utilities;
private JButton other;
private JButton goback;
//attributes for thee third panel
//attributes for the fifth panel
//constrctuor
public ExpenseTracker(){
/*
-first panel
-this is what we did in this program
-Creating the attributes
-adding color
-adding it to the panel
-adding image
*/
panel1 = new JPanel(new GridLayout(5,1));
menu = new JLabel("");
menu1 = new JButton("");
menu1.setLayout(new GridLayout(1,1,3,3));
menu1.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
menu.add(menu1);
panel1.add(menu1);
menu1.setBackground(Color.RED);
menu1.setOpaque(true);
menu1.setBorderPainted(false);
addortake = new JButton("");
addortake.setLayout(new GridLayout(2,1,3,3));
addortake.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
menu.add(addortake);
panel1.add(addortake);
addortake.setBackground(Color.RED);
addortake.setOpaque(true);
addortake.setBorderPainted(false);
category = new JButton("");
category.setLayout(new GridLayout(3,1,3,3));
category.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
menu.add(category);
category.setBackground(Color.RED);
category.setOpaque(true);
category.setBorderPainted(false);
panel1.add(category);
showdebt = new JButton("");
showdebt.setLayout(new GridLayout(4,1,3,3));
showdebt.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
menu.add(showdebt);
showdebt.setBackground(Color.RED);
showdebt.setOpaque(true);
showdebt.setBorderPainted(false);
panel1.add(showdebt);
paydebt = new JButton("");
paydebt.setLayout(new GridLayout(5,1,3,3));
paydebt.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
paydebt.setBackground(Color.RED);
paydebt.setOpaque(true);
paydebt.setBorderPainted(false);
panel1.add(paydebt);
panelcont.setLayout(cr);
panelcont.add(panel1, "1");
panelcont.add(panel2, "2");
cr.show(panelcont, "1");
//action listener
menu1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==menu1){
JOptionPane.showMessageDialog(null,"GUID: -This is the Shared Expense Tracker -The First thing you want to do is to add the names of each person -The second thing you want to do is to go in category and click on category for more info -After completing category go to 'show debt' and see who needs to pay who -Then go to 'pay debt' to clear the debt of each user");
}
}
});
addortake.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
category.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cr.show(panelcont, "2");
}
});
showdebt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
paydebt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
//panel2
panel2 = new JPanel(new GridLayout(7,1));
category1 = new JLabel("");
category2 = new JButton("");
category2.setLayout(new GridLayout(1,1,3,3));
category2.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(category2);
category2.setBackground(Color.RED);
category2.setOpaque(true);
category2.setBorderPainted(false);
panel2.add(category2);
misc = new JButton("");
misc.setLayout(new GridLayout(2,1,3,3));
misc.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(misc);
misc.setBackground(Color.RED);
misc.setOpaque(true);
misc.setBorderPainted(false);
panel2.add(misc);
rent = new JButton("");
rent.setLayout(new GridLayout(3,1,3,3));
rent.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(rent);
rent.setBackground(Color.RED);
rent.setOpaque(true);
rent.setBorderPainted(false);
panel2.add(rent);
food = new JButton("");
food.setLayout(new GridLayout(4,1,3,3));
food.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(food);
food.setBackground(Color.RED);
food.setOpaque(true);
food.setBorderPainted(false);
panel2.add(food);
utilities = new JButton("");
utilities.setLayout(new GridLayout(5,1,3,3));
utilities.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(utilities);
utilities.setBackground(Color.RED);
utilities.setOpaque(true);
utilities.setBorderPainted(false);
panel2.add(utilities);
other = new JButton("");
other.setLayout(new GridLayout(6,1,3,3));
other.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(other);
other.setBackground(Color.RED);
other.setOpaque(true);
other.setBorderPainted(false);
panel2.add(other);
goback = new JButton("");
goback.setLayout(new GridLayout(7,1,3,3));
goback.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
category1.add(goback);
goback.setBackground(Color.RED);
goback.setOpaque(true);
goback.setBorderPainted(false);
panel2.add(goback);
category.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
misc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
rent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
food.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
utilities.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
other.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
goback.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cr.show(panelcont, "1");
}
});
framecr.add(panelcont);
framecr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
framecr.pack();
framecr.setVisible(true);
}
//main method for both panel
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ExpenseTracker();
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.