我如何暂停main() [英] How do I pause main()

查看:94
本文介绍了我如何暂停main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写程序,以便当用户单击"Drive To ..." JButton(在我的主类Dispatch中)时,它从用户在另一个类(Cab)中打开一个JFrame(GoToDistination)可以输入所需的信息,然后单击继续",它会立即更新Dispatch JFrame中的mainTextArea.

I'm trying to code my program such that when a user clicks the "Drive To..." JButton (in my main class Dispatch), it opens a JFrame (GoToDistination) from another class (Cab) where the user can input desired information and upon clicking "Continue" it instantly updates mainTextArea in the Dispatch JFrame.

我的问题是,当程序通过主JFrame(调度)时,它只是打开新的JFrame并继续运行而无需等待输入的信息.仅在按下开关JButton两次后才更新(这是由于它切换到Cab#2,然后又切换回Cab#1).

My problem is that as the program goes through the main JFrame (Dispatch), it just opens the new JFrame and continues without waiting for the inputted information. It only updates after hitting the switch JButton twice (this is due to it switching to Cab#2 and then back to Cab#1).

我已经开始研究线程技术了,但是我还是Java的新手,到目前为止我所做的一切都是我自己学到的(这是针对初学者的Java大学课程,我已经超越了这点).对于可能解决该问题的任何帮助将不胜感激.这是相关代码:

I've looked into threading a little but I am still new in Java and everything I've done so far is what I have learned on my own (this is for a beginner's java college course which I have gone way beyond at this point). Any help with possible solutions to this problem will be much appreciated. Here is the relevant code:

Dispatch类:

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

public class Dispatch extends JFrame 
{
protected JTextPane mainTextArea;
private JPanel panel;
private JButton GoTo;
private JButton Switch;
private JButton quit;
private Font font;
private int cabNumber=1;
private Cab cab1=new Cab(); //Taxi #1
private Cab cab2=new Cab(); //Taxi #2

public Dispatch()
{
    super("Chess City Cab Service");                            //Sets the JFrame
    Dimension dim= Toolkit.getDefaultToolkit().getScreenSize();
    int width=400;      
    int height=200;     
    int xWindow=(dim.width-width)/2;
    int yWindow=(dim.height-height)/2;
    setLocation(xWindow, yWindow);
    SimpleAttributeSet bSet = new SimpleAttributeSet();             //For setting JTextPane to Center
    StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER); //Alignment text.

    panel=new JPanel();
    panel.setBackground(Color.YELLOW);
    mainTextArea=new JTextPane();           //create and set JTextPane
    mainTextArea.setEditable(false);
    font=new Font("Serif",Font.PLAIN, 14);
    mainTextArea.setFont(font);
    mainTextArea.setBackground(Color.YELLOW);

    String cabButtonString="";
    menuOutput();
    mainTextArea.revalidate();
    panel.add(mainTextArea);
    StyledDocument doc = mainTextArea.getStyledDocument();
    doc.setParagraphAttributes(0, 104, bSet, false);

    GoTo=new JButton("Drive To...");            //Tells taxi to go to a certain destination
    GoTo.setToolTipText("Click this button to go to a destination");
    panel.add(GoTo);

    if(cabNumber==1)
        cabButtonString="Click this button to switch to cab #2";
    else
        cabButtonString="Click this button to switch to cab #1";

    Switch=new JButton("Switch");           //for switching be/w the two cabs
    Switch.setToolTipText(cabButtonString);
    panel.add(Switch);

    quit=new JButton("Quit");               //quit the program
    quit.setToolTipText("Click this button to quit program");
    panel.add(quit);

    add(panel);                                 //set JFrame size and to visible
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,200);
    setVisible(true);

    HandlerClass handler=new HandlerClass();
    GoTo.addActionListener(handler);
    Switch.addActionListener(handler);
    quit.addActionListener(handler);
}
private class HandlerClass implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();

        if(source==GoTo)            //Goes to the GoToDistinationScreen() in the 
        {                           //CabThreadTester Class where user tells the taxi
            if (cabNumber==1)       //where to go. This is where I need this JFram to pause
            {                       //and allow for other class to complete before moving on.
                cab1.GoToDistinationScreen();
            }
            else
            {
                cab2.GoToDistinationScreen();
            }
            mainTextArea.setText("");       //Clears mainTextArea and then reenters with updated
            mainTextArea.revalidate();      //information.
            menuOutput();
            mainTextArea.revalidate();
        }
        else if(source==Switch)     //Switch between two taxis, currently only way to update taxi location
        {                           //by pressing twice.
            if(cabNumber==1)
            {
                cabNumber=2;
                Switch.setToolTipText("Click this button to switch to cab #1");
                Switch.revalidate();
            }
            else
            {
                cabNumber=1;
                Switch.setToolTipText("Click this button to switch to cab #2");
                Switch.revalidate();
            }
            mainTextArea.setText("");
            mainTextArea.revalidate();
            menuOutput();
            mainTextArea.revalidate();
        }
        else if(source==quit)   //quits the program
        {
            System.exit(0);
        }
    }
}
public void menuOutput()    //Decides what the mainTextArea will show
{
    if(cabNumber==1)
    {
        mainTextArea.setText("     Cab #"+cabNumber+"\nCurrent Location: Intersection of "+cab1.getAlphaStreetName()+" & "+cab1.getNumericStreetName()+"\nPassenger on board");
    }
    else
    {
        mainTextArea.setText("     Cab #"+cabNumber+"\nCurrent Location: Intersection of "+cab2.getAlphaStreetName()+" & "+cab2.getNumericStreetName()+"\nPassenger on board");
    }
}
public static void main(String[] args) 
{   
    new Dispatch();
}
}

Cab类:

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

public class Cab
{
public boolean hasPassenger;
private int xLoc, yLoc,xGoTo, yGoTo;
public String alphaStreet;
public String numericStreet;

public Cab()
{
    hasPassenger=false;
    xLoc=0;
    yLoc=0;
    xGoTo=0;
    yGoTo=0;
    alphaStreet="A Street";
    numericStreet="Zero Street";
    //Sets the initial values
}
public String getAlphaStreetName() //returns alphabetical Street names
{
    return alphaStreet;
}
public String getNumericStreetName() //returns numerical street names
{
    return numericStreet;
}
public void GoToDistinationScreen()     //displays GoToDistination subclass
{
    GoToDistination go=new GoToDistination();
    go.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    go.setSize(250,175);
    go.setVisible(true);
}   
public class GoToDistination extends JFrame
{
    private JComboBox alphaBox;
    private JComboBox numericBox;
    private JTextArea goTextArea;   //1st textArea
    private JTextArea asTextArea;   //2nd textArea
    private JTextArea nsTextArea;   //3rd textArea
    private Font font;
    private String[] alpha={"A", "B", "C", "D", "E",        //both arrays for JComboBox options
            "F", "G", "H", "I", "J","K"};
    private String[] numeric={"Zero","1st","2nd","3rd","4th","5th","6th","7th",
            "8th","9th","10th"};
    private JButton Continue, Cancel;
    private String letter, number;      //Data Stored from ItemEvents
    private JPanel panel;

    public GoToDistination()
    {
        super("Chess City Cab Service");
        Dimension dim= Toolkit.getDefaultToolkit().getScreenSize();
        int width=250;                                              //sets the JFrame
        int height=175;
        int xWindow=(dim.width-width)/2;
        int yWindow=(dim.height-height)/2;
        setLocation(xWindow, yWindow);
        panel=new JPanel();
        panel.setBackground(Color.YELLOW);
        goTextArea=new JTextArea(1,20);     
        goTextArea.setEditable(false);          
        font=new Font("Serif",Font.PLAIN, 14);  
        goTextArea.setFont(font);                   //sets goTextArea
        goTextArea.setBackground(Color.YELLOW);
        goTextArea.append("  Drive to the intersection of: ");
        panel.add(goTextArea);
        alphaBox=new JComboBox(alpha);
        letter=alphaBox.getSelectedItem().toString();   //for if there is no ItemEvent 
        alphaBox.addItemListener(
                new ItemListener()
                {
                    public void itemStateChanged(ItemEvent event)
                    {
                        if(event.getStateChange()==ItemEvent.SELECTED)
                            letter=alphaBox.getSelectedItem().toString();
                    }
                }
        );          
        panel.add(alphaBox);                //sets the asTextArea
        asTextArea=new JTextArea(1,5);
        asTextArea.append(" Street & ");
        asTextArea.setEditable(false);
        asTextArea.setFont(font);
        asTextArea.setBackground(Color.YELLOW);
        panel.add(asTextArea);
        numericBox=new JComboBox(numeric);
        number=numericBox.getSelectedItem().toString();
        numericBox.addItemListener(
                new ItemListener()
                {
                    public void itemStateChanged(ItemEvent event)   //for if there is no ItemEvent
                    {
                        if(event.getStateChange()==ItemEvent.SELECTED)
                            number=numericBox.getSelectedItem().toString();
                    }
                }
        );          
        panel.add(numericBox);
        nsTextArea=new JTextArea(1,4);      //sets the nsTextArea
        nsTextArea.append(" Street");
        nsTextArea.setEditable(false);
        nsTextArea.setFont(font);
        nsTextArea.setBackground(Color.YELLOW);
        panel.add(nsTextArea);

        Continue=new JButton("Continue");
        Continue.setToolTipText("Click this button to enter selected distination");
        panel.add(Continue);

        Cancel=new JButton("Cancel");
        Continue.setToolTipText("Click this button to cancel actions and return to menu");
        panel.add(Cancel);
        add(panel);

        HandlerClass handler=new HandlerClass();
        Continue.addActionListener(handler);
        Cancel.addActionListener(handler);
    }
    private class HandlerClass implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Object source = event.getSource();
            if(source==Continue)
            {
                int hashletter=letter.hashCode();
                int hashnumber=number.hashCode();
                xGoTo=aSwitch(hashletter);
                yGoTo=nSwitch(hashnumber);
                alphaStreet=aStreetName(xGoTo);                 //sets Street names for the location of taxi
                numericStreet=nStreetName(yGoTo);
                dispose();
            }
            else if(source==Cancel)
            {
                dispose();
            }
        }
    }
    private String aStreetName(int xGoTo)
    {
        switch(xGoTo)
        {
        case 0:return "A Street";
        case 1:return "B Street";
        case 2:return "C Street";
        case 3:return "D Street";
        case 4:return "E Street";
        case 5:return "F Street";
        case 6:return "G Street";
        case 7:return "H Street";
        case 8:return "I Street";
        case 9:return "J Street";
        }
        return "K Street";
    }
    private String nStreetName(int yGoTo)
    {
        switch(yGoTo)
        {
        case 0:return "Zero Street";
        case 1:return "1st Street";
        case 2:return "2nd Street";
        case 3:return "3rd Street";
        case 4:return "4th Street";
        case 5:return "5th Street";
        case 6:return "6th Street";
        case 7:return "7th Street";
        case 8:return "8th Street";
        case 9:return "9th Street";
        }
        return "10th Street";
    }
    private int aSwitch(int hashletter)
    {
        switch(hashletter)
        {
        case 65:return 0;
        case 66:return 1;
        case 67:return 2;
        case 68:return 3;
        case 69:return 4;
        case 70:return 5;
        case 71:return 6;
        case 72:return 7;
        case 73:return 8;
        case 74:return 9;
        }
        return 10;
        /* hash table for alpha string array
        A: 65
        B: 66
        C: 67
        D: 68
        E: 69
        F: 70
        G: 71
        H: 72
        I: 73
        J: 74
        K: 75
         */
    }
    private int nSwitch(int hashnumber)
    {
        switch(hashnumber)
        {
        case 2781896:return 0;
        case 50770:return 1;
        case 51560:return 2;
        case 52645:return 3;
        case 53672:return 4;
        case 54633:return 5;
        case 55594:return 6;
        case 56555:return 7;
        case 57516:return 8;
        case 58477:return 9;
        }
        return 10;
        /*  hash table for numeric string array
            Zero: 2781896
            1st: 50770
            2nd: 51560
            3rd: 52645
            4th: 53672
            5th: 54633
            6th: 55594
            7th: 56555
            8th: 57516
            9th: 58477
            10th: 1509587
         */
    }

}
}

推荐答案

除了带来不便之外,我不确定Dispatch给聚会带来了什么.相反,我将重点放在单个Cab的状态上,并根据需要重复多次.在下面的示例中,Cab具有可调整的颜色和里程表. Timer会定期增加里程表的值.每个独立的Cab都在JTabbedPane中占据一个单独的标签.

I'm not sure what Dispatch brings to the party, except inconvenience. Instead, I'd focus on the state of a single Cab and replicate that as many times as needed. In the example below, a Cab has an adjustable color and an odometer. A Timer increments the odometer value periodically. Each independent Cab occupies a separate tab in a JTabbedPane.

附录:回到问题我如何暂停main()",答案是不要".而是使用单独的线程,例如由Timer管理的那一个,以推进模型状态并更新显示.

Addendum: Returning to the question, "How do I pause main()," the answer is "don't." Rather, use a separate thread, e.g. the one managed by Timer, to advance the state of your model and update the display.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.Timer;

/** @see http://stackoverflow.com/questions/5617027 */
public class Cab extends JPanel {

    private static final Random random = new Random();
    private static final String format = "00000000";
    private static final DecimalFormat df = new DecimalFormat(format);
    private Hue hue = Hue.Yellow;
    private Timer timer;
    private JLabel odometer = new JLabel(df.format(0));
    private int km;

    public Cab() {
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(hue.getColor());
        this.add(odometer);
        final JComboBox colorBox = new JComboBox();
        for (Hue h : Hue.values()) {
            colorBox.addItem(h);
        }
        colorBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Hue h = (Hue) colorBox.getSelectedItem();
                Cab.this.setBackground(h.getColor());
            }
        });
        this.add(colorBox);
        timer = new Timer(250, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                km += random.nextInt(100);
                odometer.setText(df.format(km));
            }
        });
        timer.start();
    }

    private enum Hue {

        Yellow(Color.yellow), Cyan(Color.cyan), Magenta(Color.magenta);
        private final Color color;

        private Hue(Color color) {
            this.color = color;
        }

        public Color getColor() {
            return color;
        }
    }

    private static void display() {
        JFrame f = new JFrame("Dispatch");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("Cab #1", new Cab());
        tabbedPane.add("Cab #2", new Cab());
        tabbedPane.add("Cab #3", new Cab());
        f.add(tabbedPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

这篇关于我如何暂停main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆