如何通过单击另一个框架中的按钮来调用进度条?

时间:2014-08-21 09:01:43

标签: java swing powershell

我的java程序中有两个框架。一个用于凭证窗口,另一个用于进度条。当我单击登录按钮时,进度条也应该开始工作。这是我的代码

package Javapkg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.IOException;
import java.util.Map;
import java.util.Random;

public class ProgressBarDemo extends JPanel implements ActionListener,
        PropertyChangeListener {

    private static final long serialVersionUID = 1L;
    JFrame frame;
    JPanel panel;
    JTextField userText;
    JPasswordField passwordText;
    JButton loginButton;
    JLabel userLabel;
    JLabel passwordLabel;
    JButton cancelButton;
    JButton startButton;
    private JProgressBar progressBar;
    // private JButton startButton;
    private JTextArea taskOutput;
    private Task task;

    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        public void Credential() {
            JFrame frame = new JFrame();
            frame.setUndecorated(true);
            frame.setBackground(new Color(0, 0, 0, 0));
            frame.setSize(new Dimension(400, 300));
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel = new JPanel() {


                private static final long serialVersionUID = 1L;

                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    if (g instanceof Graphics2D) {
                        final int R = 220;
                        final int G = 220;
                        final int B = 250;
                        Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G,
                                B, 0), 0.0f, getHeight(), new Color(R, G, B,
                                255), true);
                        Graphics2D g2d = (Graphics2D) g;
                        g2d.setPaint(p);
                        g2d.fillRect(0, 0, getWidth(), getHeight());
                        Font font = new Font("Serif", Font.PLAIN, 45);
                        g2d.setFont(font);
                        g2d.setColor(Color.lightGray);
                        g2d.drawString("Get Credential", 60, 80);
                    }
                }

            };

            frame.setContentPane(panel);
            frame.setLayout(new FlowLayout());
            frame.placeComponents(panel);

        }

        public void placeComponents(JPanel panel) {

            panel.setLayout(null);

            userLabel = new JLabel("User");
            userLabel.setBounds(40, 100, 80, 25);
            panel.add(userLabel);

            userText = new JTextField(20);
            userText.setBounds(130, 100, 160, 25);
            userText.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });

            panel.add(userText);

            passwordLabel = new JLabel("Password");
            passwordLabel.setBounds(40, 140, 80, 25);
            panel.add(passwordLabel);

            passwordText = new JPasswordField(20);
            passwordText.setBounds(130, 140, 160, 25);
            panel.add(passwordText);

            loginButton = new JButton("login");
            loginButton.setBounds(100, 180, 80, 25);
            loginButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    ProcessBuilder pb = new ProcessBuilder("powershell.exe",
                            "-File", "D:\\MyPowershell\\stage1.ps1");
                    Map<String, String> env = pb.environment();
                    env.put("USER", userText.getText());
                    env.put("PASS", passwordText.getText());
                    System.out.println(userText.getText());
                    System.out.println(passwordText.getText());
                    try {
                        Process process = pb.start();

                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }
            });
            panel.add(loginButton);

            cancelButton = new JButton("cancel");
            cancelButton.setBounds(220, 180, 80, 25);
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            panel.add(cancelButton);

        }
    }

    public Void doInBackground() {
        Random random = new Random();
        int progress = 0;
        // Initialize progress property.
        setProgress(0);
        while (progress < 100) {
            // Sleep for up to one second.
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException ignore) {
            }
            // Make random progress.
            progress += random.nextInt(10);
            setProgress(Math.min(progress, 100));
        }
        return null;
    }

    private void setProgress(int min) {
        // TODO Auto-generated method stub

    }

    /*
     * Executed in event dispatching thread
     */
    public void done() {
        Toolkit.getDefaultToolkit().beep();

        startButton.setEnabled(true);
        // turn off the wait cursor
        taskOutput.append("Done!\n");
    }

    public ProgressBarDemo() {
        super(new BorderLayout());

        // Create the demo's UI.
        //startButton = new JButton("Start");
        //startButton.setActionCommand("start");
        //startButton.addActionListener(this);

        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5, 5, 5, 5));
        taskOutput.setEditable(false);

        JPanel panel = new JPanel();
        //panel.add(startButton);
        panel.add(progressBar);

        add(panel, BorderLayout.PAGE_START);
        add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    }

    /**
     * Invoked when the user presses the start button.
     */
    public void actionPerformed(ActionEvent evt) {
        // startButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // Instances of javax.swing.SwingWorker are not reusuable, so
        // we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
            taskOutput.append(String.format("Completed %d%% of task.\n",
                    task.getProgress()));
        }
    }

    /**
     * Create the GUI and show it. As with all GUI code, this must run on the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("ProgressBarDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        JComponent newContentPane = new ProgressBarDemo();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

                Credential gtw = new Credential();

                gtw.setVisible(true);
            }

        });
    }
}

我正在显示两个帧。但是当我按下登录按钮时,我没有让进度条运行。而且我无法将用户名和密码字段的值输入到powershell脚本中。我的powershell代码在stage1.ps1下面给出:

$username = $env:USER
$password = $env:PASS
$url = "https://www.jabong.com/customer/account/login/"
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep 1; 

} 
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();

当我点击登录按钮时会调用网页。但是我没有将挥杆窗口中的凭据值传递给它

0 个答案:

没有答案