gpt4 book ai didi

java - 将 2 个窗口合并在一起

转载 作者:行者123 更新时间:2023-11-30 04:06:41 25 4
gpt4 key购买 nike

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

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

这是我当前的代码:图形用户界面: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);
}
}

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

最佳答案

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

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

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

<小时/>

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

关于java - 将 2 个窗口合并在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com