Problems with GUI, seems simple fix but I can't figure it out
Heres my code:
Code java:
//Thomas Harrald
//IT215 Checkpoint Inventory Program
//Camera Inventory program
import java.util.Arrays;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt;
import java.awt.event.*;
// Stores and then gets info on a Camera
public class CameraProgram5 extends JFrame
{
private JTextArea txt;
private Inventory inv;
private int current = 0;
private JButton next;
CameraProgram5 gui = new CameraProgram5();
gui.pack();
gui.setVisible(true);
public static void main(String args[])
{
super("Camera Shack");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CameraClass[] cameras;
CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
cameras = new CameraClass[ 3 ];
cameras[ 0 ] = nikonCamera;
cameras[ 1 ] = sonyCamera;
cameras[ 2 ] = canonCamera;
inv = new Inventory();
inv.add(nikonCamera);
inv.add(sonyCamera);
inv.add(canonCamera);
// sort
inv.sort();
//output them.
for (int i = 0; i < inv.size(); i++) {
System.out.println(inv.get(i));
System.out.println();
}
System.out.println("Total value: $" + String.format("%.2f",inv.value()));
// gui stuff
JPanel panel = new JPanel();
txt = new JTextArea(15,20);
txt.setEditable(false);//user shouldn't change it
panel.add(txt);
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
JButton first = new JButton("First");
first.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
current = 0;// go to the beginning
showCamera();
}
});
buttonpanel.add(first);
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (current > 0) current--;
else current = inv.size()-1;
showCamera();
}
});
buttonpanel.add(previous);
JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (current < inv.size()-1) current++; //advance to the end
else current = 0;
showCamera();
}
});
buttonpanel.add(next);
JButton last = new JButton("Last");
last.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
current = inv.size()-1;
showCamera();
}
});
buttonpanel.add(last);
buttonpanel.add(new Logo());
panel.add(buttonpanel);
getContentPane().add(panel);
showCamera();
}
public void showCamera()
{
txt.setText("Camera Details:\n");
txt.append(inv.get(current) + "\n");
txt.append("Total value: $" + String.format("%.2f",inv.value()));
}
class Logo extends JPanel {
public Logo() {
super();
setPreferredSize(new Dimension(200,200));
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRoundRect(60,60,110,90,5,5);
g.setColor(Color.black);
g.drawString("Camera Shack", 70, 105);
}
}
}
I get three errors,
identifier expected: gui.pack();
identifier expected: gui.setVisible(true);
Illegal start of expression: gui.setVisible(true);
I have tried rereading the readings, and importing specifically java.awt since that is where pack() is. As I'm writing this, I think the problem might be in the first part of that ( CameraProgram5 gui = new CameraProgram5(); )
Any help would be greatly appreciated!
Re: Problems with GUI, seems simple fix but I can't figure it out
The lines of code:
gui.pack();
gui.setVisible(true);
...those are just floating around inside your class, outside of any method.
You can declare and define variables in that area, but not "run code" (so to speak). Tuck them away neatly within a method where they will be executed.
Re: Problems with GUI, seems simple fix but I can't figure it out
Quote:
Originally Posted by
jps
The lines of code:
gui.pack();
gui.setVisible(true);
...those are just floating around inside your class, outside of any method.
You can declare and define variables in that area, but not "run code" (so to speak). Tuck them away neatly within a method where they will be executed.
Thats what I was afraid of, because when I put it in the main method, i get 35 errors. most of them centering around referencing non static variables; inv, current, showCamera, a few of the get(). I know static methods are methods in which you don't have to declare the class, but how does that apply to my methods?
Re: Problems with GUI, seems simple fix but I can't figure it out
New code: Works perfectly
Code java:
//Thomas Harrald
//IT215 Checkpoint Inventory Program
//Camera Inventory program
import java.util.Arrays;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Stores and then gets info on a Camera
public class CameraProgram5 extends JFrame
{
private JTextArea txt;
private Inventory inv;
private JButton next;
int current = 0;
public static void main(String args[])
{
CameraProgram5 gui = new CameraProgram5();
gui.pack();
gui.setVisible(true);
}
public CameraProgram5()
{
super("Camera Shack");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Camera[] cameras;
Camera nikonCamera= new Camera( 3100, "Nikon", "Electronics", 5, 10.00);
Camera sonyCamera = new Camera( 4500, "Sony", "Electronics", 10, 300.00);
Camera canonCamera= new Camera( 1550, "Canon", "Electronics", 3, 40.00);
cameras = new Camera[ 3 ];
cameras[ 0 ] = nikonCamera;
cameras[ 1 ] = sonyCamera;
cameras[ 2 ] = canonCamera;
inv = new Inventory();
inv.add(nikonCamera);
inv.add(sonyCamera);
inv.add(canonCamera);
// sort
inv.sort();
//output them.
for (int i = 0; i < inv.size(); i++) {
System.out.println(inv.get(i));
System.out.println();
}
System.out.println("Total value: $" + String.format("%.2f",inv.value()));
// gui stuff
JPanel panel = new JPanel();
txt = new JTextArea(15,20);
txt.setEditable(false);//user shouldn't change it
panel.add(txt);
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
JButton first = new JButton("First");
first.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
current = 0;// go to the beginning
showCamera();
}
});
buttonpanel.add(first);
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (current > 0) current--;
else current = inv.size()-1;
showCamera();
}
});
buttonpanel.add(previous);
JButton next = new JButton("Next");
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (current < inv.size()-1) current++; //advance to the end
else current = 0;
showCamera();
}
});
buttonpanel.add(next);
JButton last = new JButton("Last");
last.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
current = inv.size()-1;
showCamera();
}
});
buttonpanel.add(last);
buttonpanel.add(new Logo());
panel.add(buttonpanel);
getContentPane().add(panel);
showCamera();
}
public void showCamera()
{
txt.setText("Camera Details:\n");
txt.append(inv.get(current) + "\n");
txt.append("Total value: $" + String.format("%.2f",inv.value()));
}
class Logo extends JPanel {
public Logo() {
super();
setPreferredSize(new Dimension(200,200));
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRoundRect(60,60,110,90,5,5);
g.setColor(Color.white);
g.drawString("Camera Shack", 70, 105);
}
}
}
InventoryC() was originally saved as Inventory(), I made InventoryC() and changed the declaration in the code to reflect that, but for some reason it kept trying to find Inventory()... So I ended up saving over the old Inventory() with inventoryC().
Also, I didn't construct an object of CameraProgram5 lol... so that made it difficult for all the methods to reference it, giving me all of those non-static variable and method errors.
Re: Problems with GUI, seems simple fix but I can't figure it out
jps you are awesome, thanks for not spoon feeding me and just giving me hints on which direction, it really helps a lot and I feel like I learn quite a bit as a result.
Re: Problems with GUI, seems simple fix but I can't figure it out
You are welcome and thanks for the kind words.