Java Program Help (Loading Images into Grid)
Posted: Fri Nov 26, 2010 7:05 pm
So basically I am making a program that allows the user to select the number of images they want in a the panel (1 to 4) if they enter nothing it exits.
After they select the number of images it asks for the name of the images (I haven't coded that part yet). The problem is that when I execute the program all it does it load up the input dialog for me to enter the number of images, and then lets say I put in "3", it just completes the Process, it never launches the GUI window.
Here is my code so far:
Main GUI Class
ColorPanel Class
Help please.
After they select the number of images it asks for the name of the images (I haven't coded that part yet). The problem is that when I execute the program all it does it load up the input dialog for me to enter the number of images, and then lets say I put in "3", it just completes the Process, it never launches the GUI window.
Here is my code so far:
Main GUI Class
Code: Select all
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class Images{
public static void main(String[] args){
int num = 0;
String inputstr = JOptionPane.showInputDialog("Enter number of panels:", "0");
if (inputstr != null){
if(inputstr == "1") num = 1;
else if(inputstr == "2") num = 2;
else if(inputstr == "3") num = 3;
else if(inputstr == "4") num = 4;
JFrame theGUI = new JFrame();
theGUI.setTitle("Example 6.7");
theGUI.setSize(300, 200);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = theGUI.getContentPane();
pane.setLayout(new GridLayout(num, num));
for (int count = 1; count <= num; count++){
//I haven't changed the ColorPanel class to load in images yet
ColorPanel panel = new ColorPanel(Color.white);
pane.add(panel);
}
theGUI.setVisible(true);
}
}
}
ColorPanel Class
Code: Select all
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorPanel extends JPanel{
public ColorPanel(Color backColor){
setBackground(backColor);
}
}
Help please.