Java GUI,从一个类过渡到另一个类

时间:2019-01-28 18:51:52

标签: java swing

我很沮丧,渴望获得帮助。能够弄清楚其他所有内容,直到进入GUI。我想做的是从LogInPane(登录页面)转到JobSelectionGUI(工作选择页面);

当我编译它时,它可以按照我想要的方式运行,但是我无法弄清楚如何从JFrame中获取LogInPaneGUI,并在打开JobSelectionGUI时关闭它,和视频并选择GUI /应用它们很困难!我从GridLayout开始,然后切换到GridBagLAyout,然后尝试CardLayout,现在回到GBL。

我不使用IDE;我使用SublimeText,所以如果我的代码中某些内容看起来非常冗长,那是因为我不得不为Sublime写了很长时间(或者因为我很糟糕)。我所有的代码都分为不同的类,因此它保持整洁且易于调试。每个类都有自己的包,每个包最多包含2个方法。我不停地打屁股,以使主干完全空着!

接受所有批评和建议!

主要:

package com.hallquist.kurtis.leigh.srcmain;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.text.*;

    // Class imports;
    import JobSelection.*;
    import LogInPane.*;

    // My Main function. Used to pull packages and methods and compile them here;
    public class SrcMainUserInformation{
      public static void main(String[] args){

        LogInPaneGUI logInGUI = new LogInPaneGUI();
        logInGUI.logInPaneMainGUI();
      }
    }

头等舱:

    package LogInPane; // package name;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.ImageIcon;

    import JobSelection.*; //import for next GUI when LogInButton is clicked;

    public class LogInPaneGUI{

      private static final JFrame frame = new JFrame();
      private static final int COLS = 10; // Max columns;
      private static final JPanel panelForm = new JPanel(new GridBagLayout()); // layout
      private static final JTextField fieldLogInName = new JTextField(COLS); //login
      private static  final JPasswordField logInPassword = new JPasswordField(COLS); //pw
      private static final JButton logInButton = new JButton("Log In"); //login button
      private static final JButton exitButton = new JButton("EXIT"); //system.exit button
      private static final JButton newUser = new JButton("New User? Click here to sign up!"); // new user button
    // Wigits on login page;
      public LogInPaneGUI(){

      GridBagConstraints c = new GridBagConstraints();
    // Creates the panel that goes ontop of the JFrame;
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_END;
          panelForm.add(new JLabel("Account Name: "), c);
            c.gridy ++;
          panelForm.add(new JLabel("Password: "), c);
        c.gridy ++;
        c.gridx = 1;
        c.gridy = 0;
          c.anchor = GridBagConstraints.LINE_START;
            panelForm.add(fieldLogInName, c);
        c.gridy++;
            panelForm.add(logInPassword, c);
        c.gridy++;
            panelForm.add(logInButton, c);
        c.gridy++;
            panelForm.add(newUser, c);
        c.gridy++;
            panelForm.add(exitButton, c);
    // Goes to fourm/website on newUser click;
      newUser.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
          System.out.println("Fourm/Website to sign up for game");
        }
      });
    // Exits program on exitButton click;
      exitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
          System.exit(0);
        }
      });
    // Goes to JobSelectionGUI on logInButton Click;
      logInButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
        JobSelectionGUI jobSelecting = new JobSelectionGUI();
        jobSelecting.jobSelectionJFrameGUI();
    //    frame.dispose();
    //      System.out.println("Will log you in when I set it up");
        }
      });
    }
    // Actual JFrame that everything goes ontop of;
      public static void logInPaneMainGUI(){

        JFrame frame = new JFrame("FirstProject");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(1080, 720);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().add(panelForm);
      }
    }

第二堂课:

    package JobSelection;

    import java.awt.*;
    import java.awt.event.ActionEvent.*;
    import javax.swing.*;
    import javax.swing.text.*;
    // Mass import from JobSelection package; All base job information;
    import JobSelection.JobInformationIndex.JobAmazonData.*;
    import JobSelection.JobInformationIndex.JobBanditData.*;
    import JobSelection.JobInformationIndex.JobLancerData.*;
    import JobSelection.JobInformationIndex.JobSorcererData.*;
    import JobSelection.JobInformationIndex.JobWitchData.*;
    import LogInPane.*; // to return to login screen;
    import JobSelection.*; // dont know if needed;

    public class JobSelectionGUI{

      private static JFrame frame = new JFrame();
      private static JSplitPane jSplitPane = new JSplitPane();
      private static JPanel leftPane = new JPanel();
      private static JLabel logInCharacterName = new JLabel();
      private static JTextField userCharacterName = new JTextField();
      private static JButton logInAccept = new JButton("Accept");
      private static JButton logInBack = new JButton("Back");
      private static JTextArea firstGameIntroduction = new JTextArea();
      private static JTextArea descriptionIntroduction = new JTextArea();
      private static JTextArea jobSelectedInformation = new JTextArea();
      private static JPanel allWidgits = new JPanel();

      public JobSelectionGUI(){

      JTextArea firstGameIntroduction = new JTextArea("\ Text.");
        firstGameIntroduction.setLineWrap(true);
        firstGameIntroduction.setWrapStyleWord(true);
        JScrollPane areaScrollPane = new JScrollPane(firstGameIntroduction);
        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        areaScrollPane.setPreferredSize(new Dimension(250, 250));
        areaScrollPane.setBorder(
          BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Introduction"),
            BorderFactory.createEmptyBorder(5,5,5,5)),
          areaScrollPane.getBorder()));

        leftPane.add(areaScrollPane);

      JTextArea descriptionIntroduction = new JTextArea(" Text.\n");
        descriptionIntroduction.setLineWrap(true);
        descriptionIntroduction.setWrapStyleWord(true);
        JScrollPane areaScrollPaneTwo = new JScrollPane(descriptionIntroduction);
        areaScrollPaneTwo.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        areaScrollPaneTwo.setPreferredSize(new Dimension(250, 250));
        areaScrollPaneTwo.setBorder(
          BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("What to expect"),
            BorderFactory.createEmptyBorder(5,5,5,5)),
          areaScrollPaneTwo.getBorder()));

        leftPane.add(areaScrollPaneTwo);
    }

      public static void jobSelectionJFrameGUI(){

        JFrame frame = new JFrame("FirstProject");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1080, 720);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().add(leftPane);
      }
    }

1 个答案:

答案 0 :(得分:1)

  1. 您过度使用了static修饰符,并且大多数字段实际上不是应该是静态的。我敢说一下,如果不是所有的类的所有字段都应该是私有的 instance 字段。
  2. 登录窗口不应 是JFrame,而应是阻塞对话框或“模态”对话框,对于Swing,这意味着使用模态JDialog或JOptionPane(在幕后创建模态JDialog) )
  3. 模式对话框将在显示时阻止调用代码
  4. 如果对话框是模式对话框,那么您将知道何时不再可见该对话框,因为调用代码已被解锁。在这种情况下,您将查询对话框字段的状态(使用公共getter方法,而不是直接调用静态字段),并确定登录是否成功。如果是这样,请显示您的主GUI窗口或JFrame。
  5. 另一种选择是,使用CardLayout,但是要使其正常工作,所有主要的GUI类都应面向创建JPanel,而不是JFrame。这样,您可以在需要的位置和时间插入面板,包括在诸如JFrames或JDialogs的顶级窗口中,在或JPanels中,或使用CardLayout交换面板。
  6. 请注意,frame.dispose()对您不起作用,因为您通过 重新声明 对其进行了阴影 1}}方法。

logInPaneMainGUI()

请勿执行此操作,并且public static void logInPaneMainGUI() { // ***** this creates a new local JFrame variable called frame JFrame frame = new JFrame("FirstProject"); // so calling frame.dispose() elsewhere will have no effect on this window 方法调用将关闭第一个窗口。

.dispose()

当然,框架必须是非最终的或之前未初始化的。不过,我仍然建议使用JDialog,并将 移出静态世界并移入实例世界

无关的批评:

您的问题中的闲聊文本,与您的实际问题完全无关的文本以及 more 文本告诉我们有用的信息,可帮助我们理解您的问题和代码,从而帮助我们解决问题。