字符串文本未显示在Java中的附录文本中
我正在Netbeans Java进行猜测。将猜测号与目标编号进行比较后,结果(消息/字符串文本)未显示在反馈面板TextArea中。我使用附录文本将文本从大型机类传递给了反馈类别。但是,按下猜测按钮后,文本未显示在反馈中。请帮我。
mainfame.java
public class MainFrame extends JFrame{
FeedbackPanel formPanel;
InputPanel textPanel;
public static int target;
JTextArea ta;
public MainFrame()
{
super("HIgher-Lower Game");
formPanel=new FeedbackPanel();
add(formPanel, BorderLayout.EAST);
textPanel=new InputPanel();
add(textPanel, BorderLayout.WEST);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formPanel.setFormListener(new FormListener(){
public void formEventOccurred(FormEvent e){
int num = e.getNum();
//int square=num*num;
if (num == target)
{
formPanel.appendText("Congratulations, your guess is correct!");
//formPanel.setText("Congratulations, your guess is correct!");
//ta.setText("Congratulations, your guess is correct!");
//System.out.paMairintln("Congratulations, your guess is correct!");
}
else if (num > target)
//if guess is higher than the target
{
formPanel.appendText("Your guess is higher than the target");
//formPanel.setText("Your guess is higher than the target");
//ta.setText("Your guess is higher than the target");
//System.out.println("Your guess is higher than the target");
}
else if (num < target)
//if guess is lower than the target
{
formPanel.appendText("Your guess is lower than the target");
//formPanel.setText("Your guess is lower than the target");
//ta.setText("Your guess is lower than the target");
//System.out.println("Your guess is lower than the target");
}
//textPanel.appendText(Integer.toString(square));
}
});
textPanel=new InputPanel();
add(textPanel, BorderLayout.WEST);
setSize(300,300);
setVisible(true);
this.pack();
}
public static void main(String[] args) {
Random random = new Random();
target = random.nextInt(100);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
inputpanel.java
public class InputPanel extends JPanel {
private JLabel label1;
private JTextField tfIn;
private JButton b;
private FormListener formListener;
public FormListener getTextListener() {
return formListener;
}
public void setFormListener(FormListener formListener) {
this.formListener = formListener;
}
public InputPanel() {
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(5,5,5,5);
setLayout(gb);
setBorder(BorderFactory.createTitledBorder("InputForm"));
label1 = new JLabel ("Enter number [1-100]");
gbc.anchor=GridBagConstraints.FIRST_LINE_END;
gbc.gridx=1;
gbc.gridy=1;
gbc.gridheight=1;
gbc.gridwidth=1;
add(label1,gbc);
tfIn = new JTextField (10);
gbc.gridx=1;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=1;
add(tfIn,gbc);
b = new JButton ("Guess");
gbc.anchor=GridBagConstraints.CENTER;
gbc.gridx=1;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=2;
add(b,gbc);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int num = Integer.parseInt(tfIn.getText());
FormEvent ev=new FormEvent(this,num);
if(formListener != null){
formListener.formEventOccurred(ev);
}
}
});
}
}
feffbackpanel.java
public class FeedbackPanel extends JPanel{
JTextArea ta;
FormListener listen;
public FeedbackPanel(){
setBorder(BorderFactory.createTitledBorder("Feedback"));
setLayout(new BorderLayout());
ta=new JTextArea(5,20);
add(new JScrollPane(ta), BorderLayout.CENTER);
}
public void appendText(String text){
ta.append(text + "\n" + "\r");
}
public void setFormListener(FormListener formListener) {
this.listen = formListener;
}
}
formevent.java.java
public class FormEvent extends EventObject{
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public FormEvent(Object source){
super(source);
}
public FormEvent(Object source, int num){
super(source);
this.num=num;
}
}
formlistener.java
public interface FormListener extends EventListener{
public void formEventOccurred(FormEvent e);
}
I am making a guessing game in Netbeans Java. The result (message/ String Text) after comparing the guess number to the target number is not showing in the feedback panel TextArea. I passed the text from the MainFrame class using the appendText to the feedbackPanel class. However, the text is not showing in the feedbackPanel after pressing the guess button. Please help me.
MainFrame.java
public class MainFrame extends JFrame{
FeedbackPanel formPanel;
InputPanel textPanel;
public static int target;
JTextArea ta;
public MainFrame()
{
super("HIgher-Lower Game");
formPanel=new FeedbackPanel();
add(formPanel, BorderLayout.EAST);
textPanel=new InputPanel();
add(textPanel, BorderLayout.WEST);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formPanel.setFormListener(new FormListener(){
public void formEventOccurred(FormEvent e){
int num = e.getNum();
//int square=num*num;
if (num == target)
{
formPanel.appendText("Congratulations, your guess is correct!");
//formPanel.setText("Congratulations, your guess is correct!");
//ta.setText("Congratulations, your guess is correct!");
//System.out.paMairintln("Congratulations, your guess is correct!");
}
else if (num > target)
//if guess is higher than the target
{
formPanel.appendText("Your guess is higher than the target");
//formPanel.setText("Your guess is higher than the target");
//ta.setText("Your guess is higher than the target");
//System.out.println("Your guess is higher than the target");
}
else if (num < target)
//if guess is lower than the target
{
formPanel.appendText("Your guess is lower than the target");
//formPanel.setText("Your guess is lower than the target");
//ta.setText("Your guess is lower than the target");
//System.out.println("Your guess is lower than the target");
}
//textPanel.appendText(Integer.toString(square));
}
});
textPanel=new InputPanel();
add(textPanel, BorderLayout.WEST);
setSize(300,300);
setVisible(true);
this.pack();
}
public static void main(String[] args) {
Random random = new Random();
target = random.nextInt(100);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
InputPanel.java
public class InputPanel extends JPanel {
private JLabel label1;
private JTextField tfIn;
private JButton b;
private FormListener formListener;
public FormListener getTextListener() {
return formListener;
}
public void setFormListener(FormListener formListener) {
this.formListener = formListener;
}
public InputPanel() {
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(5,5,5,5);
setLayout(gb);
setBorder(BorderFactory.createTitledBorder("InputForm"));
label1 = new JLabel ("Enter number [1-100]");
gbc.anchor=GridBagConstraints.FIRST_LINE_END;
gbc.gridx=1;
gbc.gridy=1;
gbc.gridheight=1;
gbc.gridwidth=1;
add(label1,gbc);
tfIn = new JTextField (10);
gbc.gridx=1;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=1;
add(tfIn,gbc);
b = new JButton ("Guess");
gbc.anchor=GridBagConstraints.CENTER;
gbc.gridx=1;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=2;
add(b,gbc);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int num = Integer.parseInt(tfIn.getText());
FormEvent ev=new FormEvent(this,num);
if(formListener != null){
formListener.formEventOccurred(ev);
}
}
});
}
}
FeedbackPanel.java
public class FeedbackPanel extends JPanel{
JTextArea ta;
FormListener listen;
public FeedbackPanel(){
setBorder(BorderFactory.createTitledBorder("Feedback"));
setLayout(new BorderLayout());
ta=new JTextArea(5,20);
add(new JScrollPane(ta), BorderLayout.CENTER);
}
public void appendText(String text){
ta.append(text + "\n" + "\r");
}
public void setFormListener(FormListener formListener) {
this.listen = formListener;
}
}
FormEvent.java
public class FormEvent extends EventObject{
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public FormEvent(Object source){
super(source);
}
public FormEvent(Object source, int num){
super(source);
this.num=num;
}
}
FormListener.java
public interface FormListener extends EventListener{
public void formEventOccurred(FormEvent e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中的问题:
#1是,大型机中的formeventoccurred()从未被调用。
发生这个问题,因为有两个面板,它们将相互通信。您通过创建一个界面来解决此问题,这是正确的。但是,错误类的接口被调用。
#2是,您创建并添加了两次TextPanel。这不应引起所描述的问题,但这不是很好。
#3是,变量的命名不好。请看一下命名约定。此代码不容易阅读。
#4并不是真正的问题,但是如果不需要,则不应使用关键字静态。
#5是,如果可以是本地变量,则原始数据类型和对象不应将其声明为类属性(字段)。对于可读性而言,这要好得多。
#6是,代码中有很多冗余。如果程序员在阅读自己的代码方面有问题,可能会发生这种情况。如果是这种情况,那不是直接“不好”,这对初学者来说是正常的。下次,尝试通过使用变量名称“告诉他们的工作是什么”,等等。
#7是,通常,将变量“私有”是一种好习惯使它们公开或受到保护,等等。
现在,通过学习来乐趣!
工作代码:
这是工作代码的完整版本:
类:Mainframe
类:feffbackpanel
class:inputpanel
类:formevent
接口:formListener
Problems in your code:
#1 is, that formEventOccurred() in MainFrame is never called.
This problem occurs, because there are two panels, which shall communicate with each other. You solved this by creating an interface, which is correct. But the interface of the wrong class is called.
#2 is, that you created and added the textPanel twice. This should not cause the described problem, but this is not good.
#3 is, that the variables are not named well. Please take a look at naming conventions. This code is not easy to read.
#4 is not really a problem, but the keyword static should not be used if it is not needed.
#5 is, that primitive datatypes and objects should not be declared as class attributes (fields), if they can be local variables instead. This is a lot better for readability.
#6 is, that there is much redundancy in the code. This can occur, if the programmer has problems at reading his own code. If that is the case, that is not directly "bad", that is normal for beginners. The next time, try to write the code more readable by using variable names, which are "telling what their job is", etc.
#7 is, that in general, it is a good practice to make variables "private" until you need to make them public or protected, etc.
And now, have fun by learning!
Working code:
This is a full version of the working code:
Class: Mainframe
Class: FeedbackPanel
Class: InputPanel
Class: FormEvent
Interface: FormListener