如何多次在第二个窗口而不是另一个窗口上获得结果? [英] How to get result on second window multiple times instead of another open?

查看:54
本文介绍了如何多次在第二个窗口而不是另一个窗口上获得结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的加法程序.在其中,我想将变量从Main_Window传递到Second_Window进行加法运算,并且希望多次在Second_Window上获得结果.意思是,如果我多次从Main_Window传递变量进行加法运算,则结果应位于Second_Window而不是第三和第四Window.

I want to make a simple addition program. In it I want to pass variables from Main_Window to Second_Window for addition and I want to get result on Second_Window multiple times. Means If I pass variables multiple times from Main_Window for addition then result should be on Second_Window not third and fourth Window.

在这里,我希望所有更改都应显示在Second_Window上,而不要在打开时显示.

Here I want All changes should be show on Second_Window not another on open.

编写这些行是为了传递Main_Window中的变量.

These lines are written for passing variables from Main_Window.

Second_Window s = new Second_Window(a,b);
s.setVisible(true);

推荐答案

我将从我的上一个答案中获取我的代码作为我的基本代码回答您的一个问题,并为其添加一些功能:

I'm going to take my code from my previous answer to one of your questions as my base code and add some functionality to it:

首先,您需要创建第二个窗口的一个并且只有一个实例,并具有一种可以更新发送给它的角度的方法.

First of all, you need to create one and only one instance of your second window and have a method that can update the angles sent to it.

如何做,您可能会问自己,很容易,如果还没有创建第二帧,则在动作侦听器中创建实例,否则进行更新.

How do you do that you might be asking yourself, well it's easy, in your action listener you create the instance if the second frame was not created yet and updated it otherwise.

private ActionListener listener = e -> {
    if (e.getSource().equals(submitButton)) {
        if (!frame.isVisible()) {
            circle = new MyCircle((Integer) box1.getSelectedItem(), (Integer) box2.getSelectedItem());
            frame.add(circle);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        } else {
            circle.updateAngles((Integer) box1.getSelectedItem(), (Integer) box2.getSelectedItem());
        }
    }
};

注意:

如果关闭第二个窗口,则所有以前的数据都将丢失,如果要保存该状态,请使用frame可见性并在下面代码的createAndShowGui()方法中初始化MyCircle实例.

Note:

If you close the second window all previous data will be lost, if you want to save that state, then play with the frame visibility and initialize the MyCircle instance in the createAndShowGui() method in the code below.

接下来的事情是,您需要跟踪添加的所有角度,因为您可能需要List并对其进行迭代,或者将其绘制为BufferedImage,然后在JPanel.在此示例中,我们将使用List选项.

Next thing is you need to keep track of all the angles you've added, for that you might need a List and iterate over it, or paint that to a BufferedImage and then paint that image on the JPanel. For this example we'll be using the List option.

但是,对于此示例,如果数据过多,则可能也无法显示,也可以使用JScrollPane来纠正此问题,不过我留给您自己.

However, for this example, if the data is too much, it might not display, to correct that, use a JScrollPane as well, however I'm leaving that up to you.

同样,此示例使整个程序仅在关闭主窗口时终止,而在关闭第二个窗口时不会终止.

This example as well, makes the whole program to terminate only when you close the main window, but not if you close the second window.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RadiusDrawer {
    private JFrame frame;
    private JFrame mainFrame;
    private int centerX = 50;
    private int centerY = 50;
    private int x1 = 0;
    private int y1 = 0;
    private int x2 = 0;
    private int y2 = 0;
    private int r = 100;

    private JComboBox<Integer> box1;
    private JComboBox<Integer> box2;
    private JLabel label1;
    private JLabel label2;
    private JButton submitButton;

    MyCircle circle;

    private static final Integer[] ANGLES = new Integer[]{15, 30, 45, 60, 75, 90};

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new RadiusDrawer()::createAndShowGui);
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        mainFrame = new JFrame("Main Frame");

        mainFrame.add(createMainWindow());
        mainFrame.pack();
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private ActionListener listener = e -> {
        if (e.getSource().equals(submitButton)) {
            if (!frame.isVisible()) {
                circle = new MyCircle((Integer) box1.getSelectedItem(), (Integer) box2.getSelectedItem());
                frame.add(circle);
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            } else {
                circle.updateAngles((Integer) box1.getSelectedItem(), (Integer) box2.getSelectedItem());
            }
        }
    };

    private JPanel createMainWindow() {
        JPanel pane = new JPanel();

        box1 = new JComboBox<>(ANGLES);
        box2 = new JComboBox<>(ANGLES);

        label1 = new JLabel("Angle 1");
        label2 = new JLabel("Angle 2");

        submitButton = new JButton("Submit");

        submitButton.addActionListener(listener);

        pane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(20, 30, 20, 30);
        pane.add(box1, gbc);

        gbc.gridx = 1;
        pane.add(box2, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        pane.add(label1, gbc);

        gbc.gridx = 1;
        pane.add(label2, gbc);

        gbc.gridy = 2;
        pane.add(submitButton, gbc);

        return pane;
    }

    @SuppressWarnings("serial")
    class MyCircle extends JPanel {
        int cx = 0;
        int cy = 0;
        double lineX = 0;
        double lineY = 0;
        double roundedX = 0;
        double roundedY = 0;
        int angle1 = 0;
        int angle2 = 0;

        int angle1HistoryX = 15;
        int angle2HistoryX = 150;
        int angleHistoryY = 300;
        int angleHistoryYGap = 20;

        Color angle1Color = Color.BLUE;
        Color angle2Color = Color.RED;

        List <Integer> angle1History;
        List <Integer> angle2History;

        public MyCircle(int angle1, int angle2) {
            this.angle1 = angle1;
            this.angle2 = angle2;

            angle1History = new ArrayList<>();
            angle2History = new ArrayList<>();

            angle1History.add(angle1);
            angle2History.add(angle2);

            calculateCoords();
            calculateCenter();
        }

        private void updateAngles(int angle1, int angle2) {
            this.angle1 = angle1;
            this.angle2 = angle2;

            angle1History.add(angle1);
            angle2History.add(angle2);
            calculateCoords();

            this.revalidate();
            this.repaint();
        }

        private void calculateCoords() {
            x1 = (int) (r * Math.cos(Math.toRadians(angle1)));
            y1 = (int) (r * Math.sin(Math.toRadians(angle1))) * -1;

            x2 = (int) (r * Math.cos(Math.toRadians(angle2)));
            y2 = (int) (r * Math.sin(Math.toRadians(angle2))) * -1;
        }

        private void calculateCenter() {
            cx = centerX + r;
            cy = centerY + r;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            drawCircle(g2d, centerX, centerY, r);
            drawRadius(g2d);
            drawHistory(g2d);
        }

        private void drawCircle(Graphics2D g2d, int x, int y, int r) {
            g2d.setColor(Color.BLACK);
            g2d.draw(new Ellipse2D.Double(x, y, r * 2, r * 2));
        }

        private void drawRadius(Graphics2D g2d) {
            g2d.setColor(angle1Color);
            g2d.draw(new Line2D.Double(cx, cy, cx + x1, cy + y1));

            g2d.setColor(angle2Color);
            g2d.draw(new Line2D.Double(cx, cy, cx + x2, cy + y2));
        }

        private void drawHistory(Graphics2D g2d) {
            g2d.setColor(angle1Color);
            g2d.drawString("Angle1", angle1HistoryX, angleHistoryY);
            for (int i = 0; i < angle1History.size(); i++) {
                g2d.drawString(angle1History.get(i).toString(), angle1HistoryX, angleHistoryY + (angleHistoryYGap * (i + 1)));
            }

            g2d.setColor(angle2Color);
            g2d.drawString("Angle2", angle2HistoryX, angleHistoryY);
            for (int i = 0; i < angle2History.size(); i++) {
                g2d.drawString(angle2History.get(i).toString(), angle2HistoryX, angleHistoryY + (angleHistoryYGap * (i + 1)));
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 600);
        }
    }
}

就是这样!

这篇关于如何多次在第二个窗口而不是另一个窗口上获得结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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