将2个窗口合并在一起

时间:2013-12-15 01:53:35

标签: java swing javafx

好的,所以我有2个窗口,我想将它们合并在一起,但不确定如何去做。第一个窗口是“GUI”,这是我想要的唯一窗口,第二个窗口是“Graph”,它显示了我的图形,我希望图形显示在“GUI”窗口的所有其他窗口下,就像在这张图片中一样我有照片购物:http://i.imgur.com/89fP5kE.png

这就是我的2个窗口目前的样子。 GUI:GUI图形:GUI

这是我目前的代码: GUI:http://pastebin.com/vAb3nEf1 BarGraphHandler:http://pastebin.com/DTL2DYj5

import java.awt.*;
import java.awt.event.*;
import java.awt.im.InputContext;
import java.io.IOException;
import java.util.Scanner;

import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

import javax.swing.*;

public class GUI implements ActionListener
{
    static JTextField gender;
    static JTextField password;
    JTextField output;

    GUI()       
    {       
        final JFrame jfrm = new JFrame("Password Security");
        jfrm.setLayout(new BorderLayout());
        jfrm.setSize(800, 450);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gender = new JTextField(10);
        password = new JTextField(10);
        output = new JTextField(10);
        output.setEditable(false);

        // Set first panel to retrive data from user
        JPanel inFieldPane = new JPanel();
        inFieldPane.setLayout(new GridLayout(2,2));
        inFieldPane.add(new JLabel("Gender"));
        inFieldPane.add(gender);
        gender.addActionListener(this);
        inFieldPane.add(new JLabel("Password"));
        inFieldPane.add(password);
        password.addActionListener(this);
        jfrm.add(inFieldPane,BorderLayout.NORTH);

        //Set second panel to submit data for processing
        JPanel submitPane = new JPanel();
        submitPane.setLayout(new FlowLayout());
        submitPane.add(new JLabel("Press button to submit results"));
        JButton submitButton = new JButton("Submit");
        submitButton.addActionListener(this);
        submitPane.add(submitButton);
        jfrm.add(submitPane,BorderLayout.CENTER);

        // Set third panel to display processed data
        JPanel outFieldPane= new JPanel();
        outFieldPane.setLayout(new GridLayout(1,2));
        outFieldPane.add(new JLabel("Output"));
        outFieldPane.add(output);
        jfrm.add(outFieldPane,BorderLayout.SOUTH);      



        jfrm.setVisible(true);

        jfrm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        jfrm.setSize(800, 600);

        jfrm.addWindowListener(new WindowListener() {

            @Override
            public void windowClosing(WindowEvent e) {
                if(JOptionPane.showConfirmDialog(jfrm, "Are you sure ?") == JOptionPane.OK_OPTION){
                    jfrm.setVisible(false);
                    jfrm.dispose();
                }
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowOpened(WindowEvent e) {
            }
        });

        jfrm.setVisible(true);

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Submit"))
        {
            try {
                pass();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static void pass() throws IOException{
        // Initialize variable count to 0
        String st = password.getText().toString();
        int length = st.length();
        String g = gender.getText().toString();
        String gen = null;      
        int count = 0;

        // Convert given string to char array
        char[] c = st.toCharArray();

        // Loop till end of string
        for(int i=0;i<st.length();i++)
        {
            // Get ascii value of each char and store it in k
            int k=(int)c[i];

            // Digits ascii values start from 48 till 57
            if((k>=48)&&(k<=57))
            {
                count++;
            }
        }

        // Print the no.of digits
        System.out.println("No. of digits are " + count);

        // You can also print no.of chars other than digits like..

        int chars = (st.length() - count);
        System.out.println("No. of chars other than digits are " + chars);

        System.out.println("" + st);

        if(g.equalsIgnoreCase("female")){
            gen = "female";
        }else if(g.equalsIgnoreCase("male")){
            gen = "male";
        }else{
            System.out.println("Invalid Gender!");
        }

        if(length > 18 && count >= 4){
            FetchData.main(gen + "5" + ".txt");
        }else if(length > 15 && count >= 3){
            FetchData.main(gen + "4" + ".txt");
        }else if(length > 12 && count >= 2){
            FetchData.main(gen + "3" + ".txt");
        }else if(length > 9 && count >= 1){
            FetchData.main(gen + "2" + ".txt");
        }else{
            FetchData.main(gen + "1" + ".txt");
        }

        BarGraphHandler.main(null);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GUI();
            }
        });
    }
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class BarGraphHandler extends Application {
    final static String lvl1 = "Level 1";
    final static String lvl2 = "Level 2";
    final static String lvl3 = "Level 3";
    final static String lvl4 = "Level 4";
    final static String lvl5 = "Level 5";

    @SuppressWarnings({ "rawtypes", "unchecked" })

    @Override 
    public void start(Stage stage) {
        stage.setTitle("Password Security Results");
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String,Number> bc = 
                new BarChart<String,Number>(xAxis,yAxis);
        bc.setTitle("Graphed Results");
        xAxis.setLabel("Password Security Level");       
        yAxis.setLabel("Percentage of Students");

        XYChart.Series girls = new XYChart.Series();
        girls.setName("Girls");       
        girls.getData().add(new XYChart.Data(lvl1, Integer.parseInt(FetchData.getGraphData("female1.txt"))));
        girls.getData().add(new XYChart.Data(lvl2, Integer.parseInt(FetchData.getGraphData("female2.txt"))));
        girls.getData().add(new XYChart.Data(lvl3, Integer.parseInt(FetchData.getGraphData("female3.txt"))));
        girls.getData().add(new XYChart.Data(lvl4, Integer.parseInt(FetchData.getGraphData("female4.txt"))));
        girls.getData().add(new XYChart.Data(lvl5, Integer.parseInt(FetchData.getGraphData("female5.txt"))));      

        XYChart.Series boys = new XYChart.Series();
        boys.setName("Boys");
        boys.getData().add(new XYChart.Data(lvl1, Integer.parseInt(FetchData.getGraphData("male1.txt"))));
        boys.getData().add(new XYChart.Data(lvl2, Integer.parseInt(FetchData.getGraphData("male2.txt"))));
        boys.getData().add(new XYChart.Data(lvl3, Integer.parseInt(FetchData.getGraphData("male3.txt"))));
        boys.getData().add(new XYChart.Data(lvl4, Integer.parseInt(FetchData.getGraphData("male4.txt"))));
        boys.getData().add(new XYChart.Data(lvl5, Integer.parseInt(FetchData.getGraphData("male5.txt"))));  

        Scene scene  = new Scene(bc,800,600);
        bc.getData().addAll(girls, boys);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { 
        launch(args); 
    }
}

有人可以告诉我你如何将它们合并在一起,谢谢你:)

1 个答案:

答案 0 :(得分:2)

关键是要认为“JPanel”不是JFrame,而是使用适当的布局管理器。更具体地说,

  • 您不需要设置GUI来创建JFrame,而是让它们创建JPanel。这样,您可以以任何您认为合适的方式组合JPanel。
  • 如果你的GUI和你的Graph在创建JPanels的类中,你可以创建一个使用适当布局的第三个JPanel,比如说BorderLayout,
  • 然后你可以将图表JPanel放在第三个JPanel的BorderLayout.CENTER位置
  • 并将您的GUI JPanel放在第三个JPanel的BorderLayout.PAGE_START位置,
  • 然后将第三个JPanel放入JFrame,也就是BorderLayout.CENTER。

此外,如果您过去有类似的问题,请在此处将您的代码与您的问题一起发布,而不是在链接中。这使我们能够更容易地帮助您。


修改
我刚刚注意到你的代码的一部分用于JavaFx,另一部分用于Swing。如果这是针对JavaFx项目的话,我的建议可能对您没有帮助。