Hello,
The program below has two JPanels. The first JPanel, panel contains the labels, buttons and combobox. The second JPanel, panel_1 has the image.I am using BoxLayout to line up the belows vertically. The issue I am having is when I run the program, the compoments of the panel do not show up and the image in panel_1 shows up fine. Only when I point my mouse to the first panel, I see the combo box and the buttons. The labels do not show up at all. I will be grateful for any suggestions to fix this issue.
Thank you!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
public class Scrolls extends JApplet
{
private JComboBox <String> cbox;
private JPanel panel, panel_1;
private JButton button;
private int j = 0, cell = 0;;
private JLabel label1, label2;
private String options[] = {"A", "B", "C", "D", "E"};
private String answer[] = new String[10];
private BufferedImage image;
private LayoutManager layout;
public void init()
{
getContentPane().setBackground (Color.GRAY);
File file = new File ("image.jpg");
try
{
image = ImageIO.read(file);
}
catch (IOException e)
{
}
layout = new BoxLayout (getContentPane(), BoxLayout.Y_AXIS);
setLayout (layout);
label1 = new JLabel("Answer Options ===>");
label2 = new JLabel("<=== Press this button to turn in your answer");
cbox = new JComboBox <String> (options);
button = new JButton ("Select");
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel_1 = new JPanel();
panel.add(label1);
panel.add(cbox);
panel.add(button);
panel.add(label2);
cbox.addItemListener(
new ItemListener()
{
public void itemStateChanged (ItemEvent e)
{
if (e.getStateChange() == 1)
{
j = cbox.getSelectedIndex();
}
}
}
);
button.addActionListener(
new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
answer[cell] = options[j];
cell++;
JOptionPane.showMessageDialog (null, "Your choice was " + options[j]);
}
}
);
add(panel);
add(panel_1);
}
public void paint (Graphics g)
{
g.drawImage (image, 0,100,image.getWidth(), image.getHeight(), panel_1);
}
}