• Post Reply Bookmark Topic Watch Topic
  • New Topic

Container not displaying.

 
ric hill
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ive cut and pastethis and tried to make a calculator display. What have I done wrong?




<CODE>

// GUI to display the cards, will have 7 panels and two cards in each panel.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class cardGUI implements ItemListener {




JPanel cards; //a panel that uses CardLayout
final static String ENGLISHPANEL = "JPanel with English";
final static String SLOVAKPANEL = "JPanel with Slovak";

// set up the cards in a content pane

public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { ENGLISHPANEL, SLOVAKPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);

//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Try"));
card1.add(new JButton("Next"));
card1.add(new JTextField("TextField", 20));

JPanel card2 = new JPanel();
card2.add(new JLabel("English Word"));

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, ENGLISHPANEL);

cards.add(card2, SLOVAKPANEL );

pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);


}



/// when only above code present thiswont complile because an interface is expected, why?


public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}


/// when only above code present thiswont complile because an interface is
// expected, why? No brackets

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowcardGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("Card GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
cardGUI demo = new cardGUI();
demo.addComponentToPane(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowcardGUI();
}
});
}

}


</CODE>
 
Frank Ertl
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/// when only above code present thiswont complile because an interface is expected, why?


In the declaration of your class you declare that your class implements the ItemListener-inteface. This means that the compiler is looking for the method that the ItemListener claims to hold.

The rest works for me. Which kind of error do you get?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!