调用下一个类时会调用上一个类

时间:2016-12-13 23:06:19

标签: java class jbutton

我没有经验,只有14岁的眼泪!

我做了一个小应用程序,这会给我打气。基本上我会在这样的记事本中提出问题和答案:

H
Hydrogen
O
Oxygen
K
Potassium

该程序会对这些单词进行排序,并将其显示在TextArea ScrollPane

H    =    Hydrogen
O    =    Oxygen
K    =    Potassium

在底部theres,JButton("Start Test"), and it would ask you the questions in order. E.g. ``H means? O Means? K Means?,然后它会给你反馈意见。 Wrong! H means HydrogenCorrect!

这是两个班级! GUI只是简单的GUI。 test是负责测试的类

 import javax.swing.*;
 import javax.swing.border.LineBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class GUI extends JFrame
{
        //JFrame components
        public static JButton btn = new JButton("Start Test");
        public static JPanel panelBtn = new JPanel();
        public static JPanel panelTxt = new JPanel();
        public static JTextArea txt = new JTextArea();
        public static JScrollPane  scroll = new JScrollPane(txt);

        static String[] words;

        static String link = "words.txt";

        //constructor
        public GUI()
        {   

            //title
        setTitle("Test");
        //size
        setSize(400,400);

            //layout
         setLayout(new BorderLayout());

        //on close
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //OTHER
        txt.setLineWrap(true);
        txt.setWrapStyleWord(true);
        txt.setEditable(false);
        txt.setBackground(Color.white);
        btn.setEnabled(false);
        //border for panelTxt
       LineBorder b1 = new LineBorder(Color.BLACK);
        LineBorder b2 = new LineBorder(panelBtn.getBackground() ,5);
        txt.setBorder(BorderFactory.createCompoundBorder(b2,b1));
       //actionListnere
        btn.addActionListener(new lst());

        //add
        add(scroll, BorderLayout.CENTER);
        panelBtn.add(btn);
        add(panelBtn, BorderLayout.SOUTH);

        //visible
            setVisible(true);
    }

    //action listener for btn
    private class lst implements ActionListener
    {   
            public void actionPerformed(ActionEvent e)
    {
        new test(words);
                    GUI g = new GUI() ; 
                    g.setVisible(false);    

            }
}

//main
public static void main(String[] args) throws IOException
{
    new GUI();
    final int ARRAY_LEN = getArrayLength();         
    words = makeArray(ARRAY_LEN);
    displayArray(words);

    btn.setEnabled(true);

}


//get the length of the array, using hasNext from Scanner class 
private static int getArrayLength() throws IOException 
{
    File file = new File(link);
    Scanner scanner = new Scanner(file);

    int len = 0;
    while(scanner.hasNext())
    {   
        scanner.nextLine();
        len++;
    }

     final int ARRAY_LEN = len +1;

     return ARRAY_LEN;
}

//declares and initializes String array, with length of ARRAY_LEN
private static String[] makeArray(final int ARRAY_LEN) throws     FileNotFoundException 
{
    String[] words = new String[ARRAY_LEN];

    int value = 0;
    int count = ARRAY_LEN;

    words[value] = null;
    count--;
    value++;

    File file = new File(link);
    Scanner scanner = new Scanner(file);

    do
    {
        words[value] = scanner.nextLine();
        count--;
        value++;
    }while(count != 0);

    return words;
}

//displays the array in text area
private static void displayArray(String[] words)
{

    int len = words.length - 1;

    int i = 0;
    i++;
    txt.setText(words[i]);
    i++;
    txt.setText(txt.getText() + "\t=");
    txt.setText(txt.getText() + "\t" + words[i]);
    do
    {
    i ++;
    txt.setText(txt.getText() + "\n\n" + words[i]);
    i++;
    txt.setText(txt.getText() + "\t=");
    txt.setText(txt.getText() + "\t" + words[i]);


    }while(i != len);

}
}

第2类

   import javax.swing.*;
   import java.util.Random;

public class test extends GUI
{
public static String[] words;

//constructor
public test(String[] word)
{
    words = word;
    main(word);

}
public static void main(String[] args) 
{

    int len = words.length;

    boolean first = true;
    for(int i = 0; i != len; i++)
    {
        if(first == true)   //to skip null
        {
            i++;
            first = false; 
        }

        String question = words[i];
        i++;
        String answer = JOptionPane.showInputDialog(question+ " is?");
        String rightAnswer = words[i];

        if(answer.equals(rightAnswer))
            JOptionPane.showMessageDialog(null, "Correct!");
        else
        {
            JOptionPane.showMessageDialog(null, "Wrong! " + question +" means " + rightAnswer);
        }
    }
}

}

所以这就是问题

每当我按下按钮时,它就会启动测试,但会创建GUI类的新窗口,而setVisible(false)实际上并没有做任何事情。

所以有2个问题; 1 setVisible(false)不起作用。 2在ButtonClikced()创建一个新窗口,因此有2个相同的窗口,关闭一个窗口,关闭另一个窗口。

请帮忙,因为我不知道该怎么做

1 个答案:

答案 0 :(得分:0)

从main方法中删除new GUI ();。 在您的代码中,按钮在操作执行时不可见。但是在这个时候GUI对象也被创建了。创建GUI对象时,将执行main方法。在那里你将看到按钮。 因此,将 btn.setEnabled(true);更改为所需的另一种方法。

请尝试以下代码,

//main
public static void main(String[] args) throws IOException
{
    final int ARRAY_LEN = getArrayLength();         
    words = makeArray(ARRAY_LEN);
    displayArray(words);

    // btn.setEnabled(true);

}