从父目录调用java类

时间:2015-06-17 09:36:12

标签: java swing miglayout

我有两个java类

SortsGui.java

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import Sorts.*;
import javax.swing.event.*;
import java.awt.event.*;  


public class SortsGui
{
    JFrame myMainWindow = new JFrame("Sorts");

    JPanel  sortPanel = new JPanel();

    MyMenuBar mbr = new MyMenuBar();

    //first panel components
    JTextField txtField = new JTextField();
    JTextField txtField2 = new JTextField();
    JTextField txtField3 = new JTextField();
    JTextField txtField4 = new JTextField();
    JTextField txtField5 = new JTextField();
    JTextField txtField6 = new JTextField();
    JTextField txtField7 = new JTextField();
    JTextField txtField8 = new JTextField();
    JTextField txtField9 = new JTextField();

    String sortsArray[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"};
    JComboBox sortComboBox = new JComboBox(sortsArray); 
    //end first panel

    public void runGUI()
    {
        myMainWindow.setBounds(10, 10, 800, 800);
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        myMainWindow.getContentPane().add(sortPanel);

        myMainWindow.setJMenuBar(mbr);

        myMainWindow.setVisible(true);
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("" , "[grow]");
        sortPanel.setLayout(layout);

        sortPanel.add(txtField,"growx");
        sortPanel.add(txtField2,"growx");
        sortPanel.add(txtField3,"growx");
        sortPanel.add(txtField4,"growx");
        sortPanel.add(txtField5,"wrap,growx");
        sortPanel.add(txtField6,"growx");
        sortPanel.add(txtField7,"growx");
        sortPanel.add(txtField8,"growx");
        sortPanel.add(txtField9,"growx");

        selectNothing();
    }

    public void selectNothing()
    {
        sortPanel.addAncestorListener(new RequestFocusListener(false));
    }

    public static void main(String[] args)
    {
        SortsGui sG = new SortsGui();
        sG.runGUI();
    }

    class RequestFocusListener implements AncestorListener
    {
        private boolean removeListener;

        public RequestFocusListener()
        {
            this(true);
        }

        public RequestFocusListener(boolean removeListener)
        {
            this.removeListener = removeListener;
        }

        @Override
        public void ancestorAdded(AncestorEvent e)
        {
            JComponent component = e.getComponent();
            component.requestFocusInWindow();

            if (removeListener)
                component.removeAncestorListener( this );
        }

        @Override
        public void ancestorMoved(AncestorEvent e) {}

        @Override
        public void ancestorRemoved(AncestorEvent e) {}
    }
}

MyMenuBar.java

package Sorts;

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

import java.io.*;
import java.lang.*;

public class MyMenuBar extends JMenuBar
{
    JLabel lblSorts = new JLabel("Select amount of numbers to sort");
    String sortsArray[]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"};
    JComboBox sortComboBox = new JComboBox(sortsArray);
    SortsGui sG= new SortsGui();

    public MyMenuBar()
    {
        setBorderPainted(true);
        makePopUpMenu();
    }

    void makePopUpMenu()
    {       
        add(lblSorts);

        sortComboBox.addItemListener(new sortComboBoxChanged());
        add(sortComboBox);
    }

    class sortComboBoxChanged implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent e)
        {
            System.out.println(sortComboBox.getSelectedItem());
            sG.selectNothing();
            System.out.println("Losing Focus");
        }
    }
}

在这里你可以看到我试图将SortsGui调用到MyMenuBar但是这会导致SortsGui无法找到符号错误。那么可以从父目录调用一个类吗?如果有人可以请更正我的错误吗?

家长目录

Parent Directory

对目录进行排序

Sorts Directory

2 个答案:

答案 0 :(得分:2)

您需要为SortsGui创建一个新包,因为它在默认包中并且存在歧义。

e.g:

package abc;
class sortGui {

}

然后你必须在Mymenubar中导入包

答案 1 :(得分:2)

默认包中的类不能由包中的类导入。