2016-12-13 22 views
0

我没有经历过,只有14岁的老泪!上一课在打电话给下一班时被称为

我做了一个小应用程序,这将测验我。基本上,我会投入的问题和答案在这样的记事本:

H 
Hydrogen 
O 
Oxygen 
K 
Potassium 

该计划将像这样↓排序的话,并在TextAreaScrollPane

H = Hydrogen 
O = Oxygen 
K = Potassium 

在底部显示它们theres,a JButton("Start Test"), and it would ask you the questions in order. E.g. ``H means?O Means?K Means?然后它会给你反馈Eg 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个问题; 不起作用。 2在ButtonClikced()创建一个新窗口,所以有2个相同的窗口,关闭一个,关闭另一个窗口。

请帮助,因为我不知道该怎么办

+0

你的代码看起来像你可能把谚语本末倒置。 –

+0

@HovercraftFullOfEels ?? – Eddie

+1

这里有一段免费的建议,“永远不会有静态字段给可变对象”。 –

回答

0

从主要方法取出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); 

} 
相关问题