使用JFrame和JOptionPane

时间:2016-05-25 04:34:13

标签: java swing joptionpane

我正在创建一个和声仪的模拟,我正在使用抽象类和绘画方法。我为输入窗口制作了两个单独的JFrame和JPanel,每个要求大约8个数字。 (我的主框架和面板包含主UI)。我有一个JOptionPane.showConfirmDialog来延迟读取面板中的变量,但是在面板启动之前会弹出确认对话框。

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.lang.Math;


//@Mark Coats 5/11/16--Jadeja 3
public class Harm extends java.applet.Applet implements ActionListener,MouseListener
{
int numY = 0;
int numX = 0;
int numa2,numf2,nump2,numd2;
int numa1,numf1,nump1,numd1;
int numa4,numf4,nump4,numd4;
int numa3,numf3,nump3,numd3;
// add scanner
Scanner key = new Scanner(System.in);
// add JFrame
JFrame frame = new JFrame("Harmonograph Sim");
//add JPanel
JPanel pnlF = new JPanel();
// add Canvas
Canvas c = new Canvas();
// new JTextField
JTextField uIn = new JTextField(15);

//add Fonts
Font btnF = new Font("Serif",Font.BOLD,12);



//Setup Buttons
JButton setx = new JButton("SET P1");

JButton sety = new JButton("SET P2");

JButton RUN = new JButton("RUN");

JButton CLEAR = new JButton("CLR");

JButton exit = new JButton("EXIT");

//End Setup Buttons



public static void main(String[] args) 
{

    Harm h = new Harm();
    h.initBut();

}

public void initBut()
{
    //setFont
    setx.setFont(btnF);
    sety.setFont(btnF);
    RUN.setFont(btnF);
    CLEAR.setFont(btnF);
    exit.setFont(btnF);

    //set up jframe
    frame.setSize(1000,650);
    frame.setBackground(Color.BLACK);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pnlF);
    frame.pack();

    pnlF.setBackground(Color.BLACK);
    frame.setVisible(true);

    //set up panel layout
    pnlF.setLayout(null);

    //add button locations
    setx.addActionListener(this);
    setx.setBounds(25,25,1,1);
    setx.setBackground(Color.GRAY);
    setx.setSize(75,50);
    pnlF.add(setx);

    sety.addActionListener(this);
    sety.setBounds(25,130,1,1);
    sety.setBackground(Color.GRAY);
    sety.setSize(75,50);
    pnlF.add(sety);


    RUN.addActionListener(this);
    RUN.setBounds(25,210,1,1);
    RUN.setBackground(Color.GREEN);
    RUN.setSize(75,50);
    pnlF.add(RUN);

    CLEAR.addActionListener(this);
    CLEAR.setBounds(25,290,1,1);
    CLEAR.setSize(75,50);
    CLEAR.setBackground(Color.GRAY);
    pnlF.add(CLEAR);

    exit.addActionListener(this);
    exit.setBounds(25,370,1,1);
    exit.setSize(75,50);
    exit.setBackground(Color.RED);
    pnlF.add(exit);

    //other Actions
    uIn.addActionListener(this);




}

public void key (KeyEvent e)
{


}


public void actionPerformed(ActionEvent e)//Oh crap...here I go
{

    Object s = e.getSource();

    if (s == setx)
    {
        JFrame n1 = new JFrame("Set Pendulum 1");
        JPanel setp1 = new JPanel();
        n1.setSize(200,400);
        n1.setVisible(true);
        n1.add(setp1);
        n1.pack();
        repaint();
        setp1.setOpaque(false);
        setp1.setBackground(Color.WHITE);
        setp1.setSize(200,400);
        setp1.setVisible(true);
        JTextField seta1 = new JTextField(5);
        JTextField setf1 = new JTextField(5);
        JTextField setpe1 = new JTextField(5);
        JTextField setd1 = new JTextField(5);
        JTextField seta2 = new JTextField(5);
        JTextField setf2 = new JTextField(5);
        JTextField setpe2 = new JTextField(5);
        JTextField setd2 = new JTextField(5);
        //add textboxes
        setp1.add(new JLabel("AmplitudeX:"));
        setp1.add(seta1);
        setp1.add(Box.createHorizontalStrut(15));//create space
        setp1.add(new JLabel("FrequencyX:"));
        setp1.add(setf1);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("PhaseX:"));
        setp1.add(setpe1);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("DampingX:"));
        setp1.add(setd1);
        setp1.add(new JLabel("AmplitudeY:"));
        setp1.add(seta2);
        setp1.add(Box.createHorizontalStrut(15));//create space
        setp1.add(new JLabel("FrequencyY:"));
        setp1.add(setf2);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("PhaseY:"));
        setp1.add(setpe2);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("DampingY:"));
        setp1.add(setd2);


        int ok = JOptionPane.showConfirmDialog(n1,"Enter Requested Info","Confirm",JOptionPane.OK_CANCEL_OPTION);

        if (ok==JOptionPane.OK_OPTION)
        {

        //setnums

        String strA1;
        String strf1;
        String strp1;
        String strd1;
        strA1 = seta1.getText();
        strf1 = setf1.getText();
        strp1 = setpe1.getText();
        strd1 = setd1.getText();
        numa1 = Integer.parseInt(strA1);
        numf1 = Integer.parseInt(strf1);
        nump1 = Integer.parseInt(strp1);
        numd1 = Integer.parseInt(strd1);
            if (numa1 == 0)
        {
            JOptionPane.showMessageDialog(n1,"ERROR: \n X is missing","ERROR",JOptionPane.ERROR_MESSAGE);
        }
        }
        else 
        {
            System.exit(0);
        }

        /*
        String strX = JOptionPane.showInputDialog("SET X:");
        numX = Integer.parseInt(strX);
        */
        setx.setBackground(Color.GREEN);
        repaint();
    }
    else
    if (s == sety) //___________________________________________________________________________________
    {


        JFrame n2 = new JFrame("Set Pendulum 2");
        JPanel setp2 = new JPanel();
        n2.setSize(200,400);
        n2.setVisible(true);
        n2.add(setp2);
        n2.pack();
        repaint();
        setp2.setOpaque(false);
        setp2.setBackground(Color.WHITE);
        setp2.setSize(200,400);
        setp2.setVisible(true);
        JTextField seta3 = new JTextField(5);
        JTextField setf3 = new JTextField(5);
        JTextField setpe3 = new JTextField(5);
        JTextField setd3 = new JTextField(5);
        JTextField seta4 = new JTextField(5);
        JTextField setf4= new JTextField(5);
        JTextField setpe4 = new JTextField(5);
        JTextField setd4 = new JTextField(5);
        //add textboxes
        setp2.add(new JLabel("AmplitudeX:"));
        setp2.add(seta3);
        setp2.add(Box.createHorizontalStrut(15));//create space
        setp2.add(new JLabel("FrequencyX:"));
        setp2.add(setf3);
        setp2.add(Box.createHorizontalStrut(15));
        setp2.add(new JLabel("PhaseX"));
        setp2.add(setpe3);
        setp2.add(Box.createHorizontalStrut(15));
        setp2.add(new JLabel("DampingX"));
        setp2.add(setd3);
        setp2.add(new JLabel("AmplitudeY:"));
        setp2.add(seta4);
        setp2.add(Box.createHorizontalStrut(15));//create space
        setp2.add(new JLabel("FrequencyY:"));
        setp2.add(setf4);
        setp2.add(Box.createHorizontalStrut(15));
        setp2.add(new JLabel("PhaseY:"));
        setp2.add(setpe4);
        setp2.add(Box.createHorizontalStrut(15));
        setp2.add(new JLabel("DampingY:"));
        setp2.add(setd4);

        int ok = JOptionPane.showConfirmDialog(n2,"Enter Requested Info","Confirm",JOptionPane.OK_CANCEL_OPTION);

        if (ok==JOptionPane.OK_OPTION)
        {

        //setnums

        String strA3;
        String strf3;
        String strp3;
        String strd3;
        String strA4;
        String strf4;
        String strp4;
        String strd4;
        strA3 = seta3.getText();
        strf3 = setf3.getText();
        strp3 = setpe3.getText();
        strd3 = setd3.getText();
        numa3 = Integer.parseInt(strA3);
        numf3 = Integer.parseInt(strf3);
        nump3 = Integer.parseInt(strp3);
        numd3= Integer.parseInt(strd3);
        strA4 = seta4.getText();
        strf4 = setf4.getText();
        strp4 = setpe4.getText();
        strd4 = setd4.getText();
        numa4 = Integer.parseInt(strA4);
        numf4 = Integer.parseInt(strf4);
        nump4 = Integer.parseInt(strp4);
        numd4= Integer.parseInt(strd4);
        }
        else 
        {
            System.exit(0);
        }

        sety.setBackground(Color.GREEN);
        repaint();
    }
    else
    if (s == RUN) //______________________________________________________________________vv
        {
        JFrame drawF = new JFrame("Simulator");
        JPanel simPanel = new JPanel();
        drawF.setVisible(true);
        drawF.setSize(1000,650);
        drawF.add(simPanel);
        drawF.pack();
        simPanel.setLayout(null);
        simPanel.setSize(1000,650);
        simPanel.setVisible(true);
        simPanel.setBackground(Color.BLACK);

        //add drawing surface


        //start drawing
            simPanel.add(new JComponent()
            {
            public void paint(Graphics g)
            {
                g.setColor(Color.RED);
                int xLast,yLast;
                for(int i=0;i < 5000000; i++)  // five million
                {

                    double dX = (Math.E*Math.exp(-numd1*i)*Math.sin(i*numf1+nump1))+(Math.E*Math.exp(-numd2*i)*Math.sin(i*numf2*nump2));
                    double dY = (Math.E*Math.exp(-numd3*i)*Math.sin(i*numf3+nump3))+(Math.E*Math.exp(-numd4*i)*Math.sin(i*numf4*nump4));
                    int drawX = (int)dX;
                    int drawY = (int)dY;
                    if (i==0)
                    {
                        xLast = 0;
                        yLast = 0;
                        g.drawLine(xLast,yLast,drawX,drawY);
                    }
                    else
                    {
                        g.drawLine(xLast,yLast,drawX,drawY);
                    }
                }
            }
        });





        repaint();
    }
    else
    if (s == CLEAR)//__________________________________________________________________________^^
    {
        if (numY == 0)
        {
            JOptionPane.showMessageDialog(null,"X Has Been Cleared.","RESET COMPLETE",JOptionPane.PLAIN_MESSAGE);
            setx.setBackground(Color.GRAY);
            sety.setBackground(Color.GRAY);
        }
        else
        if (numX == 0)
        {
            JOptionPane.showMessageDialog(null,"Y Has Been Cleared.","RESET COMPLETE",JOptionPane.PLAIN_MESSAGE);
            setx.setBackground(Color.GRAY);
            sety.setBackground(Color.GRAY);
        }
        else
        {

        numY = 00;
        numX = 00;
        JOptionPane.showMessageDialog(null,"X and Y Have Been Cleared.","RESET COMPLETE",JOptionPane.PLAIN_MESSAGE);
        setx.setBackground(Color.GRAY);
        sety.setBackground(Color.GRAY);
        }
        repaint();
    }
    else
    if (s == exit)//__________________________________
    {
        System.exit(0);
        repaint();
    }


}

public void mousePressed(MouseEvent e)//________________________________________________________________________________________________________
{
    e.getX();
    e.getY();
}
// ignore
public void mouseReleased(MouseEvent e)
  {

  }

public void mouseClicked(MouseEvent e)
  {

  }

public void mouseEntered(MouseEvent e) 
  {
    int a=e.getX();
    int b=e.getY();
  }


public void mouseExited(MouseEvent e)
  {

  }
  //end ignore

}

这个问题好像来自这里:

Object s = e.getSource();

    if (s == setx)
    {
        JFrame n1 = new JFrame("Set Pendulum 1");
        JPanel setp1 = new JPanel();
        n1.setSize(200,400);
        n1.setVisible(true);
        n1.add(setp1);
        n1.pack();
        repaint();
        setp1.setOpaque(false);
        setp1.setBackground(Color.WHITE);
        setp1.setSize(200,400);
        setp1.setVisible(true);
        JTextField seta1 = new JTextField(5);
        JTextField setf1 = new JTextField(5);
        JTextField setpe1 = new JTextField(5);
        JTextField setd1 = new JTextField(5);
        JTextField seta2 = new JTextField(5);
        JTextField setf2 = new JTextField(5);
        JTextField setpe2 = new JTextField(5);
        JTextField setd2 = new JTextField(5);
        //add textboxes
        setp1.add(new JLabel("AmplitudeX:"));
        setp1.add(seta1);
        setp1.add(Box.createHorizontalStrut(15));//create space
        setp1.add(new JLabel("FrequencyX:"));
        setp1.add(setf1);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("PhaseX:"));
        setp1.add(setpe1);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("DampingX:"));
        setp1.add(setd1);
        setp1.add(new JLabel("AmplitudeY:"));
        setp1.add(seta2);
        setp1.add(Box.createHorizontalStrut(15));//create space
        setp1.add(new JLabel("FrequencyY:"));
        setp1.add(setf2);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("PhaseY:"));
        setp1.add(setpe2);
        setp1.add(Box.createHorizontalStrut(15));
        setp1.add(new JLabel("DampingY:"));
        setp1.add(setd2);


        int ok = JOptionPane.showConfirmDialog(n1,"Enter Requested Info","Confirm",JOptionPane.OK_CANCEL_OPTION);

        if (ok==JOptionPane.OK_OPTION)
        {

        //setnums

        String strA1;
        String strf1;
        String strp1;
        String strd1;
        strA1 = seta1.getText();
        strf1 = setf1.getText();
        strp1 = setpe1.getText();
        strd1 = setd1.getText();
        numa1 = Integer.parseInt(strA1);
        numf1 = Integer.parseInt(strf1);
        nump1 = Integer.parseInt(strp1);
        numd1 = Integer.parseInt(strd1);
            if (numa1 == 0)
        {
            JOptionPane.showMessageDialog(n1,"ERROR: \n X is missing","ERROR",JOptionPane.ERROR_MESSAGE);
        }
        }
        else 
        {
            System.exit(0);
        }

        /*
        String strX = JOptionPane.showInputDialog("SET X:");
        numX = Integer.parseInt(strX);
        */
        setx.setBackground(Color.GREEN);
        repaint();

0 个答案:

没有答案