Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: i have a problem this code

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry i have a problem this code

    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 !!!
    Last edited by mihaivalcea123; January 25th, 2020 at 05:46 AM.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: i have a problem this code

    Because you are not using it. You may remove it.
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Jan 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Re: i have a problem this code

    Thanks i do and try again and this code function in this moment.

    A beautiful day.

Similar Threads

  1. what's problem with this code?
    By ashik16 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2012, 11:32 AM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. Problem with my jsp code
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 16th, 2011, 01:12 AM
  4. problem with code
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 08:06 AM
  5. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM