【问题标题】:Merging 2 windows together将 2 个窗口合并在一起
【发布时间】:2014-01-02 15:10:17
【问题描述】:

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

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

这是我当前的代码: 界面:http://pastebin.com/vAb3nEf1 条形图处理程序: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); 
    }
}

谁能告诉我你如何将它们合并在一起,谢谢:)

【问题讨论】:

  • 我感觉这是一个家庭作业。
  • 编辑问题:添加了代码、swing 和 javafx 标签。

标签: java swing javafx


【解决方案1】:

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

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

另外,如果您过去有类似的问题,请将您的代码与您的问题一起发布在这里,而不是在链接中。这让我们更容易为您提供帮助。


编辑
我刚刚注意到您的代码的一部分用于 JavaFx,而另一部分用于 Swing。如果这是针对 JavaFx 项目,那么我的建议可能对您没有帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 2011-05-22
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多