Package

Source Code of GUI

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUI extends JFrame {

  JTextField input = new JTextField();
  JDialog j=new JDialog();
  JTextArea txt=new JTextArea();
  JButton get=new JButton("取得數據資料");
  JButton enter = new JButton("輸入");
  JComboBox box;
  Font classFont = new Font("微軟正黑體", Font.BOLD, 15);
  JPanel inputField = new JPanel();
  JPanel photo =new JPanel();
  JPanel chart=new JPanel();
  int num;
  int currentQuestion=0;
  DataCollection dc;
 
 
  GUI() {
    super("問卷選項統計器 by小囧");
    String temp=new String();
    do{
      temp=JOptionPane.showInputDialog("輸入題目數量,欲離開請按取消或xx。");
      try{
        Integer.parseInt(temp);
      }catch(Exception e) {
        if(temp==null){
          System.exit(0);
        }
        else{
          JOptionPane.showMessageDialog(this, "輸入數字聽不懂阿(# °Д°)!!");
          temp="60";
        }
      }
    }while(Integer.parseInt(temp)<=0||Integer.parseInt(temp)>50);
    this.num=Integer.parseInt(temp);
    do{
      temp=JOptionPane.showInputDialog("題目最多幾個選項?");
      try{
        Integer.parseInt(temp);
      }catch(Exception e) {
        if(temp==null){
          System.exit(0);
        }
        else{
          JOptionPane.showMessageDialog(this, "輸入數字聽不懂阿(# °Д°)!!");
          temp="60";
        }
      }
    }while(Integer.parseInt(temp)<=0||Integer.parseInt(temp)>50)
    dc=new DataCollection(this,this.num,Integer.parseInt(temp));
   
    enter.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        dc.addData(input.getText());
        input.setText("");
        chart.removeAll();
        chart.add(dc.showData(currentQuestion));
        chart.updateUI();
      }
    });
    input.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_ENTER){
          dc.addData(input.getText());
          input.setText("");
          chart.removeAll();
          chart.add(dc.showData(currentQuestion));
          chart.updateUI();
        }
      }
    });
    get.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        j.setVisible(true);
        //j.removeAll();
       
        txt.setText(dc.getData());
        txt.setFont(classFont);
       
        j.setVisible(true);
        j.setDefaultCloseOperation(HIDE_ON_CLOSE);
      }
    });
   
    this.setChart();
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(700, 530);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.add(this.photo,BorderLayout.CENTER);
    this.add(this.get,BorderLayout.SOUTH);
   
    j.setLocationRelativeTo(null);
    j.setSize(300, 350);
    j.add(txt);
    input.setSize(180, 20);
    inputField.setLayout(new BorderLayout());
    inputField.add(input, BorderLayout.CENTER);
    inputField.add(enter, BorderLayout.EAST);
    this.add(inputField, BorderLayout.NORTH);
   
    this.input.requestFocus();

   

  }
  public void setChart(){
    String list[]=new String[this.num];
    for(int i=0;i<this.num;i++){
      list[i]=String.valueOf(i+1);
    }
    JButton next=new JButton("下一題");
    JButton prev=new JButton("上一題");
    JButton go=new JButton("GO");
    box=new JComboBox(list);
    JPanel ctrl=new JPanel();
    ctrl.setLayout(new FlowLayout());
    this.photo.setLayout(new BorderLayout());

    chart.setFont(classFont);
    chart.add(dc.showData(this.currentQuestion));
    this.photo.add(chart,BorderLayout.CENTER);
    this.photo.add(ctrl,BorderLayout.NORTH);
    ctrl.add(prev);
    ctrl.add(box);
    ctrl.add(go);
    ctrl.add(next);
   
    go.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          currentQuestion=box.getSelectedIndex();
          //System.out.println(currentQuestion);
          box.setSelectedIndex(currentQuestion);
          chart.removeAll();
          chart.add(dc.showData(currentQuestion));
          chart.updateUI();
      }
    });
    next.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(currentQuestion<num-1){
          currentQuestion++;
          box.setSelectedIndex(currentQuestion);
          //System.out.println(currentQuestion);
          chart.removeAll();
          chart.add(dc.showData(currentQuestion));
          chart.updateUI();
        }
      }
    });
    prev.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(currentQuestion>0){
          currentQuestion--;
          //System.out.println(currentQuestion);
          box.setSelectedIndex(currentQuestion);
          chart.removeAll();
          chart.add(dc.showData(currentQuestion));
          chart.updateUI();
        }
      }
    });
  }

  public static void main(String[] a) {
    GUI g = new GUI();

  }

}
TOP

Related Classes of GUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.