图形用户界面和主要方法需要排序

时间:2018-12-10 19:44:49

标签: java user-interface actionlistener main

我正在尝试对我的GUI进行排序,以便它能够使用其他类中的方法 在JProject中,我也想从主调用它。 我今天刚刚学到的动作听众,所以我也需要在那里做些什么的反馈。

任何想法?

对此有一定的了解将是一个很大的帮助! (我是新手,所以我的代码会发布在说明和实际脚本idk中  为什么)。

package test;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;

import java.awt.Container;
import java.awt.SystemColor;
import java.awt.Color;

import javax.swing.JRadioButton;

import java.awt.Font;
import java.io.File;

import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JButton;

import java.util.ArrayList;
import java.util.Scanner;

 //Main Method
 public class TextUI{
 protected StormTracker var;
 public TextUI(StormTracker var)
    {
      this.var=var;
    }

    public static void main(String[] args)
    {
    ShowGUI gui = new SHOWGUI();
    while(gui.running)
        if(gui.buttonPress == -1)
            continue;

      StormTracker obj = new StormTracker("config.txt");
      WeatherAuthority wa = new WeatherAuthority("stormfile.txt");
      int xcoord=0;
      int ycoord=0;
      TextUI uiobj = new TextUI(obj);
      Scanner scan = new Scanner(System.in);
      while(true){
        System.out.println("---------------------------------------------------------------");
        System.out.println("Welcome to the StormTracker Application.");
        System.out.println("Type the following commands to carry out the associated actions.");
        System.out.println("\n");
        System.out.println("----------------------{Commands Menu}--------------------- 
         -------");
        System.out.println("(1) NEW STORM x y");
        System.out.println("(2) UPGRADE <name of storm>");
        System.out.println("(3) DOWNGRADE <name of storm>");
        System.out.println("(4) TICK");
        System.out.println("(5) EXIT");
        System.out.println("----------------------------------------------------------------");
        String option=scan.nextLine();
        if (option.substring(0,4).equals("TICK")){
              obj.tick();
              continue;
        }
        if (option.substring(0,9).equals("NEW STORM")){
              if(option.length()==13){
                xcoord = Integer.parseInt(option.substring(10,11));
                ycoord = Integer.parseInt(option.substring(12,13));
              }
            else if (option.length()==14){
              if (option.substring(11,12).equals(" ")){
                  xcoord = Integer.parseInt(option.substring(10,11));
                  ycoord = Integer.parseInt(option.substring(12,14)); 
              }
              else{
                  xcoord = Integer.parseInt(option.substring(10,12));
                  ycoord = Integer.parseInt(option.substring(13,14));
              }
            }
            else if (option.length()==15){
                  xcoord = Integer.parseInt(option.substring(10,12));
                  ycoord = Integer.parseInt(option.substring(13,15)); 
            }
            obj.newstorm(xcoord,ycoord);
        }
        if (option.substring(0,7).equals("UPGRADE")){
              String storm_name = option.substring(8);
              obj.upgrade(storm_name);
        }
        if (option.substring(0,9).equals("DOWNGRADE")){
              String storm_name = option.substring(10);
              obj.downgrade(storm_name);
        }
      }
    }

//GUI Class
public ShowGUI extends JPanel {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;


    System.out.println("GUI Successfully Loaded");
    createAndShow();
    setToolTipText("Select Storm");
    setBackground(Color.WHITE);
}

private void createAndShow(){
    JPanel mainFrame = new JPanel("Storm Tracker V1.0");
}
    private void addComponents(Container pane){
    pane.setLayout(null);

    JLabel lblStormTrackerV = DefaultComponentFactory.getInstance().createTitle("                          
    Storm Tracker V1.0");
    lblStormTrackerV.setBackground(SystemColor.activeCaption);
    lblStormTrackerV.setFont(new Font("Calibri", Font.PLAIN, 22));
    lblStormTrackerV.setBounds(162, 11, 430, 41);
    add(lblStormTrackerV);

    JRadioButton rdbtnMap = new JRadioButton("MAP");
    rdbtnMap.setBounds(283, 59, 72, 23);
    add(rdbtnMap);

    JRadioButton rdbtnBulletin = new JRadioButton("Bulletin");
    rdbtnBulletin.setBounds(406, 60, 72, 23);
    add(rdbtnBulletin);

    textField = new JTextField();
    textField.setBounds(61, 103, 72, 20);
    add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(61, 134, 72, 20);
    add(textField_1);
    textField_1.setColumns(10);

    JTextPane txtpnX = new JTextPane();
    txtpnX.setText("X:");
    txtpnX.setBounds(23, 103, 28, 20);
    add(txtpnX);

    JTextPane txtpnY = new JTextPane();
    txtpnY.setText("Y:");
    txtpnY.setBounds(23, 134, 12, 20);
    add(txtpnY);

    JTextPane txtpnSpeed = new JTextPane();
    txtpnSpeed.setText("Speed:");
    txtpnSpeed.setBounds(10, 165, 41, 20);
    add(txtpnSpeed);

    textField_2 = new JTextField();
    textField_2.setBounds(61, 165, 72, 20);
    add(textField_2);
    textField_2.setColumns(10);

    textField_3 = new JTextField();
    textField_3.setBounds(61, 196, 72, 20);
    add(textField_3);
    textField_3.setColumns(10);

    JTextPane txtpnSpeed_1 = new JTextPane();
    txtpnSpeed_1.setText("Speed:");
    txtpnSpeed_1.setBounds(10, 196, 41, 20);
    add(txtpnSpeed_1);

    JTextPane txtpnBearing = new JTextPane();
    txtpnBearing.setText("Bearing:");
    txtpnBearing.setBounds(10, 227, 41, 20);
    add(txtpnBearing);

    textField_4 = new JTextField();
    textField_4.setBounds(61, 227, 72, 20);
    add(textField_4);
    textField_4.setColumns(10);


    File input = new File(new 
    FileReader("C:\\Users\\620086793\\Documents\\test\\src\\test"));
    List<String> strings = new ArrayList<String>();
    try {
      String line = null;
      while (( line = input.readLine()) != null){
        strings.add(line);
      }
    }

    catch (FileNotFoundException e) {
        System.err.println("Error, file " + "C:\\Users\\620086793\\Documents\\test\\src\\test" + " didn't exist.");
    }
    finally {
        input.close();
    }

    String[] lineArray = strings.toArray(new String[]{});

    JComboBox comboBox = new JComboBox(lineArray);
    JComboBox comboBox = new JComboBox();
    comboBox.setToolTipText("Select Storm");
    comboBox.setEditable(true);
    comboBox.setBounds(20, 60, 113, 20);
    add(comboBox);

    JButton btnUpdate = new JButton("UPDATE");
    btnUpdate.setFont(new Font("Calibri", Font.BOLD, 11));
    btnUpdate.setBounds(10, 273, 110, 28);
    add(btnUpdate);

    JButton btnNewButton = new JButton("NEW STORM\r\n");
    btnNewButton.setFont(new Font("Calibri", Font.BOLD, 11));
    btnNewButton.setBounds(10, 312, 110, 28);
    add(btnNewButton);

    addActionListeners();
    pane.add(rdbtnMap);
    pane.add(rdbtnBulletin);
    pane.add(textField);
    pane.add(textField_1);
    pane.add(textField_2);
    pane.add(textField_3);
    pane.add(textField_4);
    pane.add(comboBox);
    pane.add(btnUpdate);
    pane.add(btnNewButton);
    }

//Im not sure how to properly add the action listeners i first learned about them 
today.
private void addActionListeners(){
    rdbtnMap.addActionListener(new ActionListener){
        public void actionPerformed(ActionEvent ){
    }
    }

    rdbtnBulletin.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
        }

    textField.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
        }

    textField_2.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
        }

    textField_3.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
        }

    textField_4.addActionListener(){
        public void actionPerformed(ActionEvent ){
            stormTracker.tick = textField_4;
        }
        }

    comboBox.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
        }

    btnUpdate.addActionListener(){
        public void actionPerformed(ActionEvent ){
            obj.upgrade() = btnUpdate;
            return btnUpdate; 
        }
        }

    btnNewButton.addActionListener(){
        public void actionPerformed(ActionEvent ){
        }
    }
}

0 个答案:

没有答案