获取null / empty JtextArea

时间:2014-11-09 16:34:07

标签: swing jframe jtextarea swingworker

我创建了一个框架,里面有一个面板,面板里面有一个textarea。现在我创建了一个构造函数,使框架可见一段时间后,它被设置为不可见。它可见的时间显示了一些按摩。

当我在outputDisplay类的main方法中运行构造函数代码时,它会显示文本按钮

Get this output when runs inside the same method

Getting a empty frame when run it from other class by using new keyword

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class OutputDisplay {

static JFrame frame;
JPanel  panel;
JTextArea area;
Font font;

    public void timer(int time){
        Timer timer = new Timer(time, new ActionListener(){
            public void actionPerformed(ActionEvent e){
                frame.setVisible(false);
            }
        });
        timer.start();
        timer.setRepeats(false);
    }

    OutputDisplay(String ip,int time) throws InterruptedException{
    frame = new JFrame("Warning");
    frame.setLocation(400, 220);
    panel = new JPanel();
    area = new JTextArea();
    font = new Font("Aharoni", Font.BOLD, 16);
    area.setFont(font);
    area.setForeground(Color.RED);
    area.setSize(200, 200);
    int j=0;
    String[] t = {ip}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(600, 200);
    frame.setVisible(true);
    Thread.sleep(time);
    j++;
    if(j==1){
        frame.setVisible(false);
    }//if
    frame.setResizable(false);
    }//constructor

    OutputDisplay(String dialogue,String path,int time) throws InterruptedException{
    frame = new JFrame("Warning");
    frame.setLocation(400, 220);
    panel = new JPanel();
    area = new JTextArea();
    font = new Font("Aharoni", Font.BOLD, 16);
    area.setFont(font);
    area.setForeground(Color.MAGENTA);
    area.setSize(200, 200);
    int j=0;
    String[] t = {dialogue}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(500, 200);
    frame.setVisible(true);
    Thread.sleep(time);
    j++;
    if(j==1){
        frame.setVisible(false);
    }//if
    frame.setResizable(false);
    }//constructor

    OutputDisplay(String dialogue) throws InterruptedException{
    frame = new JFrame("Report");
    frame.setLocation(400, 220);
    panel = new JPanel();
    area = new JTextArea();
    font = new Font("Aharoni", Font.BOLD, 16);
    area.setFont(font);
    area.setForeground(Color.BLUE);
    area.setSize(200, 200);
    String[] t = {dialogue}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setResizable(false);
    }//constructor

   public OutputDisplay() {
    }//no arg

public void dialogue (String massage) throws InterruptedException{
        frame = new JFrame("Massage");
        frame.setLocation(400, 220);
        JPanel panel = new JPanel();
        JTextArea area = new JTextArea();
        Font font = new Font("Aharoni", Font.BOLD, 16);
        area.setFont(font);
        area.setForeground(Color.GREEN);
        area.setSize(200, 200);
        int j=0;
        String[] t = {massage}; 
        for(int i=0;i<t.length;i++){
            area.append(t[i]+"\n");
        }//for
        panel.add(area);
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.pack();
        frame.setSize(400, 100);
        frame.setVisible(true);
        Thread.sleep(8*1000);
        j++;
        if(j==1){
            frame.setVisible(false);
        }//if
        frame.setResizable(false);
    }//dialogue

   static void warningPopUp (String massage){
       JOptionPane.showMessageDialog(null, massage);
    }//dialogue

   public static void main(String[] args){
   new OutputDisplay("We are checking for account validation"+"\n"
                + "If user id and password matches then we will goto websites for updation. "    +"\n"
            + "You will get a complete report once we are done", 4*1000);


   }//main
 }//OutputDisplay

1 个答案:

答案 0 :(得分:5)


修改
你还在使用Thread.sleep,你不应该再使用它,而应该使用Timer。此外,永远不要设置JTextArea的大小或首选大小。这样做,JScrollPane的滚动条将无法正常工作。而是设置其行和列。

import java.awt.Color;
import java.awt.Font;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TimeLimitedDialogTester {
   private static final int EB_GAP = 25;
   private JPanel mainPanel = new JPanel();

   public TimeLimitedDialogTester() {
      mainPanel.add(new JButton(new ShowDialogAction("Show Dialog")));
      mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, 2 * EB_GAP, 2 * EB_GAP, 2 * EB_GAP));
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   @SuppressWarnings("serial")
   private class ShowDialogAction extends AbstractAction {
      public ShowDialogAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         Window mainWin = SwingUtilities.getWindowAncestor(mainPanel);
         int seconds = 5;
         String dialogTitle = "WARNING";
         String text = "We are checking for account validation. If user id and password matches then we"
               + " will go to websites for updating. You will get a complete report once we are done.";
         TimeLimitedDialog timeLimitedDialog = new TimeLimitedDialog(mainWin, seconds, dialogTitle, text);
         timeLimitedDialog.dialogShow();         
      }
   }

   private static void createAndShowGui() {
      TimeLimitedDialogTester timeLimitedDialog = new TimeLimitedDialogTester();

      JFrame frame = new JFrame("TimeLimitedDialog");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(timeLimitedDialog.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class TimeLimitedDialog {
   private static final int ROWS = 6;
   private static final int COLS = 30;
   private static final Color FG = Color.green.darker().darker();
   private JDialog dialog;
   private JTextArea displayArea = new JTextArea(ROWS, COLS);
   private int seconds;

   public TimeLimitedDialog(Window mainWin, int seconds, String dialogTitle, String text) {
      displayArea.setWrapStyleWord(true);
      displayArea.setLineWrap(true);
      displayArea.setFocusable(false);
      displayArea.setText(text);
      displayArea.setForeground(FG);
      displayArea.setFont(displayArea.getFont().deriveFont(Font.BOLD));

      this.seconds = seconds;
      dialog = new JDialog(mainWin, dialogTitle, ModalityType.APPLICATION_MODAL);
      dialog.add(new JScrollPane(displayArea));
      dialog.pack();
      dialog.setLocationRelativeTo(mainWin);
   }

   public void dialogShow() {
      new Timer(seconds * 1000, new TimerListener()).start();
      dialog.setVisible(true);
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         if (dialog != null && dialog.isVisible()) {
            dialog.dispose();
         }
         ((Timer) e.getSource()).stop();
      }
   }
}