Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否使用滚动窗格自动滚动JTextArea?_Java_Swing_Jscrollpane_Jtextarea_Autoscroll - Fatal编程技术网

Java 是否使用滚动窗格自动滚动JTextArea?

Java 是否使用滚动窗格自动滚动JTextArea?,java,swing,jscrollpane,jtextarea,autoscroll,Java,Swing,Jscrollpane,Jtextarea,Autoscroll,我正在尝试构建一个动态日志窗口(基本上是一个自动滚动的jtext区域) 我遇到的问题是,尽管我在文本区域打印了500行,但它显示如下: 下面是我的代码: import java.awt.Dimension; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; i

我正在尝试构建一个动态日志窗口(基本上是一个自动滚动的jtext区域)

我遇到的问题是,尽管我在文本区域打印了500行,但它显示如下:

下面是我的代码:

    import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;


public class Main {

    private static JFrame mainFrame;

    public static void main(String args[]) {
        mainFrame = new JFrame();
        mainFrame.setSize(500, 500);

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ControlPanel cp = new ControlPanel();
        mainFrame.add(cp);
        mainFrame.setVisible(true);
    }
}

class ControlPanel extends JPanel {

private JButton resetButton = new JButton("Reset");

private JPanel logPanel = new JPanel();

private JLabel actionLogsLabel = new JLabel("Action Log");

private JLabel pointsLogsLabel = new JLabel("Points Log");

private JTextArea actionLog = new JTextArea();

private JTextArea pointsLog = new JTextArea();

private JScrollPane actionScroll;

private JScrollPane pointsScroll;

public ControlPanel() {
    init();

    this.add(resetButton);
    this.add(logPanel);
}

private void init() {
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    this.setAlignmentX(LEFT_ALIGNMENT);
    this.logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));
    this.logPanel.setAlignmentX(LEFT_ALIGNMENT);

    actionLog.setPreferredSize(new Dimension(500, 300));
    actionLog.setMaximumSize(actionLog.getPreferredSize());
    actionLog.setEditable(false);
    actionLog.setWrapStyleWord(true);
    DefaultCaret caret = (DefaultCaret) actionLog.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

    pointsLog.setPreferredSize(new Dimension(500, 300));
    pointsLog.setMaximumSize(pointsLog.getPreferredSize());
    pointsLog.setEditable(false);
    pointsLog.setWrapStyleWord(true);

    pointsScroll = new JScrollPane(pointsLog, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    actionScroll = new JScrollPane(actionLog, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    logPanel.add(actionLogsLabel);
    logPanel.add(actionScroll);

    for(int i = 0; i < 500; i++) {
        actionLog.setText(actionLog.getText() + "Line: " + i + "\n");
    }

    logPanel.add(pointsLogsLabel);
    logPanel.add(pointsScroll);
}
}
导入java.awt.Dimension;
导入javax.swing.BoxLayout;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.text.DefaultCaret;
公共班机{
专用静态JFrame主机;
公共静态void main(字符串参数[]){
大型机=新的JFrame();
大型机。设置大小(500500);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ControlPanel cp=新的ControlPanel();
大型机。添加(cp);
mainFrame.setVisible(true);
}
}
类控制面板扩展了JPanel{
私有JButton resetButton=新JButton(“重置”);
private JPanel logPanel=new JPanel();
私有JLabel actionLogsLabel=新JLabel(“操作日志”);
专用JLabel pointsLogsLabel=新JLabel(“点日志”);
私有JTextArea actionLog=新JTextArea();
私有JTextArea pointsLog=新JTextArea();
私有JScrollPane actionScroll;
私有JScrollPane pointsScroll;
公共控制小组(){
init();
此。添加(重置按钮);
添加(日志面板);
}
私有void init(){
this.setLayout(新的BoxLayout(this,BoxLayout.Y_轴));
此.setAlignmentX(左对齐);
this.logPanel.setLayout(新的BoxLayout(logPanel,BoxLayout.Y_轴));
这个.logPanel.setAlignmentX(左对齐);
setPreferredSize(新维度(500300));
actionLog.setMaximumSize(actionLog.getPreferredSize());
actionLog.setEditable(false);
actionLog.setWrapStyleWord(true);
DefaultCaret插入符号=(DefaultCaret)actionLog.getCaret();
插入符号setUpdatePolicy(DefaultCaret.ALWAYS\u UPDATE);
pointsLog.setPreferredSize(新维度(500300));
pointsLog.setMaximumSize(pointsLog.getPreferredSize());
pointsLog.setEditable(假);
pointsLog.setWrapStyleWord(true);
pointsScroll=新的JScrollPane(pointsLog、JScrollPane.VERTICAL\u SCROLLBAR\u根据需要,JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
actionScroll=新建JScrollPane(actionLog,JScrollPane.VERTICAL\u SCROLLBAR\u根据需要,JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
logPanel.add(actionLogsLabel);
添加(actionScroll);
对于(int i=0;i<500;i++){
actionLog.setText(actionLog.getText()+“行:”+i+“\n”);
}
logPanel.add(pointsLogsLabel);
日志面板。添加(点滚动);
}
}
希望有更多挥杆经验的人能抽出时间为我指出正确的方法。

千万不要这样做:

actionLog.setPreferredSize(new Dimension(500, 300));
因为这样做会人为地限制JTextArea的大小,从而导致当前困扰您的效果。还要注意的是,避免在任何东西上设置首选尺寸通常是一个好主意

而是设置JTextARea的列和行计数。这可以通过setter方法或简单的构造函数调用来实现:
JTextArea myTextArea=newjtextarea(行、列)

顺便说一句:我想知道JList是否更适合您


例如:

import javax.swing.*;
import javax.swing.text.DefaultCaret;

public class Main2 {

   private static void createAndShowGUI() {
      JPanel mainPanel = new ControlPanel();

      JFrame frame = new JFrame("Main2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

class ControlPanel extends JPanel {
   private static final int LOG_ROWS = 15;
   private static final int LOG_COLS = 40;
   private JButton resetButton = new JButton("Reset");
   private JPanel logPanel = new JPanel();
   private JLabel actionLogsLabel = new JLabel("Action Log");
   private JLabel pointsLogsLabel = new JLabel("Points Log");
   private JTextArea actionLog = new JTextArea();
   private JTextArea pointsLog = new JTextArea();
   private JScrollPane actionScroll;
   private JScrollPane pointsScroll;

   public ControlPanel() {
      init();
      this.add(resetButton);
      this.add(logPanel);
   }

   private void init() {
      this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
      this.setAlignmentX(LEFT_ALIGNMENT);
      this.logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));
      this.logPanel.setAlignmentX(LEFT_ALIGNMENT);
      // !! actionLog.setPreferredSize(new Dimension(500, 300));
      // !! actionLog.setMaximumSize(actionLog.getPreferredSize());
      actionLog.setRows(LOG_ROWS); // !!
      actionLog.setColumns(LOG_COLS); // !!

      actionLog.setEditable(false);
      actionLog.setWrapStyleWord(true);
      DefaultCaret caret = (DefaultCaret) actionLog.getCaret();
      caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
      // !! pointsLog.setPreferredSize(new Dimension(500, 300));
      // !! pointsLog.setMaximumSize(pointsLog.getPreferredSize());
      pointsLog.setRows(LOG_ROWS); // !!
      pointsLog.setColumns(LOG_COLS); // !!

      pointsLog.setEditable(false);
      pointsLog.setWrapStyleWord(true);
      pointsScroll = new JScrollPane(pointsLog,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      actionScroll = new JScrollPane(actionLog,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      logPanel.add(actionLogsLabel);
      logPanel.add(actionScroll);
      for (int i = 0; i < 500; i++) {
         actionLog.setText(actionLog.getText() + "Line: " + i + "\n");
      }
      logPanel.add(pointsLogsLabel);
      logPanel.add(pointsScroll);
   }
}
import javax.swing.*;
导入javax.swing.text.DefaultCaret;
公共类Main2{
私有静态void createAndShowGUI(){
JPanel mainPanel=新的控制面板();
JFrame=新JFrame(“Main2”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}
类控制面板扩展了JPanel{
私有静态最终int LOG_行=15;
私有静态最终整数对数=40;
私有JButton resetButton=新JButton(“重置”);
private JPanel logPanel=new JPanel();
私有JLabel actionLogsLabel=新JLabel(“操作日志”);
专用JLabel pointsLogsLabel=新JLabel(“点日志”);
私有JTextArea actionLog=新JTextArea();
私有JTextArea pointsLog=新JTextArea();
私有JScrollPane actionScroll;
私有JScrollPane pointsScroll;
公共控制小组(){
init();
此。添加(重置按钮);
添加(日志面板);
}
私有void init(){
this.setLayout(新的BoxLayout(this,BoxLayout.Y_轴));
此.setAlignmentX(左对齐);
this.logPanel.setLayout(新的BoxLayout(logPanel,BoxLayout.Y_轴));
这个.logPanel.setAlignmentX(左对齐);
//!!actionLog.setPreferredSize(新维度(500300));
//!!actionLog.setMaximumSize(actionLog.getPreferredSize());
actionLog.setRows(LOG_ROWS);/!!
actionLog.setColumns(LOG_COLS);/!!
actionLog.setEditable(false);
actionLog.setWrapStyleWord(true);
DefaultCaret插入符号=(DefaultCaret)actionLog.getCaret();
插入符号setUpdatePolicy(DefaultCaret.ALWAYS\u UPDATE);
//!!pointsLog.setPreferredSize(新尺寸(500300));
//!!pointsLog.setMaximumSize(pointsLog.getPreferredSize());
pointsLog.setRows(LOG_ROWS);/!!
pointsLog.setColumns(LOG_COLS);/!!
pointsLog.setEditable(假);
pointsLog.setWrapStyleWord(true);
pointsScroll=新的JScrollPane(pointsLog,
JScrollPane.VERTICAL\u滚动条\u根据需要,
JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
actionScroll=新的JScrollPane(actionLog,
JScrollPane.VERTICAL\u滚动条\u根据需要,
JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
logPanel.add(actionLogsLabel);
添加(actionScroll);
对于(int i=0;i<500;i++){
actionLog.setText(actionLog.getText()+“行:”+i+“\n”);
}
日志面板。添加(点
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

public class Main2B {

   private static void createAndShowGUI() {
      ControlPanel2B controlPanel = new ControlPanel2B();
      controlPanel.setBorder(BorderFactory.createEtchedBorder());

      JPanel mainPanel = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      mainPanel.add(controlPanel, gbc);

      JFrame frame = new JFrame("Main2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

@SuppressWarnings("serial")
class ControlPanel2B extends JPanel {
   private static final int LOG_ROWS = 15;
   private static final int LIST_WIDTH = 500;
   private JButton resetButton = new JButton("Reset");
   private JPanel logPanel = new JPanel();
   private JLabel actionLogsLabel = new JLabel("Action Log");
   private JLabel pointsLogsLabel = new JLabel("Points Log");

   private DefaultListModel<String> actionLogListModel = new DefaultListModel<>();
   private JList<String> actionLogList = new JList<String>(actionLogListModel);
   private DefaultListModel<String> pointsLogListModel = new DefaultListModel<>();
   private JList<String> pointsLogList = new JList<String>(pointsLogListModel);
   private JScrollPane actionScroll;
   private JScrollPane pointsScroll;

   public ControlPanel2B() {
      init();
      this.add(resetButton);
      this.add(logPanel);
   }

   private void init() {
      actionLogList.setVisibleRowCount(LOG_ROWS);
      pointsLogList.setVisibleRowCount(LOG_ROWS);
      actionLogList.setFixedCellWidth(LIST_WIDTH);

      this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
      this.setAlignmentX(LEFT_ALIGNMENT);
      this.logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));
      this.logPanel.setAlignmentX(LEFT_ALIGNMENT);
      pointsScroll = new JScrollPane(pointsLogList,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      actionScroll = new JScrollPane(actionLogList,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      logPanel.add(actionLogsLabel);
      logPanel.add(actionScroll);
      for (int i = 0; i < 500; i++) {
         actionLogListModel.addElement("Line: " + i);
      }
      logPanel.add(pointsLogsLabel);
      logPanel.add(pointsScroll);
   }
}