【问题标题】:Java - JLabel wont add when in a while loopJava - JLabel 在 while 循环中不会添加
【发布时间】:2022-01-24 18:33:49
【问题描述】:

我正在用 Java 和 Swing 制作一个 GUI 控制台。它调用扫描仪输入文本,然后将该文本放入一个变量中以提供给 JFrame。加粗的代码是有问题的代码。

代码有效,但是当您输入“更改文本”时,在输入所需的输入后,它不会将 JLabel 添加到 JFrame。

请帮忙!谢谢!

//prepare all imports

Random rand = new Random();

Scanner input = new Scanner(System.in);

JFrame myframe = new JFrame();

myframe.setSize(300, 300);
myframe.setTitle("Blank Window");
myframe.setResizable(false);
myframe.setLocation(300,300);


//*******************************************

boolean done = false;
boolean winopen = false;

System.out.println("Type commands here. Type 'help' for list of commands.");


while (done == false) {

  System.out.print("Console > > >  ");  

  String coninput = input.nextLine();

  if (coninput.equals("window open")) {
    System.out.println("Opening Window...");
    System.out.println("Done!");

    winopen = true;

    myframe.setVisible(true);
  }

  if (coninput.equals("window close")) {
    System.out.println("Closing Window...");

    winopen = false;

    myframe.setVisible(false);
    System.out.println("Done!");
  }

  if (coninput.equals("exit")) {
    System.out.println("Exiting...");
    myframe.dispose();
    done = true;
    System.out.println("Done!");
  }

  if (coninput.equals("help")) {
    System.out.println("Commands: ");
    System.out.println("window open: opens a window");
    System.out.println("window close: closes the open window");
    System.out.println("exit: shuts down the program");
    System.out.println("help: lists commands");
    //System.out.println("");
    //System.out.println("");
    //System.out.println("");
  }

  **if (coninput.equals("change text") && winopen == true) {
    System.out.print("What do you want the text to say > > > ");
    JLabel text1 = new JLabel(input.nextLine());
    System.out.println("Adding...");
    myframe.add(text1);
  }**



  if (coninput.equals("change text") && winopen == false) {
    System.out.print("You have to have a window open.");
  }

}

} }

【问题讨论】:

    标签: java swing jframe jlabel


    【解决方案1】:

    首先,您需要以更好的方式构建代码。其次,最好使用将添加文本 (JLabel) 的摇摆容器。下面使用Box,这是一个使用BoxLayout作为其布局管理器的容器,允许垂直(Y_AXIS)或水平(X_AXIS)布局多个组件(在您的情况下为JLabels) .然后,您可以使用JScrollPane 提供 Box 组件的可滚动视图,并将该 JScrollPane 实例添加到您的框架中。每次添加新文本时,都将其添加到 Box 实例中,然后在框架上调用 repaint();revalidate(); 以显示文本。如果您只需要在窗口上显示最后一个文本(而不是您添加的所有文本),请取消注释 box.removeAll(); 作为快速修复。否则,不要将 Box 与 JScrollPane 一起使用,而只需将标签添加到框架中,例如 myframe.getContentPane().add(lbl, BorderLayout.CENTER);。再次,记得拨打myframe.getContentPane().removeAll();在添加新的 JLabel 之前,以及之后添加 repaint();revalidate();。下面的工作示例:

    import java.awt.*;
    import javax.swing.*;
    import java.util.Scanner;
    
    public class App {
        Scanner input;
        JFrame myframe;
        Box box;
        JScrollPane scrollPane;
    
        public App() {
            input = new Scanner(System.in);
            box = new Box(BoxLayout.Y_AXIS);
            scrollPane = new JScrollPane(box);
            myframe = new JFrame();
            myframe.getContentPane().add(scrollPane, BorderLayout.CENTER);
            myframe.setSize(300, 300);
            myframe.setTitle("Blank Window");
            myframe.setResizable(false);
            myframe.setLocation(300, 300);
        }
    
        public void go() {
            boolean done = false;
            boolean winopen = false;
            System.out.println("Type commands here. Type 'help' for list of commands.");
    
            while (done == false) {
                System.out.print("Console > > >  ");
                String coninput = input.nextLine();
    
                if (coninput.equals("window open")) {
                    System.out.println("Opening Window...");
                    System.out.println("Done!");
                    winopen = true;
                    myframe.setVisible(true);
                }
    
                if (coninput.equals("window close")) {
                    System.out.println("Closing Window...");
                    winopen = false;
                    myframe.setVisible(false);
                    System.out.println("Done!");
                }
    
                if (coninput.equals("exit")) {
                    System.out.println("Exiting...");
                    myframe.dispose();
                    done = true;
                    System.out.println("Done!");
                }
    
                if (coninput.equals("help")) {
                    System.out.println("Commands: ");
                    System.out.println("window open: opens a window");
                    System.out.println("window close: closes the open window");
                    System.out.println("exit: shuts down the program");
                    System.out.println("help: lists commands");
                }
    
                if (coninput.equals("change text") && winopen == true) {
                    System.out.print("What do you want the text to say > > > ");
                    JLabel lbl = new JLabel(input.nextLine());
                    System.out.println("Adding...");
                    // box.removeAll();
                    box.add(lbl);
                    myframe.repaint();
                    myframe.revalidate();
                    Rectangle bounds = lbl.getBounds();
                    scrollPane.getViewport().scrollRectToVisible(bounds);// scroll to the new text
                }
    
                if (coninput.equals("change text") && winopen == false) {
                    System.out.print("You have to have a window open.");
                }
    
            }
        }
    
        public static void main(String[] args) {
            new App().go();
        }
    }
    

    【讨论】:

    • 嘿,谢谢!我一直在使用 Replit,直到我可以在我的电脑上安装 Blue Jay。此代码是否适用于 Repls Swing 功能?另外,你导入的 java.awt 类是什么?
    • 好的,感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2011-07-08
    • 2010-11-26
    相关资源
    最近更新 更多