简体   繁体   中英

Why will this code compile in eclipse but not jGrasp

I decided to take a java class and the prof wants all the .java files to run in jGrasp but I am much more familiar with eclipse so I've been using that. I have two files who will compile fine in eclipse but when I open their .java files in jGrasp they either don't execute correctly or some of the import statements cause errors.

This one will compile but the JFrame will be empty:

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class JDisappearingFriends {
static int i = 1;

static void increase_i() {
    ++i;
}

public static void main(String[] args) {
    JFrame frame1 = new JFrame();
    frame1.setVisible(true);
    frame1.setSize(303, 286);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel[] friends = new JLabel[6];
    friends[0] = new JLabel(
            "Hello. To begin changing friends press the button");
    friends[1] = new JLabel("Laura");
    friends[2] = new JLabel("Kendra");
    friends[3] = new JLabel("Nicole");
    friends[4] = new JLabel("Melissa");
    friends[5] = new JLabel("Elizabeth");

    frame1.add(friends[0]);

    JButton btnChangeFriends = new JButton("Change Friends");
    btnChangeFriends.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (i < 6) {
                frame1.getContentPane().add(friends[i]);

                frame1.remove(friends[i - 1]);
                frame1.revalidate();
                frame1.repaint();
                increase_i();
            } else {
                JLabel done = new JLabel("That's it, your out of friends");
                frame1.remove(friends[5]);
                frame1.add(done);
                frame1.revalidate();
                frame1.repaint();
            }
        }
    });

    frame1.getContentPane().add(btnChangeFriends, BorderLayout.SOUTH);
}

}

This code won't compile at all. jGrasp claims the import statement for jGoodies is invalid but it works fine in eclipse.

 import java.awt.BorderLayout;
 import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


public class JPhotoFrame extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JPhotoFrame frame = new JPhotoFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public JPhotoFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 505, 274);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.WEST);
    panel.setLayout(new FormLayout(new ColumnSpec[] {
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("46px"),
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("97px"),
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("97px"),
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("97px"),},
        new RowSpec[] {
            FormFactory.LINE_GAP_ROWSPEC,
            RowSpec.decode("23px"),
            FormFactory.RELATED_GAP_ROWSPEC,
            FormFactory.DEFAULT_ROWSPEC,}));

    JLabel lblNewLabel_2 = new JLabel("Please select a number of people. Pets Optional");
    panel.add(lblNewLabel_2, "2, 2, 7, 1, fill, center");

    JCheckBox single = new JCheckBox("Single ");
    panel.add(single, "4, 4, left, top");

    JCheckBox more_people = new JCheckBox("2+ People");
    panel.add(more_people, "6, 4, left, top");

    JCheckBox pets = new JCheckBox("Pet");
    panel.add(pets, "8, 4, left, top");

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1, BorderLayout.NORTH);
    panel_1.setLayout(new FormLayout(new ColumnSpec[] {
            ColumnSpec.decode("87px"),
            ColumnSpec.decode("46px"),
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("97px"),
            FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
            ColumnSpec.decode("97px"),},
        new RowSpec[] {
            FormFactory.LINE_GAP_ROWSPEC,
            RowSpec.decode("23px"),}));

    JLabel lblNewLabel_3 = new JLabel("Please select a location");
    panel_1.add(lblNewLabel_3, "1, 2, 3, 1, left, center");



    JCheckBox studio = new JCheckBox("Studio");


    panel_1.add(studio, "4, 2, left, top");
    JCheckBox other = new JCheckBox("Other");
    panel_1.add(other, "6, 2, left, top");

    studio.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if((e.getStateChange() == ItemEvent.SELECTED)){
                  other.setEnabled(false);
            }
            }
    });
    other.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if((e.getStateChange() == ItemEvent.SELECTED)){
                  studio.setEnabled(false);
            }
            }
    });
    single.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if((e.getStateChange() == ItemEvent.SELECTED)){
                  more_people.setEnabled(false);
            }
            }
    });
    more_people.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if((e.getStateChange() == ItemEvent.SELECTED)){
                  single.setEnabled(false);
            }
            }
    });
    JLabel lblNewLabel = new JLabel("Press Button to calculate total cost");
    contentPane.add(lblNewLabel, BorderLayout.SOUTH);

    JButton btnCalculateTotal = new JButton("Calculate Total");
    btnCalculateTotal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int total = 0;
            int other_price=0;
            int single_price=0;
            int more_people_price=0;
            int pets_price=0;
            if(studio.isSelected() == false && other.isSelected()== false){
                lblNewLabel.setText("Please enter a least one location value");
            }
            else if(single.isSelected() == false && more_people.isSelected() == false){
                lblNewLabel.setText("Please enter at least one subject value");
            }
            else{
            if(single.isSelected() == true) single_price = 40;
            if(other.isSelected() ==true ) other_price = 90;
            if(more_people.isSelected() == true) more_people_price = 75;
            if(pets.isSelected() == true) pets_price = 95;

             total = calculate(single_price,other_price, more_people_price, pets_price);
            lblNewLabel.setText("Your Total is: $" + total);
            }
            lblNewLabel.revalidate();
            lblNewLabel.repaint();
            studio.setEnabled(true);
            other.setEnabled(true);
            single.setEnabled(true);
            more_people.setEnabled(true);
            studio.setSelected(false);
            other.setSelected(false);
            single.setSelected(false);
            more_people.setSelected(false);
            pets.setSelected(false);
        }
    });
    contentPane.add(btnCalculateTotal, BorderLayout.EAST);
}
 int calculate(int single, int other, int more_people, int pets){
    int total = single + other + more_people + pets;
    return total;
}
}

When you create a project in eclipse it creates certain dependencies to packages and modules based on its configuration. The program is showing an empty JFrame because the project in eclipse uses those dependancies but jGrasp can't find them because it is not familiar with project structure of eclipse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM