简体   繁体   English

java swing它没有system.out.println

[英]java swing it doesn't without system.out.println

Hello so i started learning java a week back and i basically started making a gui just to see how things work and i found a weird "bug" or i don't exactly understand how things work and it's not even a bug你好,所以我一周前开始学习 Java,我基本上开始制作 gui 只是为了看看事情是如何工作的,我发现了一个奇怪的“错误”,或者我不完全理解事情是如何工作的,它甚至不是一个错误

i have a class called startPanel that makes a panel that is visible from the start and it asks you as to what you wish to log in admin,user or a guest this is startPanel:我有一个名为startPanel的类,它使面板从一开始就可见,它会询问您希望登录管理员、用户或访客的内容,这是 startPanel:

package library;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/*
 * this panel is responsible for the first opening panel and redirects you to your panel
 * 
 * 
 */
import javax.swing.*;
public class startPanel extends JFrame {


boolean adminState=false;
boolean userState=false;
boolean guestState=false;

JButton adminBut,userBut,guestBut ;

//start of constructor
public startPanel(){
    //frame size,close when pressing x,title,and spawn at middle of the screen
    this.setSize(500,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Welcome guest");
    this.setLocationRelativeTo(null);
    //making the panel
    JPanel panel1 = new JPanel();
    //making a label to fill things up it doesn't really do anything
    JLabel startLabel = new JLabel("you wan't to log in as...");
    //3 buttons for the user to click 1 only and the according frame will show up
    adminBut = new JButton("Admin");
    userBut = new JButton("User");
    guestBut = new JButton("Guest");
    //making an event handler for admin only so far just for test purposes
     ListenForButton lForButton = new ListenForButton();
     adminBut.addActionListener(lForButton);

     //adding comps to the panel
    panel1.add(startLabel);
    panel1.add(adminBut);
    panel1.add(userBut);
    panel1.add(guestBut);

    //adding the panel to the frame
    this.add(panel1);

} // end of startPanel constructor

private class ListenForButton implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        /*probably not the correct way to do what i want to but just figured this might work
         *it only works for admin button if the user presses the adminBut
         *it will change the states and with a getter we can change each state 
         *from main accordingly
         */
        if (event.getSource() == adminBut ){
            adminState=true;
            guestState=false;
            userState= false;

        }
    }

} // end of Listen for button

//all getters for the states
public boolean getAdminState(){
    return adminState;
}
public boolean getUserState(){
    return guestState;
}
public boolean getGuestState(){
    return userState;
}

}

this is main :这是主要的:

package library;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class mainLibrary {
public static void main(String[] args) {


    adminPanel adminP = new adminPanel();
    userPanel userP = new userPanel();
    startPanel gui = new startPanel();
    gui.setVisible(true);

    while(true){
        System.out.println(gui.getAdminState());
        if (gui.getAdminState() == true) {
            gui.setVisible(false);
            userP.setVisible(true);

        }
    }

the problem now is that if i remove System.out.println(gui.getAdminState());现在的问题是,如果我删除System.out.println(gui.getAdminState()); this does not work it doesn't even get in the if at all if it's false at start if i don't remove it works correctly :/ so what is going on这不起作用它甚至不会进入如果在开始时它是假的如果我不删除它正常工作:/所以发生了什么

this is adminPanel for the adminPanel if it matters如果重要,这是 adminPanel 的 adminPanel

package library;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class adminPanel extends JFrame {

    //start of adminPanel constructor
public adminPanel(){ 
    //frame size,close when pressing x,title,and spawn at middle of the screen
    this.setSize(500,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Admin panel area");
    this.setLocationRelativeTo(null);

    JPanel panel1 = new JPanel();

    this.add(panel1);


    } //end of admin constructor

}

This is not a good GUI design.这不是一个好的 GUI 设计。 Never use such active loop.永远不要使用这种主动循环。

Remarks: use standard naming convention (uppercase/lowercase), and never call a frame a panel (this is too confusing).备注:使用标准命名约定(大写/小写),切勿将框架称为面板(这太混乱了)。

A better design would be to have references to Panels to be activated in the startPanel and setting appropriate property reacting to button actions.更好的设计是引用要在 startPanel 中激活的面板并设置适当的属性以响应按钮操作。 Something like:就像是:

class StartFrame extends JFrame implements ActionListener {
    private JFrame adminFrame;
    private JFrame userFrame;
    ...

    // add construtor to initialize adminFrame and userFrame appropriately
    ...

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == adminBut) {
            this.setVisible(false);
            adminFrame.setVisible(true);
        }
        if (e.getSource() == userBut) {
            this.setVisible(false);
            userFrame.setVisible(true);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM