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

Java 如何通过单击另一个框架中的按钮来调用进度条?,java,swing,powershell,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 cl

我的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);
            }

        });
    }
}

当我单击“登录”按钮时,会调用该网页。但我没有获得在swing窗口中传递给它的凭据值

1请参见2 swing GUI可能必须在不同的平台上工作,使用不同的PLAF,在不同的屏幕大小和分辨率上,使用不同的字体大小默认设置。因此,它们不利于组件的精确放置。相反,使用布局管理器,或者空白。这可能是一个有效的例子。不,我所说的问题不起作用提示:确保添加@trashgod或其他人,@对于通知某人新评论很重要。每个评论只能通知一个人。顺便说一句,这句话是针对谁的?@AndrewThompson是正确的;作为调试工作的指南,中的公认答案显示了如何检查流程的输出,而您目前忽略了该输出;你还应该研究他的有用链接。
$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();