[英]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.");
}
}
} }
試試這個text1.setVisible(true);
這里:
if (coninput.equals("change text") && winopen == true) {
System.out.print("What do you want the text to say > > > ");
JLabel text1 = new JLabel(input.nextLine());
text1.setVisible(true)
System.out.println("Adding...");
myframe.add(text1);
首先,您需要以更好的方式構建代碼。 其次,最好使用 swing 容器,其中將添加文本 ( JLabel
)。 使用下面的Box ,這是一個使用BoxLayout作為其布局管理器的容器,允許垂直( Y_AXIS
)或水平( JLabels
)布局多個組件(在您的情況下為X_AXIS
)。 然后,您可以使用JScrollPane
提供 Box 組件的可滾動視圖,並將該 JScrollPane 實例添加到您的框架中。 每次添加新文本時,都將其添加到 Box 實例中,然后調用repaint();
並revalidate();
在您的框架上顯示文本。 如果您只需要在 window 上顯示最后一個文本(而不是您添加的每個文本),請取消注釋box.removeAll();
作為快速修復。 否則,不要將 Box 與 JScrollPane 一起使用,而只需將 label 添加到您的框架中,例如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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.