Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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 JSplitPane不显示项目_Java_Swing_Jsplitpane - Fatal编程技术网

Java JSplitPane不显示项目

Java JSplitPane不显示项目,java,swing,jsplitpane,Java,Swing,Jsplitpane,我正在写一个工具,在文本文件上执行任务。这项任务需要一些时间来执行,所以我制作了一个面板,显示文件名和进度百分比。用户可以在一个或多个文件上运行任务,因此我需要为每个文件显示一个面板。 在我得到的帮助下,我得到了这个代码,它显示文本区域并添加面板。问题在于,当添加新项目时,textarea和panel列表都会以牺牲对方为代价而增长。您可以在添加行或单击“新建”按钮并添加面板时看到这种情况: import java.awt.event.*; import javax.swing.*; import

我正在写一个工具,在文本文件上执行任务。这项任务需要一些时间来执行,所以我制作了一个面板,显示文件名和进度百分比。用户可以在一个或多个文件上运行任务,因此我需要为每个文件显示一个面板。
在我得到的帮助下,我得到了这个代码,它显示文本区域并添加面板。问题在于,当添加新项目时,textarea和panel列表都会以牺牲对方为代价而增长。您可以在添加行或单击“新建”按钮并添加面板时看到这种情况:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.logging.Logger;

public class FProgressDisplay extends JFrame {
    private final static Logger LOGGER = Logger.getLogger(FProgressDisplay.class.getName());
    private List<PanelTaskProgress> tasks;
    JTextArea txtLog;
    JButton btnNew;
    JButton btnAbort;
    JButton btnClose;
    static int i;
    JPanel taskPanel;

    public static void main(String[] args) {
        try {
            FProgressDisplay frame = new FProgressDisplay();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to initialize application.");
        }
    }
    /**
     * Create the frame.
     */
    public FProgressDisplay() {
        setTitle("Mask tool - Progress");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // should be done AFTER components are added
        //pack();
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        taskPanel = new JPanel();
        taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        getContentPane().add(panel);

        btnNew = new JButton("New");
        panel.add(btnNew);
        btnNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                addTask(++i, "Task " + i);
            }
        });

        btnAbort = new JButton("Abort");
        panel.add(btnAbort);

        btnClose = new JButton("Close");
        panel.add(btnClose);

        txtLog = new JTextArea(10,0);
        txtLog.setLineWrap(true);
        getContentPane().add(txtLog);

        tasks = new ArrayList<PanelTaskProgress>();

        JScrollPane scrollPane = new JScrollPane(taskPanel);
        getContentPane().add(scrollPane);

        for(i = 0; i < 10; i++) {
            addTask(i, "Task"+i);
        }
        pack();
    }

    public void addTask(long id, String fileName) {
        PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName);
        tasks.add(newTaskPanel);
        taskPanel.add(newTaskPanel);
        validate();
        //repaint();
        LOGGER.info("Added new panel");
    }

    public class PanelTaskProgress extends JPanel {
        private static final long serialVersionUID = 1L;
        JLabel lblTaskDescription;
        JLabel lblProgress;
        private long id;
        /**
         * Create the panel.
         */
        public PanelTaskProgress(long id, String fileName) {
            try {
                //setLayout(null);

                lblTaskDescription = new JLabel(id + " " + fileName);
                //lblTaskDescription.setPreferredSize(new Dimension(632, 14));
                add(lblTaskDescription);

                lblProgress = new JLabel("0%");
                lblProgress.setHorizontalAlignment(SwingConstants.CENTER);
                //lblProgress.setBounds(664, 11, 51, 14);
                add(lblProgress);

                LOGGER.info("Created new panel; Id: " + id + "; File: " + fileName);
            } catch (Exception e) {
                LOGGER.severe("Error creating new panel; " + e.getMessage());
            }
        }
    }
}
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
导入java.util.logging.Logger;
公共类FProgressDisplay扩展了JFrame{
私有最终静态记录器Logger=Logger.getLogger(FProgressDisplay.class.getName());
私有列表任务;
JTextArea txtLog;
JButton Btnew;
JButton-btnAbort;
JButton-btnClose;
静态int-i;
JPanel任务小组;
公共静态void main(字符串[]args){
试一试{
FProgressDisplay frame=新的FProgressDisplay();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
抛出新的RuntimeException(“未能初始化应用程序”);
}
}
/**
*创建框架。
*/
公共FProgressDisplay(){
setTitle(“遮罩工具-进度”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//应在添加组件后执行
//包装();
getContentPane().setLayout(新的BoxLayout(getContentPane(),BoxLayout.Y_轴));
taskPanel=新的JPanel();
taskPanel.setLayout(新的BoxLayout(taskPanel,BoxLayout.Y_轴));
JPanel面板=新的JPanel();
getContentPane().add(面板);
btnNew=新的按钮(“新”);
面板。添加(btnNew);
btnNew.addActionListener(新的ActionListener(){
已执行的公共无效行动(行动事件ae){
添加任务(++i,“任务”+i);
}
});
btnAbort=新的JButton(“中止”);
面板。添加(btnAbort);
btnClose=新按钮(“关闭”);
panel.add(btnClose);
txtLog=新的JTextArea(10,0);
txtLog.setLineWrap(true);
getContentPane().add(txtLog);
tasks=newarraylist();
JScrollPane scrollPane=新的JScrollPane(任务面板);
getContentPane().add(滚动窗格);
对于(i=0;i<10;i++){
添加任务(i,“任务”+i);
}
包装();
}
public void addTask(长id,字符串文件名){
PanelTaskProgress newTaskPanel=新的PanelTaskProgress(id,文件名);
添加(newTaskPanel);
taskPanel.add(newTaskPanel);
验证();
//重新油漆();
LOGGER.info(“新增面板”);
}
公共类PanelTaskProgress扩展了JPanel{
私有静态最终长serialVersionUID=1L;
JLabel lbltaskd描述;
JLabel-lblProgress;
私人长id;
/**
*创建面板。
*/
公共面板任务进度(长id,字符串文件名){
试一试{
//setLayout(空);
lbltaskddescription=新的JLabel(id+“”+文件名);
//lbltaskddescription.setPreferredSize(新维度(632,14));
添加(lbltaskddescription);
lblProgress=新的JLabel(“0%”);
lblProgress.setHorizontalAlignment(SwingConstants.CENTER);
//lblProgress.立根(664,11,51,14);
添加(lblProgress);
info(“创建的新面板;Id:+Id+”;文件:+fileName);
}捕获(例外e){
LOGGER.severy(“创建新面板时出错;”+e.getMessage());
}
}
}
}
我希望每个都保持在自己的区域,并添加滚动如果需要。我试图在上面的示例中添加一个JSplitPane,但两个窗格都是空的

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

import java.util.*;
import java.util.logging.Logger;

public class FProgressDisplay extends JFrame {
    private final static Logger LOGGER = Logger
            .getLogger(FProgressDisplay.class.getName());
    private List<PanelTaskProgress> tasks;
    JTextArea txtLog;
    JButton btnNew;
    JButton btnAbort;
    JButton btnClose;
    static int i;
    JPanel taskPanel;

    private JSplitPane splitPane;

    public static void main(String[] args) {
        try {
            FProgressDisplay frame = new FProgressDisplay();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to initialize application.");
        }
    }

    /**
     * Create the frame.
     */
    public FProgressDisplay() {
        setTitle("Mask tool - Progress");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // should be done AFTER components are added
        // pack();
        getContentPane().setLayout(
                new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        taskPanel = new JPanel();
        taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        getContentPane().add(panel);

        btnNew = new JButton("New");
        panel.add(btnNew);
        btnNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                addTask(++i, "Task " + i);
            }
        });

        btnAbort = new JButton("Abort");
        panel.add(btnAbort);

        btnClose = new JButton("Close");
        panel.add(btnClose);

        txtLog = new JTextArea(10, 0);
        txtLog.setLineWrap(true);
        //getContentPane().add(txtLog);

        tasks = new ArrayList<PanelTaskProgress>();

        JScrollPane scrollPane = new JScrollPane(taskPanel);
        //getContentPane().add(scrollPane);
        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, txtLog, scrollPane);
        splitPane.setDividerLocation(150);

        for (i = 0; i < 10; i++) {
            addTask(i, "Task" + i);
        }
        pack();
    }

    public void addTask(long id, String fileName) {
        PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName);
        tasks.add(newTaskPanel);
        taskPanel.add(newTaskPanel);
        validate();
        // repaint();
        LOGGER.info("Added new panel");
    }

    public class PanelTaskProgress extends JPanel {
        private static final long serialVersionUID = 1L;
        JLabel lblTaskDescription;
        JLabel lblProgress;
        private long id;

        /**
         * Create the panel.
         */
        public PanelTaskProgress(long id, String fileName) {
            try {
                // setLayout(null);

                lblTaskDescription = new JLabel(id + " " + fileName);
                // lblTaskDescription.setPreferredSize(new Dimension(632, 14));
                add(lblTaskDescription);

                lblProgress = new JLabel("0%");
                lblProgress.setHorizontalAlignment(SwingConstants.CENTER);
                // lblProgress.setBounds(664, 11, 51, 14);
                add(lblProgress);

                LOGGER.info("Created new panel; Id: " + id + "; File: "
                        + fileName);
            } catch (Exception e) {
                LOGGER.severe("Error creating new panel; " + e.getMessage());
            }
        }
    }
}
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.plaf.SplitPaneUI;
导入java.util.*;
导入java.util.logging.Logger;
公共类FProgressDisplay扩展了JFrame{
专用最终静态记录器=记录器
.getLogger(FProgressDisplay.class.getName());
私有列表任务;
JTextArea txtLog;
JButton Btnew;
JButton-btnAbort;
JButton-btnClose;
静态int-i;
JPanel任务小组;
私有JSplitPane拆分窗格;
公共静态void main(字符串[]args){
试一试{
FProgressDisplay frame=新的FProgressDisplay();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
抛出新的RuntimeException(“未能初始化应用程序”);
}
}
/**
*创建框架。
*/
公共FProgressDisplay(){
setTitle(“遮罩工具-进度”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//应在添加组件后执行
//包装();
getContentPane().setLayout(
新的BoxLayout(getContentPane(),BoxLayout.Y_轴));
taskPanel=新的JPanel();
taskPanel.setLayout(新的BoxLayout(taskPanel,BoxLayout.Y_轴));
JPanel面板=新的JPanel();
getContentPane().add(面板);
btnNew=新的按钮(“新”);
面板。添加(btnNew);
btnNew.addActionListener(新的ActionListener(){
已执行的公共无效行动(行动事件ae){
添加任务(++i,“任务”+i);
}
});
btnAbort=新的JButton(“中止”);
面板。添加(btnAbort);
btnClose=新按钮(“关闭”);
panel.add(btnClose);
txtLog=新的JTextArea(10,0);
txtLog.setLineWrap(true);
//getContentPane().add(txtLog);
tasks=newarraylist();
package layout.sscce;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.logging.Logger;

public class FProgressDisplay extends JFrame {
    private final static Logger LOGGER = Logger
            .getLogger(FProgressDisplay.class.getName());
    private List<PanelTaskProgress> tasks;
    JTextArea txtLog;
    JButton btnNew;
    JButton btnAbort;
    JButton btnClose;
    static int i;
    JPanel taskPanel;

    private JSplitPane splitPane;

    public static void main(String[] args) {
        try {
            FProgressDisplay frame = new FProgressDisplay();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to initialize application.");
        }
    }

    /**
     * Create the frame.
     */
    public FProgressDisplay() {
        setTitle("Mask tool - Progress");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // should be done AFTER components are added
        // pack();
//        getContentPane().setLayout(
//                new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        getContentPane().setLayout(
                new BorderLayout());

        taskPanel = new JPanel();
        taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS));

        JPanel buttonPanel = new JPanel();
        getContentPane().add(buttonPanel);

        btnNew = new JButton("New");
        buttonPanel.add(btnNew);
        btnNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                addTask(++i, "Task " + i);
            }
        });

        btnAbort = new JButton("Abort");
        buttonPanel.add(btnAbort);

        btnClose = new JButton("Close");
        buttonPanel.add(btnClose);

        txtLog = new JTextArea(10, 30);
        txtLog.setLineWrap(true);
        //getContentPane().add(txtLog);

        tasks = new ArrayList<PanelTaskProgress>();

        JScrollPane taskScrollPane = new JScrollPane(taskPanel);
        JScrollPane textScrollPane = new JScrollPane(txtLog);

        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textScrollPane, taskScrollPane);
        splitPane.setDividerLocation(150);

        for (i = 0; i < 10; i++) {
            addTask(i, "Task" + i);
        }



        getContentPane().add(buttonPanel, BorderLayout.NORTH);
        getContentPane().add(splitPane, BorderLayout.CENTER);

        pack();
    }

    public void addTask(long id, String fileName) {
        PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName);
        tasks.add(newTaskPanel);
        taskPanel.add(newTaskPanel);
        validate();
        // repaint();
        LOGGER.info("Added new panel");
    }

    public class PanelTaskProgress extends JPanel {
        private static final long serialVersionUID = 1L;
        JLabel lblTaskDescription;
        JLabel lblProgress;
        private long id;

        /**
         * Create the panel.
         */
        public PanelTaskProgress(long id, String fileName) {
            try {
                // setLayout(null);

                lblTaskDescription = new JLabel(id + " " + fileName);
                // lblTaskDescription.setPreferredSize(new Dimension(632, 14));
                add(lblTaskDescription);

                lblProgress = new JLabel("0%");
                lblProgress.setHorizontalAlignment(SwingConstants.CENTER);
                // lblProgress.setBounds(664, 11, 51, 14);
                add(lblProgress);

                LOGGER.info("Created new panel; Id: " + id + "; File: "
                        + fileName);
            } catch (Exception e) {
                LOGGER.severe("Error creating new panel; " + e.getMessage());
            }
        }
    }
}
        txtLog = new JTextArea(10, 30); // changed
        txtLog.setLineWrap(true);
        getContentPane().add(txtLog);

        tasks = new ArrayList<PanelTaskProgress>();

        JScrollPane scrollPane = new JScrollPane(taskPanel);
        scrollPane.setPreferredSize( txtLog.getPreferredSize() ); // added