为什么这段代码会在eclipse中编译而不是jGrasp

时间:2015-05-30 11:10:44

标签: java eclipse swing compilation jgrasp

我决定采用一个java类,教程希望所有.java文件都在jGrasp中运行,但我对eclipse更熟悉,所以我一直在使用它。我有两个文件可以在eclipse中编译好但是当我在jGrasp中打开他们的.java文件时,他们要么没有正确执行,要么某些导入语句会导致错误。

这个将编译,但JFrame将为空:

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);
}

}

这段代码根本不会编译。 jGrasp声称jGoodies的import语句无效,但它在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;
}
}

1 个答案:

答案 0 :(得分:0)

在eclipse中创建项目时,它会根据其配置为包和模块创建某些依赖项。 该程序显示一个空的JFrame,因为eclipse中的项目使用了这些依赖项,但是jGrasp找不到它们,因为它不熟悉eclipse的项目结构。