图形用户界面,项/动作侦听器无法正常工作

时间:2016-02-10 16:34:32

标签: java swing user-interface action

我特别遇到扩展下拉框的问题,它在更改图形输出后似乎没有更新,并且应用了“显示”操作按钮。我不确定这个下拉框是否需要动作或项目监听器。 任何帮助非常感谢。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; //Import event handler classes
import java.awt.geom.*; // Geom packages

public class Listric_gui extends JFrame
    implements ActionListener, ItemListener {

    JPanel pane = new JPanel();
    JComboBox Extension;
    MyCanvas can1;
//Class Variables 
    static float[] x = new float[201];
    static float[] y = new float[201];
    static float[] y2 = new float[201];
    static float[] y3 = new float[201];
    static float[] y4 = new float[201];
    static float[] y5 = new float[201];
    static float[] hwd = new float[201];
//static float Dip = 0f;
    static int end = 200;
    static int i, e1;

    static int x_ending = 160, y_ending = 35, ticksize = 5;
    static float x_orig = 50f, y_orig = 50f, x_scale = 4f, y_scale = 5f, hwd_orig = 50f, hwd_scale = 5f;

    Listric_gui() {
        try {
            UIManager.setLookAndFeel(
                //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Can't set look and feel: " + e);
        }

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        pane.setLayout(gridbag);
        setBackground(Color.lightGray);

        //Title
        buildConstraints(constraints, 0, 0, 5, 1, 0, 10);
        constraints.fill = GridBagConstraints.BOTH;
        Font f = new Font("Helvetica", Font.BOLD, 22);
        JLabel label1 = new JLabel("Fault Modelling Program", JLabel.CENTER);
        label1.setFont(f);
        //label1.setBackground(Color.white);
        label1.setBackground(Color.red);
        gridbag.setConstraints(label1, constraints);
        pane.add(label1);

    //Initialise Button objects
        //File
        buildConstraints(constraints, 0, 1, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.BOTH;
        Font f1 = new Font("Helvetica", Font.BOLD, 14);
        JButton file = new JButton("File");
        gridbag.setConstraints(file, constraints);
        file.setFont(f1);
        pane.add(file);

        //Input
        buildConstraints(constraints, 1, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        //constraints.ipadx = 10;
        JButton input = new JButton("Input");
        gridbag.setConstraints(input, constraints);
        input.setFont(f1);
        pane.add(input);

        //Run
        buildConstraints(constraints, 2, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton run = new JButton("Run");
        gridbag.setConstraints(run, constraints);
        run.setFont(f1);
        pane.add(run);

        //Display Button
        buildConstraints(constraints, 3, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton display = new JButton("Display");
        gridbag.setConstraints(display, constraints);
        display.setFont(f1);
        pane.add(display);

        //Quit button
        buildConstraints(constraints, 4, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton quit = new JButton("Quit");
        gridbag.setConstraints(quit, constraints);
        quit.setFont(f1);
        pane.add(quit);

    //Fault Geometry label 
        buildConstraints(constraints, 0, 2, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.SOUTH;
        JLabel label2 = new JLabel("Fault Geometry", JLabel.CENTER);
        label2.setFont(f1);
        gridbag.setConstraints(label2, constraints);
        pane.add(label2);

    //Geometry - Listric
        buildConstraints(constraints, 0, 3, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;
        JCheckBox C1 = new JCheckBox("Listric");
        gridbag.setConstraints(C1, constraints);
        pane.add(C1);

        //Checkbox group define so that only one selected at time
        ButtonGroup bg = new ButtonGroup();
        bg.add(C1);

        //Extension Label
        buildConstraints(constraints, 0, 4, 1, 1, 1, 10);
        constraints.anchor = GridBagConstraints.SOUTH;
        JLabel label3 = new JLabel("Extension", JLabel.CENTER);
        label3.setFont(f1);
        gridbag.setConstraints(label3, constraints);
        pane.add(label3);

        //Extension Choice
        buildConstraints(constraints, 0, 5, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;
        JComboBox Extension = new JComboBox();
        Extension.addItem("-5 km");
        Extension.addItem("-2 km");
        Extension.addItem("0 km");
        Extension.addItem("5 km");
        Extension.addItem("10 km");
        Extension.addItem("15 km");
        Extension.addItem("20 km");
        Extension.addItem("25 km");
        Extension.addItem("30 km");
        gridbag.setConstraints(Extension, constraints);
        pane.add(Extension);

        //Num. of faults Label
        buildConstraints(constraints, 0, 6, 1, 1, 1, 10);
        constraints.anchor = GridBagConstraints.SOUTH;
        JLabel label4 = new JLabel("Number of Faults", JLabel.CENTER);
        label4.setFont(f1);
        gridbag.setConstraints(label4, constraints);
        pane.add(label4);

        //Num. of faults Choice
        buildConstraints(constraints, 0, 7, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;
        JComboBox FaultNum = new JComboBox();
        FaultNum.addItem("1");
        FaultNum.addItem("2");
        FaultNum.addItem("3");
        FaultNum.addItem("4");
        FaultNum.addItem("5");
        gridbag.setConstraints(FaultNum, constraints);
        pane.add(FaultNum);

        //Draw Canvas
        buildConstraints(constraints, 1, 2, 4, 5, 0, 0);
        constraints.fill = GridBagConstraints.BOTH;
        MyCanvas can2 = new MyCanvas(); // paint method in seperate class
        gridbag.setConstraints(can2, constraints);
        can2.setBackground(Color.white);
        pane.add(can2);

        //Add all event listeners to components
        display.addActionListener(this);
        quit.addActionListener(this);
        Extension.addActionListener(this);
        FaultNum.addItemListener(this);
        setContentPane(pane);
    }
//Define main block

    public static void main(String args[]) {
        JFrame frame = new Listric_gui();
        //Method to exit
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        frame.addWindowListener(l);
        frame.setSize(1600, 600);
    //frame.pack();
        //frame.revalidate();
        frame.repaint();
        frame.setVisible(true);
    }

    static void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;
    }

    public Insets getInsets() {
        return new Insets(10, 10, 10, 10);
    }

//declare actionlistener method
    public void actionPerformed(ActionEvent ae) {
        String label = ae.getActionCommand();
        if (label == "Display") {
            Modelling m1 = new Modelling();
            can1 = new MyCanvas();
            pane.add(can1);
            setContentPane(pane);
        } else if (label == "Quit") {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent evt) {
        if (evt.getSource() == Extension) {
            String s = (String) evt.getItem();
            if (s == "-5 km") {
                e1 = (int) -5f;
            }
            if (s == "-2 km") {
                e1 = (int) -2f;
            }
            if (s == "0 km") {
                e1 = (int) 0f;
            } else if (s == "5 km") {
                e1 = (int) 5f;
            } else if (s == "10 km") {
                e1 = (int) 10f;
            } else if (s == "15 km") {
                e1 = (int) 15f;
            } else if (s == "20 km") {
                e1 = (int) 20f;
            } else if (s == "25 km") {
                e1 = (int) 25f;
            } else if (s == "30 km") {
                e1 = (int) 30f;
            }
        }
    }

//End of controlling class
//Class for modelling fault
    class Modelling {

        Modelling() {
            calc();
        }

        private void calc() {
            float Sum1, Sum2, Sum3, Sum4; //Initialise variables
            float A = 30f;
            float B = 50f;
            float C = -1f;
            float Zd = 20f;
//float x0 = 60f;
            float D = 1f;
            float E = 70f;
            float F = 90f;
            float G = 110f;
            int k, nflt;
            //Loop for calculating fault 1 coordinates
            for (k = 0; k <= Listric_gui.x_ending; k++) {
                Listric_gui.x[k] = (float) k;
                Sum1 = (Listric_gui.x[k] - A);
                Sum2 = ((Sum1 / Zd) * C);
                Sum3 = D - ((float) Math.exp(Sum2));
                Sum4 = (A * Sum3);
                Listric_gui.y[k] = Sum4;
                if (Listric_gui.x[k] <= 30) {
                    Listric_gui.y[k] = 0;
                }

                //Fault 2
                Listric_gui.y2[k] = (A * (D - ((float) Math.exp((((Listric_gui.x[k] - B) / Zd) * C)))));
                if (Listric_gui.x[k] <= 50) {
                    Listric_gui.y2[k] = 0;
                }
                //Fault 3
                Listric_gui.y3[k] = (A * (D - ((float) Math.exp((((Listric_gui.x[k] - E) / Zd) * C)))));
                if (Listric_gui.x[k] <= 70) {
                    Listric_gui.y3[k] = 0;
                }
                //Fault 4
                Listric_gui.y4[k] = (A * (D - ((float) Math.exp((((Listric_gui.x[k] - F) / Zd) * C)))));
                if (Listric_gui.x[k] <= 90) {
                    Listric_gui.y4[k] = 0;
                }
                //Fault 5
                Listric_gui.y5[k] = (A * (D - ((float) Math.exp((((Listric_gui.x[k] - G) / Zd) * C)))));
                if (Listric_gui.x[k] <= 110) {
                    Listric_gui.y5[k] = 0;
                }

                System.out.println("x = " + Listric_gui.x[k] + " y = " + Listric_gui.y[k] + "y2 = " + Listric_gui.y2[k] + "y3 = " + Listric_gui.y3[k] + "y4 = " + Listric_gui.y4[k] + "y5 = " + Listric_gui.y5[k]);
            }
        //Loop for calculating hangingwall deformation

        //Initialise Variables
            Listric_gui.hwd[k] = (float) k;
            Listric_gui.e1 = 30;
            for (k = 0; k <= Listric_gui.x_ending; k++) {
                Listric_gui.hwd[k] = 0f;
                if (Listric_gui.x[k] > 30) {
                    Listric_gui.hwd[k] = Listric_gui.y[k] - Listric_gui.y[k - Listric_gui.e1];
                }
            }
            //Fault modification
            for (k = 0; k <= Listric_gui.x_ending; k++) {
                //for (nflt=2; nflt<=2;nflt++){
                Listric_gui.y2[k] = Listric_gui.y2[k] + Listric_gui.hwd[k];
                Listric_gui.y3[k] = Listric_gui.y3[k] + Listric_gui.hwd[k];
                Listric_gui.y4[k] = Listric_gui.y4[k] + Listric_gui.hwd[k];
                Listric_gui.y5[k] = Listric_gui.y5[k] + Listric_gui.hwd[k];
                //}
            }

//System.out.println("x = " + x[k] + " hwd = "+ hwd [k]);
        }
    }

//Listener Method
//draw Class
    class MyCanvas extends JPanel {

        int k, i;

        public void paintComponent(Graphics screen) {
            super.paintComponent(screen);
            Graphics2D screen2D = (Graphics2D) screen;

            screen2D.setColor(Color.red);
            GeneralPath ln = new GeneralPath();
            ln.moveTo(Listric_gui.x_orig, Listric_gui.y_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y[k] * Listric_gui.y_scale)));
                //ln.closePath();
                screen2D.draw(ln);
            }
            GeneralPath ln2 = new GeneralPath();
            ln2.moveTo(Listric_gui.x_orig, Listric_gui.hwd_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln2.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.hwd[k] * Listric_gui.hwd_scale)));
                //ln2.closePath();
                screen2D.draw(ln2);
            }
            //Plot fault profile graphically
            GeneralPath ln3 = new GeneralPath();
            ln3.moveTo(Listric_gui.x_orig, Listric_gui.hwd_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln3.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y2[k] * Listric_gui.hwd_scale)));
                //ln3.closePath();
                screen2D.draw(ln3);
            }

            GeneralPath ln4 = new GeneralPath();
            ln4.moveTo(Listric_gui.x_orig, Listric_gui.hwd_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln4.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y3[k] * Listric_gui.hwd_scale)));
                //ln4.closePath();
                screen2D.draw(ln4);
            }

            GeneralPath ln5 = new GeneralPath();
            ln5.moveTo(Listric_gui.x_orig, Listric_gui.hwd_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln5.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y4[k] * Listric_gui.hwd_scale)));
                //ln5.closePath();
                screen2D.draw(ln5);
            }

            GeneralPath ln6 = new GeneralPath();
            ln6.moveTo(Listric_gui.x_orig, Listric_gui.hwd_orig);
            for (k = 0; k < Listric_gui.x_ending; k++) {
                ln6.lineTo((Listric_gui.x[k] * Listric_gui.x_scale) + Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y5[k] * Listric_gui.hwd_scale)));
                //ln6.closePath();
                screen2D.draw(ln6);
            }

//x axis
            BasicStroke xpen = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
            screen2D.setStroke(xpen);
            screen2D.setFont(new Font("Serif", Font.PLAIN, 11));
            screen2D.setColor(Color.black);
            GeneralPath xaxis = new GeneralPath();
            xaxis.moveTo(Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y_ending * Listric_gui.y_scale)));
            xaxis.lineTo((Listric_gui.x_orig + (Listric_gui.x_ending * Listric_gui.x_scale)), (Listric_gui.y_orig + (Listric_gui.y_ending * Listric_gui.y_scale)));
            for (i = 0; i <= (Listric_gui.x_ending); i = i + 20) {
                Line2D.Float ticks = new Line2D.Float(((i * Listric_gui.x_scale) + Listric_gui.x_orig), ((Listric_gui.y_orig) + (Listric_gui.y_ending * Listric_gui.y_scale)), ((i * Listric_gui.x_scale) + Listric_gui.x_orig), ((Listric_gui.y_orig + Listric_gui.ticksize) + (Listric_gui.y_ending * Listric_gui.y_scale)));
                screen2D.draw(ticks);
                screen2D.drawString("" + i, ((i * Listric_gui.x_scale) + Listric_gui.x_orig), ((Listric_gui.y_orig) + (Listric_gui.y_ending * Listric_gui.y_scale)) + 20);
            }

//y axis
            BasicStroke ypen = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
            screen2D.setStroke(ypen);
            screen2D.setFont(new Font("Serif", Font.PLAIN, 11));
            screen2D.setColor(Color.black);
            GeneralPath yaxis = new GeneralPath();
            yaxis.moveTo(Listric_gui.x_orig, Listric_gui.y_orig);
            yaxis.lineTo(Listric_gui.x_orig, (Listric_gui.y_orig + (Listric_gui.y_ending * Listric_gui.y_scale)));
            screen2D.draw(yaxis);
            for (i = 0; i <= Listric_gui.y_ending; i = i + 5) {
                screen2D.drawString("" + i, ((Listric_gui.x_orig)) - 15, (Listric_gui.y_orig + (i * Listric_gui.y_scale)));
                Line2D.Float yticks = new Line2D.Float(((Listric_gui.x_orig)), (Listric_gui.y_orig + (i * Listric_gui.y_scale)), (((Listric_gui.x_orig + Listric_gui.ticksize))), (Listric_gui.y_orig + (i * Listric_gui.y_scale)));
                screen2D.draw(yticks);
            }

//Y axis label
            screen2D.setFont(new Font("Serif", Font.PLAIN, 14));
            screen2D.draw(xaxis);
            AffineTransform orig = screen2D.getTransform();

            screen2D.drawString("Depth(km)", Listric_gui.x_orig - 20, Listric_gui.y_orig - 20);
            screen2D.rotate(-Math.PI / 2);
            screen2D.setTransform(orig);

//X axis label
            screen2D.setFont(new Font("Serif", Font.PLAIN, 14));
            screen2D.drawString("Distance (km)", Listric_gui.x_orig + (Listric_gui.x_ending * Listric_gui.x_scale / 2), Listric_gui.y_orig + (Listric_gui.y_ending * Listric_gui.y_scale) + 40);

//Values
            screen2D.setFont(new Font("Serif", Font.PLAIN, 10));
            screen2D.setColor(Color.red);
            int y_val = 25;
            for (k = 0; k <= 160; k = k + 5) {
                screen2D.drawString(" X = " + String.valueOf(Listric_gui.x[k]) + " Y = " + String.valueOf(Listric_gui.y[k]), (Listric_gui.x_orig + (Listric_gui.x_ending * Listric_gui.x_scale) + 35), y_val);
                y_val = y_val + 10;
            }
//print screen for hwd values
            int y_val2 = 25;
            for (k = 0; k <= 160; k = k + 5) {
                screen2D.drawString("X = " + String.valueOf(Listric_gui.x[k]) + " Hwd = " + String.valueOf(Listric_gui.hwd[k]), (Listric_gui.x_orig + (Listric_gui.x_ending * Listric_gui.x_scale) + 155), y_val2);
                y_val2 = y_val2 + 10;
            }
//Title
            screen2D.setColor(Color.black);
            Font f1 = new Font("Helvetica", Font.BOLD, 24);
            screen2D.setFont(f1);
            screen2D.drawString("LISTRIC FAULT", (Listric_gui.x_ending * Listric_gui.x_scale / 2) + Listric_gui.x_orig, (Listric_gui.y_orig - 10));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

actionListner是您所需要的。看来你使用按钮和组合框的actionListner的主类。我会放一些调试系统或某些东西来确保操作正在解决。

在所有情况下,界面都没有反映您的更改,因为您可能需要.doLayout()和/或.repaint()调用来强制接口更新。