import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MTPSwingExample extends JFrame {
JPanel mainPanel;
JList list;
JScrollPane pane;
JButton button;
private String label[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven"};
public MTPSwingExample(){
buildContentPane();
}
private void buildContentPane( ){
//panoul principal
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
// componente
list = new JList(label);
pane = new JScrollPane(list);
button = new JButton("Print");
button.addActionListener(new PrintListener());
mainPanel.add(pane, BorderLayout.CENTER);
mainPanel.add(button, BorderLayout.SOUTH);
this.setContentPane(mainPanel);
this.setTitle("Exemplu SWING pentru MTP");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.setSize(300, 200);
}
// An inner class to respond to clicks on the Print button
class PrintListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int selected[] = list.getSelectedIndices();
System.out.println("Selected Elements: ");
for (int i = 0; i < selected.length; i++) {
String element = (String)
list.getModel().getElementAt(selected[i]);
System.out.println(" " + element);
}
}
}
}
public class newGui {
public static void main(String[] args) {
// TODO Auto-generated method stub
MTPSwingExample mtp = new MTPSwingExample();
mtp.setVisible(true);
}
}
when running the program tells me that
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The public type MTPSwingExample must be defined in its own file
at MTPSwingExample.<init>(TutorialClass.java:10)
at newGui.main(newGui.java:6)
Help me !!!