2014-03-06 95 views
0

每当我在我的Java GUI中按“清除”,它都无法工作,请帮我完成此操作。如果我用另一个按钮替换“textPanel”,它会起作用,否则用“textpanel”它不会。textPanel.setText(“”);无法正常工作或显示

这里是我的代码的轻量级版本演示问题:

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

public class MainFrame extends JFrame { 
    private TextPanel textPanel; 
    private FormPanel formpanel; 

    public MainFrame(){ 
     super("My Frame"); 
     createLayout(); 
     createFrame(); 
    } 

    public void createFrame(){ 
     setSize(600, 600); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true);  
    } 

    public void createLayout(){ 
     BorderLayout myLayout = new BorderLayout();  
     setLayout(myLayout); 

     textPanel = new TextPanel(); 
     formpanel = new FormPanel(); 

     // adding components 
     add(textPanel, BorderLayout.CENTER); 
     add(formpanel, BorderLayout.WEST); 
    } 

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

    public static class FormPanel extends JPanel { 

     private JButton clear; 
     private TextPanel textPanel; 

     public FormPanel(){ 
      clear = new JButton("Clear Cart!"); 
      textPanel=new TextPanel(); 
      add(clear); 

      clear.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent aev){ 
        System.out.println("Test"); 
        textPanel.setText(""); 
       }  
      }); 
      createGrid();    
     } 

     /* this methods simply creates the layout */ 
     void createGrid(){ 
      //creating layout 
      setLayout(new GridBagLayout());  
      GridBagConstraints gc = new GridBagConstraints(); 

      gc.gridy++; 
      gc.weightx = .1; 
      gc.weighty = .1; 
      gc.gridx = 2; 
      gc.gridy=5; 
      gc.anchor = GridBagConstraints.LINE_START; 
      gc.insets = new Insets(0,0,0,0); 

      add(clear, gc); 

     } 
    } 

    public static class TextPanel extends JPanel { 
     private JTextArea textArea; 

     TextPanel(){ 
      textArea = new JTextArea(); 
      setLayout(new BorderLayout()); 

      JScrollPane p = new JScrollPane(textArea); 
      add(p, BorderLayout.CENTER); 
     } 

     public void appendSomeText(String t){ 
      textArea.append(t);  
     } 

     public void setText(String s){ 
      textArea.setText(s); 
     } 
    } 
} 
+2

你的问题是什么? –

+0

如何让我的清除按钮实际上清除文本面板? – user3389471

+2

对代码的过度杀伤,请将此代码转储更改为[SSCCE](http://www.sscce.org/),但没有人希望查看所有这些内容。 – turbo

回答

4

你有TextPanel两种情况,一种在MainFrame,另一个在FormPanel。在FormPanel中定义的TextPanel实际上并未添加到面板中,因此textPanel.setText("");对其没有影响,因为它不可见。

当文本附加了Add To Cart!按钮时,它实际上会通过MainFrame - formEventOccurred()中的方法执行textPanel.appendSomeText()。这是TextPanel的另一个实例,它是MainFrame的一部分,实际上它是可见的。

看起来您需要将复制的逻辑从主框架移动到面板。通常你不应该扩展JFrame,因为你没有添加任何新功能。

+0

但是,当我摆脱textPanel = new TextPanel();从FormPanel我得到一个错误在我的textPanel.setText(“”);说java.lang.NullPointerException异常 – user3389471

+0

当我从MainFrome中删除它时,程序混乱并且不显示任何按钮或面板。 – user3389471

+0

@ user3389471毫无疑问,您需要重构和重新组织您的程序。就像现在一样,你不能简单地评论一行并使其工作。清理'MainFrame',它只是一个顶级容器。它应该包含你的面板但不包含动作逻辑。查看[如何使用操作](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html),以便在面板和工具栏中分离和重用功能。 – tenorsax