Java - JLabel 在 while 循环中不会添加
Java - JLabel wont add when in a while loop
我正在用 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.");
}
}
}
}
首先,您需要以更好的方式构建代码。其次,最好使用将添加文本 (JLabel
) 的 swing 容器。在 Box is used, which is a container that uses BoxLayout 下方作为其布局管理器,允许多个组件(在您的情况下为 JLabels
)垂直(Y_AXIS
)或水平(X_AXIS
)布局。然后,您可以使用 JScrollPane
提供 Box 组件的可滚动视图,并将该 JScrollPane 实例添加到您的框架中。每次添加新文本时,都会将其添加到 Box 实例,然后在框架上调用 repaint();
和 revalidate();
以显示文本。如果您只需要在 window 上显示最后的文本(而不是您添加的每个文本),那么取消注释 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();
}
}
我正在用 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.");
}
}
} }
首先,您需要以更好的方式构建代码。其次,最好使用将添加文本 (JLabel
) 的 swing 容器。在 Box is used, which is a container that uses BoxLayout 下方作为其布局管理器,允许多个组件(在您的情况下为 JLabels
)垂直(Y_AXIS
)或水平(X_AXIS
)布局。然后,您可以使用 JScrollPane
提供 Box 组件的可滚动视图,并将该 JScrollPane 实例添加到您的框架中。每次添加新文本时,都会将其添加到 Box 实例,然后在框架上调用 repaint();
和 revalidate();
以显示文本。如果您只需要在 window 上显示最后的文本(而不是您添加的每个文本),那么取消注释 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();
}
}