【问题标题】:String variable will not display is will not display in JFrame字符串变量不会显示在 JFrame 中不会显示
【发布时间】:2014-09-05 05:11:31
【问题描述】:

当我运行这个程序时,变量String result 不会显示在JPanel outpanel 中。有人能告诉我为什么吗?我已经尝试使用谷歌搜索和尝试static 等几个小时,但我相信static 不是面向对象的答案,即使它确实解决了它。

import java.util.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;

public class AProgram extends JFrame 
            implements WindowListener, ActionListener  {

    public JButton gbt = new JButton("GO");
    public JPanel outpanel = new JPanel();
    public ArrayList<String> responses;
    public String result;

    public AProgram() {
        super ("Program");
    }

    public void init() {
        //output
        outpanel.setOpaque(true);
        outpanel.setPreferredSize(new Dimension(600, 75)); 
        outpanel.setBorder(new LineBorder(Color.black, 2));       
        JLabel jl = new JLabel();    
        jl.setText(result);
        outpanel.add(jl);
        this.add("South", outpanel);

        //press-button - does NOT sit in a panel.
        gbt.setBackground(Color.GREEN);
        gbt.setPreferredSize(new Dimension(75, 75));
        gbt.setBorder(new LineBorder(Color.black, 2)); 
        this.add("East", gbt);

        //all
        gbt.addActionListener(this);
        this.addWindowListener(this);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }

    public void actionPerformed (ActionEvent evt) {
        String input = "A Lion is ";
        finishSentence(input);
    }

    public void windowClosing (WindowEvent e) {
        this.setVisible(false);
        this.dispose();
    }

    public void windowOpened(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}

    public String finishSentence(String inwds) {
        responses = new ArrayList<String>();
        responses.add(0, "a bookish creature");
        responses.add(1, "quiet and unassuming");
        responses.add(2, "King of the Jungle");

        String result = inwds + responses.get(2);

        return result;
    }

    public static void main (String[] args) {
        AProgram me = new AProgram();
        me.init();
    }
}

【问题讨论】:

  • 使用逻辑一致的代码格式风格!代码缩进旨在帮助人们遵循程序流程。
  • 请记住,可以简单地使用frameInstance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE),而不是覆盖WindowListener 方法,以达到与windowClosing() 方法相同的效果。这 1 行相当于那 10 行 :-)

标签: java swing arraylist jframe


【解决方案1】:

在您的init() 方法中,您将jl 的文本设置为null 的结果。

然后在您的actionPerformed() 中调用方法finishSentence(),它返回一个String,但您没有将它分配给任何变量或元素。

在您的actionPerformed() 中再次调用jl.setText()

public void actionPerformed (ActionEvent evt)  {

        String input = "A Lion is ";

        jl.setText(finishSentence(input));                                        

        }

【讨论】:

  • :D(以及制作我的 JLabel 类级数据)它可以工作。哦,天哪,谢谢。我已经坚持了大约3个小时。 !
  • 如果它有助于解决问题,请accept回答。我还建议查看您的 earlier questions 并为这些选择一个答案。
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多