Swing 获取空/空JtextArea

Swing 获取空/空JtextArea,swing,jframe,jtextarea,swingworker,Swing,Jframe,Jtextarea,Swingworker,我用板条箱装了一个框架,里面有一个面板,面板里面有一个文本区域。现在,我创建了一个构造器,使框架在一段时间内可见,然后将其设置为不可见。在可见的时间内,它显示一些按摩 当我在outputDisplay类的main方法中运行构造函数代码时,它会显示文本消息 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.

我用板条箱装了一个框架,里面有一个面板,面板里面有一个文本区域。现在,我创建了一个构造器,使框架在一段时间内可见,然后将其设置为不可见。在可见的时间内,它显示一些按摩

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

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

导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入javax.swing.Timer;
公共类输出显示{
静态JFrame;
JPanel小组;
jtext面积;
字体;
公共无效计时器(整数时间){
计时器计时器=新计时器(时间,新ActionListener(){
已执行的公共无效操作(操作事件e){
frame.setVisible(假);
}
});
timer.start();
timer.setRepeats(假);
}
OutputDisplay(字符串ip,整数时间)抛出InterruptedException{
框架=新的JFrame(“警告”);
帧设置位置(400220);
panel=新的JPanel();
面积=新的JTextArea();
font=新字体(“Aharoni”,font.BOLD,16);
area.setFont(字体);
设置前景(颜色为红色);
面积。设置尺寸(200200);
int j=0;
字符串[]t={ip};
对于(int i=0;i
  • 不要在Swing EDT上调用
    Thread.sleep(…)
    ,因为这只会让整个GUI进入睡眠状态
  • 一定要用一个字母来代替
  • 在谷歌上搜索类似的问题
  • 请退房
  • 还可以查看

编辑
您仍然在使用Thread.sleep,同样,您不应该使用它,而应该使用计时器。此外,永远不要设置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();
      }
   }
}

先生,你能给我一个示例代码来指导我怎么做吗?我自己试过了,但没有成功so@SwetaSingh:我试图使用您的代码创建一个示例,但您的代码没有main方法,无法运行。请修复它。我在其中放入了main方法。@SwetaSingh:再次,您仍然在使用
线程。sleep
,再次不应该使用它,而是应该使用一个计时器。你的类在一个你从未调用过的方法中有一些Swing Timer代码。解决这个问题——去掉
线程。睡眠
并使用你的Swing Timer。