很奇怪的一个问题,下面的程序运行时正确的
我想把  sumJTextArea放在点击菜单的时候不出现,而是在点击按钮的时候出现
为什么我public void actionPerformed(ActionEvent e)里改为
      sumJScrollPane.setVisible(false);
     sumJTextArea.setVisible(false);
而在private void okhosJButtonActionPerformed( ActionEvent event )里改为
sumJScrollPane.setVisible(true);
     sumJTextArea.setVisible(true);
结果运行却没有sumJTextArea显示了import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.Box;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;public class sale extends JFrame implements ActionListener {
  JMenuBar mubar;
  JMenu filemu, inputmu, selemu, callmu, helpmu, aboutmu,updatemu; //menu
   JMenuItem selehosmuit, selehospermuit, seleallmuit; //selemuit  JPanel inputJPanel,selectallJPanel,selehospersonJPanel,updtJPanel; // selec se;
  selehosperson sehp;//input windows  JButton okJButton, nextJButton;
  JTextField sumJTextField;
  JLabel nameJLabel, hosJLabel, datefromJLabel, datetoJLabel, sumJLabel;
 
  public sale() {
    createUserInterface();
  }  private void createUserInterface() {
    Container contentPane = getContentPane();
   sehp=new  selehosperson (this);
   inputJPanel=new JPanel();
   
   selectallJPanel=new JPanel();//no use
   updtJPanel=new JPanel();   selehospersonJPanel=new JPanel(); 
  // selectallJPanel.add(se);
   selehospersonJPanel.add(sehp);
  
   
   
   contentPane.add(selehospersonJPanel, BorderLayout.CENTER);
    mubar = new JMenuBar();
    setJMenuBar(mubar);    //setmenu
   
    selemu = new JMenu("查询");
    selemu.setFont(new Font("SansSerif", Font.PLAIN, 15));
  
   
 
    mubar.add(selemu);    selehosmuit = new JMenuItem("按医院查询");
    selehosmuit.setFont(new Font("SansSerif", Font.PLAIN, 15));
   selehospermuit = new JMenuItem("按医院或销售员查询");
   selehospermuit.setFont(new Font("SansSerif", Font.PLAIN, 15));
    selemu.add(selehospermuit);
 
    selehospermuit.addActionListener(sehp);    setTitle("sale"); // set title bar text
    setSize(935, 790); // set window size
    setVisible(true); // display window  }  private void okJButtonactionPerformed(ActionEvent e) {    sumJTextField.setText("");
  }  public void actionPerformed(ActionEvent e) {  }  public static void main(String[] args) {
    sale application = new sale();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  } // end method main
}
//input
   class selehosperson extends JPanel implements ActionListener
      {
     JButton okhosJButton,okperJButton, hosJButton,perJButton;
    JTextArea sumJTextArea;
    JScrollPane sumJScrollPane;
    JLabel nameJLabel, hosJLabel, datefromJLabel, datetoJLabel, sumJLabel;
        sale ss;     selehosperson(sale ss) {
        this.ss = ss;
         createUserInterface();
      }        private void createUserInterface() {
          //input windows          BorderLayout layout = new BorderLayout();
          setLayout(layout);            okhosJButton = new JButton();
       okhosJButton.setText( "fffffffffffffff" );
        okhosJButton.setFont(new Font("SansSerif", Font.PLAIN, 15));
       okhosJButton.setVisible(false);
        okhosJButton.addActionListener(           new ActionListener() 
           {
         
              public void actionPerformed( ActionEvent event )
              {
                 okhosJButtonActionPerformed( event );
              }           }         ); 
                       sumJTextArea = new JTextArea(5, 30);
      // sumJTextArea.setMaximumSize( sumJTextArea.getPreferredSize());
      sumJTextArea.setFont(new Font("SansSerif", Font.PLAIN, 15));
       sumJTextArea.setVisible(false);
     sumJScrollPane=new JScrollPane(sumJTextArea);
    // sumJScrollPane.setPreferredSize(new Dimension(12, 3));
     
     sumJScrollPane.setVisible(false);
 
     Box hbox5= Box.createHorizontalBox();
     Box hbox6= Box.createHorizontalBox();         hbox5.add(Box.createHorizontalStrut(58));
    
       
         hbox5.add( sumJScrollPane );
         hbox5.add(Box.createHorizontalStrut(85));        
         
          hbox6.add(Box.createHorizontalGlue());
          hbox6.add(okhosJButton);
           hbox6.add(Box.createHorizontalStrut(85));
          Box vbox = Box.createVerticalBox();
       
         
     
          vbox.add(Box.createVerticalStrut(10));
          vbox.add(hbox5);
           vbox.add(Box.createVerticalStrut(10));
          vbox.add(hbox6);  
          add(vbox);
              }
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ss.selehospermuit)
      okhosJButton.setVisible(true);
       sumJTextArea.setVisible(true);
  sumJScrollPane.setVisible(true);
     // sumJScrollPane.setVisible(false);
     // sumJTextArea.setVisible(false);
      }
   private void okhosJButtonActionPerformed( ActionEvent event )
     {      
       //sumJScrollPane.setVisible(true);
       //sumJTextArea.setVisible(true);
      sumJTextArea.append("aaaaaaaaaaaaaaa"+"\n");
       
 
;        }
    }

解决方案 »

  1.   

    sumJScrollPane.setVisible(true);
    sumJTextArea.setVisible(true);
    在显示或隐藏组件时,最好调用一下repaint方法.这样有效保证你的更新有效.
      

  2.   

    谢谢JaboR() ,听说过调用repaint
    但是我不知道怎么调用
    直接加在里面吗(直接加没有用的)?还是要写个paint函数private void okhosJButtonActionPerformed( ActionEvent event )
         {      
           sumJScrollPane.setVisible(true);
          sumJTextArea.setVisible(true);
          repaint() ;      sumJTextArea.append("aaaaaaaaaaaaaaa"+"\n");
           
     
    ;        }