gpt4 book ai didi

java - 如何在 Java jPanel 上打印一些文本?

转载 作者:行者123 更新时间:2023-12-01 21:29:54 28 4
gpt4 key购买 nike

这对某些人来说可能是一个非常明显的问题,但我无法弄清楚。我是 Eclipse 新手,只使用 Dr.Java 进行编程。我正在创建一个疯狂的库程序,用户必须输入名词、形容词、名称、数字,最后它将显示在故事中。

用户输入所有必需的信息后,我希望在 jPanel 中打开完成的故事。我不知道如何将文本添加到 jPanel。(我希望在用户输入所有信息后以 c. 代码开头的文本显示在窗口中)代码如下:

import java.util.Scanner;   
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MadLibs{

public static void Action1 ()
{

JFrame frame = new JFrame("Mad Libs");
// Add a window listener for close button
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

Scanner input = new Scanner(System.in);

System.out.println("Male Friend:");
String maleFriend = input.nextLine();
System.out.println("Adjective:");
String adjective1 = input.nextLine();
System.out.println("Past Tense Verb:");
String pastTenseVerb1 = input.nextLine();
System.out.println("Past Tense Verb 2:");
String pastTenseVerb2 = input.nextLine();
System.out.println("Large Number:");
String largeNumber = input.nextLine();
System.out.println("Famous Female:");
String famousFemale = input.nextLine();
System.out.println("Adverb:");
String adverb = input.nextLine();
System.out.println("Place:");
String place = input.nextLine();
System.out.println("Body Part(singular):");
String bodyPart = input.nextLine();
System.out.println("Large Number:");
String largeNumber2 = input.nextLine();
System.out.println("Verb ending with -ing");
String ingEnding1 = input.nextLine();
System.out.println("Singular noun:");
String singularNoun = input.nextLine();
System.out.println("Plural Noun:");
String pluralNoun = input.nextLine();



// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(600, 800));
frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

//The story I want displayed on the jPanel:
/*
c.println("The Great Dough Disaster");
c.println("\nLast summer, my friend "+ maleFriend + " got a job at the " + adjective1 +" Pastry Shop. For the first few");
c.println("weeks, he " + pastTenseVerb1 + " the floors, " + pastTenseVerb2 + " on the shelves, and unloaded " + largeNumber + " pound sacks");
c.println("of flour from the delivery trucks.\n");
c.println("Finally, "+famousFemale+", the owner, told "+maleFriend+" that she would teach him to make bread. Now,");
c.println("pay attention, "+maleFriend+",” she said every day. “I'll make the first batch of dough. Then you");
c.println("can make the next batch while I go to "+place+".\n");
c.println("Poor "+maleFriend+"! He had a habit of letting his "+bodyPart+" wander. When " +famousFemale+ " left for "+place);
c.println("he started to mix the ingredients. “Let me see,” he said. “I think she put in "+largeNumber2);
c.println("packages of yeast.”\n");
c.println("A short while later, the dough started "+ingEnding1+". It kept on "+ingEnding1+". "+maleFriend+" tried to");
c.println("cover it with a(n) "+singularNoun+", but the dough wouldn't stop "+ingEnding1+". It was everywhere! ");
c.println("“What can I do?” thought "+maleFriend+".\n");
c.println("Just then, Tyana returned from toronto. “"+maleFriend+"” she screamed. “What have you done?”");
c.println("“It's not my fault,” cried "+maleFriend+". “The dough just started "+ingEnding1+" and wouldn't stop.”");
c.println(famousFemale + " had to let him go. Now "+maleFriend+" has a job making "+singularNoun+". I don't think he'll ever");
c.print("eat bread again, let alone make it.");
*/


}

public static void main(String []args){
Action1();
}

}

另外,我对 jPanel 的工作原理有点困惑。我发现了很多在 jPanel 上显示内容的方法,但我不知道应该使用哪一种。

最佳答案

我立即想到了两种方法来做到这一点。

第一个更简单,那就是使用paintComponent(Graphics)方法。每当程序认为需要重新绘制对象时,例如最小化和 ermmmm 取消最小化包含 jpanel 的窗口,就会自动调用此方法。

这是一个简单的例子...

import javax.swing.JPanel;

import java.awt.Graphics;

import java.lang.Override; //for @Override

class yourClass extends JPanel { //yourClass is the JPanel
@Override //if you aren't overriding correctly this makes the compiler tell you
protected void paintComponent(Graphics gr){
super.paintComponent(gr);
gr.drawString("string literal or a string variable", 0,10);
}
}

代码解释...

super.paintComponent(gr);重写时应始终使用 super 命令,在这种情况下,您将重写 JPanel 的 PaintComponent 方法,因此请按照所示操作。这还可以实现增量绘制(正如我所说的),这意味着程序不会将屏幕绘制成白色,然后绘制它要执行的操作。所以你可以画一个黑框,一分钟后画一个红框,黑框不会消失。

gr.drawString(String strText,int x,int y); Graphic类的drawString方法。图形gr是程序通过一堆隐藏的方法创建的。所以不用担心创建它,只需将它放在paintComponent参数中,然后让程序调用paintComponent即可。

对于drawString命令,x和y是坐标,我把y设置为10,因为根据我的经验,该方法按以下方式工作:从坐标(x,y)(这是相对于JPanel的),绘制到向左、向上。因此,如果 y 为 0,您的文本将从 JPanel 中绘制出来。这意味着您将看不到它,因为在 JPanel 之外绘制的任何内容都不会出现。 (具体原因我也不知道)

这是使用paintComponent,如果你使用循环来绘制每条线,它可以有效地工作;因为drawString绘制一条线。这比下一个解决方案简单得多。

下一个解决方案是使用布局。如果你想一排一排地绘制每一行,每次向下一行,我会推荐 gridLayout(rows, columns)。如果您有 20 行文本,该方法将需要 new GridLayout(20, 1);

这个想法是计算行数和列数。

 .    Column 1 

第 1 行

第 2 行

第 3 行

等等

GridLayout 的问题是您需要一个对象来保存每个字符串(在这种情况下,我推荐 JLabel)。

您还需要启用增量绘制(或者我非常怀疑),这意味着创建一个扩展 JLabel 的类。

最后,您还需要将 JLabel 作为子项添加到 JPanel

JPanel.add(JLabel);

尽管您已经使用了一些布局内容,但这可能并不那么难。

举个例子...

import java.awt.GridLayout;
import java.awt.Graphics;

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

import java.lang.Override;

class YourMainClass {
static JFrame mainFrame;
static YourJLabel clsLabel;
static JPanel pnlJPanel;
public static void main(String[]args){
mainFrame = new JFrame("Testing"); //initialize, and set size the frame
mainFrame.setSize(500,500);
pnlJPanel = new JPanel();//initialize our panel
pnlJPanel.setLayout(new GridLayout(3,1));//set its layout to gridlayout, with grid of 3 rows and 1 column
clsLabel = new YourJLabel(); //create a jlabel, add some text to it, then add it to the jpanel
clsLabel.setText("some");
pnlJPanel.add(clsLabel);
clsLabel = new YourJLabel();
clsLabel.setText("Text");
pnlJPanel.add(clsLabel);
clsLabel = new YourJLabel();
clsLabel.setText("drawn");
pnlJPanel.add(clsLabel);
mainFrame.add(pnlJPanel);//add the jpanel to the frame
mainFrame.pack(); //believe you already know these two lines
mainFrame.setVisible(true);
}
}
class YourJLabel extends JLabel {
YourJLabel(){
super();
setOpaque(true); //going on memory, by default jlabels opaque is false, or transparent
}
protected void paintComponent(Graphics gr){
super.paintComponent(gr);
}
}

这个例子的作用是:

enter image description here

使用布局时,一切都与其父级相关。

请注意,我们无法再访问前两个 JLabel,如果您想在使用类实例时访问它们,则必须将实例存储在从您那里获取它们的位置。存储它们的最佳位置是数组。一个简单的例子... YourJLabel[] aryJLabel = new YourJLabel[3];

[3]决定了数组的大小,一旦创建就无法更改。 YourJLabel 上的 [] 表示您正在创建 YourJLabel 数组。

关于java - 如何在 Java jPanel 上打印一些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com