如何显示(字符串)在Java GUI中的另一堂课中导致Textarea
在确定输入猜测数字是否更高/较低/从大型机类中正确/正确之后,我想在FeffbackPanel类中显示字符串结果(TextArea中的TextArea位于FeftbackPanel中)。但是,在我运行代码后,相应的字符串结果未显示。在大型机类评估后,如何使用FefagbackPanel类显示字符串结果?
*我想在反馈类中的TextArea中显示的字符串结果是“恭喜,您是正确的”,“您的猜测高于目标”,“您的猜测低于目标“
反馈”类
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ThatStupidCoder
*/
public class FeedbackPanel extends JPanel{
JTextArea ta;
public FeedbackPanel(){
setBorder(BorderFactory.createTitledBorder("Feedback"));
setLayout(new BorderLayout());
ta=new JTextArea(5,10);
add(new JScrollPane(ta), BorderLayout.CENTER);
}
public void appendText(String text){
ta.append(text + "\n" + "\r");
}
}
Mainframe类
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.WEST);
formPanel.setFormListener(new FormListener(){
public void formEventOccurred(FormEvent e){
int num = e.getNum();
//int square=num*num;
if (num == target)
{
ta.setText("Congratulations, your guess is correct!");
//System.out.println("Congratulations, your guess is correct!");
}
else if (num > target)
//if 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
{
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.EAST);
setSize(300,300);
setVisible(true);
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Random random = new Random();
target = random.nextInt(100);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
After identifying if the input guess number is higher/ lower/ correct from the MainFrame class, I want to display the string result in the textarea under FeedbackPanel class (the textarea is located in the FeedbackPanel). However after I ran the codes, the corresponding string result is not showing. How can I display the string result using FeedbackPanel class after evaluating it in the MainFrame class?
*The string result that I want to display in the Textarea in FeedbackPanel class are "Congratulations, you are correct", "Your guess is higher than the target", "Your guess is lower than the target"
FEEDBACKPANEL CLASS
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ThatStupidCoder
*/
public class FeedbackPanel extends JPanel{
JTextArea ta;
public FeedbackPanel(){
setBorder(BorderFactory.createTitledBorder("Feedback"));
setLayout(new BorderLayout());
ta=new JTextArea(5,10);
add(new JScrollPane(ta), BorderLayout.CENTER);
}
public void appendText(String text){
ta.append(text + "\n" + "\r");
}
}
MAINFRAME CLASS
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.WEST);
formPanel.setFormListener(new FormListener(){
public void formEventOccurred(FormEvent e){
int num = e.getNum();
//int square=num*num;
if (num == target)
{
ta.setText("Congratulations, your guess is correct!");
//System.out.println("Congratulations, your guess is correct!");
}
else if (num > target)
//if 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
{
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.EAST);
setSize(300,300);
setVisible(true);
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Random random = new Random();
target = random.nextInt(100);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论