【问题标题】:How do I change the text in a JTextArea when I press a button?按下按钮时如何更改 JTextArea 中的文本?
【发布时间】:2013-04-24 18:03:13
【问题描述】:

我正在尝试制作一个选择你自己的冒险游戏,当我按下一个按钮时,文本会变为正确的场景。但是,在第 71 行,当我尝试设置 TextArea 的文本时,它显示“线程中的异常“AWT-EventQueue-0”java.lang.Error:未解决的编译问题:无法解决 AdventureArea”。请帮忙?!

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;

public class TheGame extends JPanel implements ActionListener{

/**
 * @param args
 */
private int numClicks1 = 0;
private int numClicks2 = 0;
String gameText = "You wake up in the morning feeling like a sir. As is tradition, you need electronics of any kind to keep your mind running (going outside is out of the question. Nothing exciting in the real world). There are many options available to you, but the internet and games are the ones that appeal the most. Do you want to surf the web or use an app?";

private static final long serialVersionUID = 1L;
 JButton option1;
 JButton option2;

    public TheGame(){
        JPanel buttonPane = new JPanel(new BorderLayout(1,1));
        JPanel textPane = new JPanel(new BorderLayout(1,1));

        option1 = new JButton("Click here for teh interwebs");
         option1.addActionListener(this);
        option1.setPreferredSize(new Dimension(300, 50));
         option1.setVisible(true);

        option2 = new JButton("Click here for teh entertainments");
        option2.addActionListener(this);
         option2.setPreferredSize(new Dimension(300, 50));
         option2.setVisible(true);


        JTextArea adventureArea = new JTextArea();
        adventureArea.setFont(new Font("Serif", Font.PLAIN, 16));
        adventureArea.setLineWrap(true);
        adventureArea.setWrapStyleWord(true);
        adventureArea.setEditable(true);
        adventureArea.setText(gameText);

        JScrollPane adventureScroll = new JScrollPane(adventureArea);
        adventureScroll.setPreferredSize(new Dimension(350, 350));
        adventureScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        Border adventureSpace = BorderFactory.createEmptyBorder(0,10,10,10);
        Border adventureBorder = BorderFactory.createTitledBorder(adventureSpace, "TECHNOLOGY!!!");
        adventureScroll.setBorder(adventureBorder);
        adventureScroll.setVisible(true);


        textPane.add(adventureScroll, BorderLayout.CENTER);
        buttonPane.add(option1,BorderLayout.NORTH);
        buttonPane.add(option2,BorderLayout.SOUTH);


        add(buttonPane, BorderLayout.SOUTH);
        add(textPane, BorderLayout.CENTER);

        setVisible(true);
        buttonPane.setVisible(true);
        textPane.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==option1){
            //numClicks1++;
            //gameText="The internet: A wondrous place full of knowledge, videos, stories, memes, and everything else. Like every place, it has a dark and a light side. Where go?";
            adventureArea.append("The internet: A wondrous place full of knowledge, videos, stories, memes, and everything else. Like every place, it has a dark and a light side. Where go?");
        }else if (e.getSource()==option2){
        numClicks2++;
        };

      /* if(numClicks1==1){
           gameText="The internet: A wondrous place full of knowledge, videos, stories, memes, and everything else. Like every place, it has a dark and a light side. Where go?";
       }else if (numClicks2==1){

       };*/
    }


    private static void createAndShowGUI() {

        JFrame frame = new JFrame("The Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TheGame theContentPane = new TheGame();
        theContentPane.setOpaque(true); 
        frame.setContentPane(theContentPane);

        JFrame.setDefaultLookAndFeelDecorated(true);

        frame.pack();
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }



}

【问题讨论】:

    标签: java swing jbutton jtextarea preferredsize


    【解决方案1】:

    这仅仅是因为adventureArea 是您的构造函数的本地字段,而不是您的类的成员。

    相反,在JButton option2; 的声明下方添加private final JTextArea adventureArea;

    TheGame 的构造函数中,将JTextArea adventureArea = new JTextArea(); 替换为this.adventureArea = new JTextArea();

    旁注:

    • 切勿使用setPreferredSize(),而是使用适当的LayoutManager 或提供您想要达到的目标的提示。例如,您的按钮可以放置在带有GridLayoutJPanel 中(这样它们的大小相同),然后您将该面板放在BorderLayoutSOUTH 中。对于JTextArea,请指明您想要的行数和列数(例如,24 行 x 80 列):new JTextArea(24, 80);。这将自动传播到滚动窗格,然后传播到父窗口。
    • 尝试正确缩进您的代码,这样可以避免愚蠢的错误并使其他人更容易阅读您的代码。
    • 不要同时调用frame.pack();frame.setSize(800, 600);,最后一个会赢。支持pack() 而不是setSize()
    • 所有组件默认可见,无需调用setVisible(true)。只有顶级容器 (Windows) 需要显式显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2019-08-05
      • 2017-12-28
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多