Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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 JPanel内部的JScrollPane内部的GridBagLayout不';不能正常滚动_Java_Swing - Fatal编程技术网

Java JPanel内部的JScrollPane内部的GridBagLayout不';不能正常滚动

Java JPanel内部的JScrollPane内部的GridBagLayout不';不能正常滚动,java,swing,Java,Swing,我有一个JPanel在JScrollPane在GridBagLayout 这是不正确的滚动。应该发生的是,每次 按下xxx按钮,滚动窗格内会添加一行新行。 实际情况是,如果按下xxx,比如说10次,只有 显示前七行,其余的无法滚动到。谁能 建议对下面的源代码进行更改,以使滚动正常进行 适当地?我花了几个小时在这上面,但没有成功,我尝试了所有的策略 通过网络 注: 文本在paintComponent中拆分,因为drawString会 不处理行尾字符 JPanel内部的JScrollPane内部的G

我有一个
JPanel
JScrollPane
GridBagLayout
这是不正确的滚动。应该发生的是,每次 按下xxx按钮,滚动窗格内会添加一行新行。 实际情况是,如果按下xxx,比如说10次,只有 显示前七行,其余的无法滚动到。谁能 建议对下面的源代码进行更改,以使滚动正常进行 适当地?我花了几个小时在这上面,但没有成功,我尝试了所有的策略 通过网络

注:

  • 文本在
    paintComponent
    中拆分,因为
    drawString
    会 不处理行尾字符
  • JPanel
    内部的
    JScrollPane
    内部的
    GridBagLayout
    配置是一个更大系统的必要组成部分 一个有同样问题的软件,所以我把它保存在这里
  • 谢谢

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class question {
    
        public static void main(String[] args) {
            new DrawingGUI();
        }
    
        private static class ScrollingPane extends JPanel {
            private static final long serialVersionUID = 1L;
    
            String text;
    
            public ScrollingPane() {
                setPreferredSize(new Dimension(300, 100));
                text = "";
            }
    
            public void SetText(String text_x) {text = text + System.lineSeparator() + text_x;}
    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                FontMetrics fm = g.getFontMetrics();
    
                int y = -fm.getHeight();
                for (String text : text.split("\n"))
                    g.drawString(text,  0,  y += fm.getHeight());
            }
        }
    
        static class DrawingGUI extends JFrame implements ActionListener {
            private static final long serialVersionUID = 1L;
    
            JPanel jp;
            JScrollPane js;
            ScrollingPane sp;
    
            int LineNum;
    
            DrawingGUI() {
                LineNum = 0;
                JFrame frame = new JFrame("xxx");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
                addComponentsToPane(frame.getContentPane());
    
                frame.setSize(800,800);
                frame.setVisible(true);
            }
    
            public void addComponentsToPane(Container pane) {
                pane.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
    
                JButton button = new JButton("xxx");
                button.setActionCommand("add_text");
                button.addActionListener(this);
                c.gridx = 0;
                c.gridy = 0;
                pane.add(button, c);
    
                jp = new JPanel();
                c.gridx = 0;
                c.gridy = 1;
                c.ipadx = 600;
                c.ipady = 450;
                pane.add(jp, c);
    
                sp = new ScrollingPane();
    
                js = new JScrollPane(sp);
                js.getViewport().setPreferredSize(new Dimension(300,100));
                c.gridx = 0;
                c.gridy = 2;
                c.ipady = 50;
                pane.add(js, c);
            }
    
            public void actionPerformed(ActionEvent e) {
                if ("add_text".equals(e.getActionCommand())) {
                    LineNum++;
                    sp.SetText("LineNum = " + LineNum);
                    sp.revalidate();
                    sp.repaint();
                }
            }
        }
    
    }
    

    您的ScrollingPane JPanel无法使用以下行正确调整自身大小:

    setPreferredSize(new Dimension(300, 100));
    
    这将修复滚动窗格JPanel的大小。我发现您有几种可能的解决方案:

  • 困难:覆盖ScrollingPane JPanel的getPreferredSize(),并使用FontMetrics根据其保存和绘制的文本大小计算适当的首选大小
  • 更简单:不要在添加文本时添加文本,而是让ScrollingPane使用GridLayout(0,1)(一列,行数可变),并在需要添加新文本时向ScrollingPane添加JLabel。然后对其调用
    revalidate()
    repaint()
  • 更简单的是:不要使用滚动窗格JPanel,而是使用JTextArea,它看起来像JPanel,不能编辑。将其添加到JScrollPane中,同样,不要限制其大小
  • 最简单:只需使用JList,因为这是您在此处使用的功能 例如,这两种方法中的任何一种都会起作用,并且看起来很相似:

    private static class ScrollingPane2 extends JPanel {
        private static final long serialVersionUID = 1L;
        private JTextArea textArea = new JTextArea(6, 20);
    
        public ScrollingPane2() {
            setLayout(new BorderLayout());
            add(textArea, BorderLayout.CENTER);
            textArea.setEditable(false);
            textArea.setFocusable(false);
            textArea.setBackground(null);
        }
    
        public void SetText(String text_x) {
            textArea.append(text_x + "\n");
        }
    }
    
    private static class ScrollingPane3 extends JPanel {
        private static final long serialVersionUID = 1L;
        private DefaultListModel<String> listModel = new DefaultListModel<>();
        private JList<String> jList = new JList<>(listModel);
    
        public ScrollingPane3() {
            setLayout(new BorderLayout());
            add(jList, BorderLayout.CENTER);
            jList.setBackground(null);
        }
    
        public void SetText(String text_x) {
            listModel.addElement(text_x);
        }
    }
    
    私有静态类ScrollingPane2扩展了JPanel{
    私有静态最终长serialVersionUID=1L;
    私有JTextArea textArea=新JTextArea(6,20);
    公共滚动窗格2(){
    setLayout(新的BorderLayout());
    添加(textArea,BorderLayout.CENTER);
    textArea.setEditable(false);
    textArea.setFocusable(false);
    textArea.setBackground(空);
    }
    公共void SetText(字符串text_x){
    textArea.append(text_x+“\n”);
    }
    }
    私有静态类ScrollingPane3扩展了JPanel{
    私有静态最终长serialVersionUID=1L;
    private DefaultListModel listModel=新的DefaultListModel();
    私有JList JList=新JList(列表模型);
    公共滚动窗格3(){
    setLayout(新的BorderLayout());
    添加(jList,BorderLayout.CENTER);
    jList.setBackground(空);
    }
    公共void SetText(字符串text_x){
    listModel.addElement(text_x);
    }
    }
    
    您正在使ScrollingPane JPanel的功能短路,无法使用以下行正确调整自身大小:

    setPreferredSize(new Dimension(300, 100));
    
    这将修复滚动窗格JPanel的大小。我发现您有几种可能的解决方案:

  • 困难:覆盖ScrollingPane JPanel的getPreferredSize(),并使用FontMetrics根据其保存和绘制的文本大小计算适当的首选大小
  • 更简单:不要在添加文本时添加文本,而是让ScrollingPane使用GridLayout(0,1)(一列,行数可变),并在需要添加新文本时向ScrollingPane添加JLabel。然后对其调用
    revalidate()
    repaint()
  • 更简单的是:不要使用滚动窗格JPanel,而是使用JTextArea,它看起来像JPanel,不能编辑。将其添加到JScrollPane中,同样,不要限制其大小
  • 最简单:只需使用JList,因为这是您在此处使用的功能 例如,这两种方法中的任何一种都会起作用,并且看起来很相似:

    private static class ScrollingPane2 extends JPanel {
        private static final long serialVersionUID = 1L;
        private JTextArea textArea = new JTextArea(6, 20);
    
        public ScrollingPane2() {
            setLayout(new BorderLayout());
            add(textArea, BorderLayout.CENTER);
            textArea.setEditable(false);
            textArea.setFocusable(false);
            textArea.setBackground(null);
        }
    
        public void SetText(String text_x) {
            textArea.append(text_x + "\n");
        }
    }
    
    private static class ScrollingPane3 extends JPanel {
        private static final long serialVersionUID = 1L;
        private DefaultListModel<String> listModel = new DefaultListModel<>();
        private JList<String> jList = new JList<>(listModel);
    
        public ScrollingPane3() {
            setLayout(new BorderLayout());
            add(jList, BorderLayout.CENTER);
            jList.setBackground(null);
        }
    
        public void SetText(String text_x) {
            listModel.addElement(text_x);
        }
    }
    
    私有静态类ScrollingPane2扩展了JPanel{
    私有静态最终长serialVersionUID=1L;
    私有JTextArea textArea=新JTextArea(6,20);
    公共滚动窗格2(){
    setLayout(新的BorderLayout());
    添加(textArea,BorderLayout.CENTER);
    textArea.setEditable(false);
    textArea.setFocusable(false);
    textArea.setBackground(空);
    }
    公共void SetText(字符串text_x){
    textArea.append(text_x+“\n”);
    }
    }
    私有静态类ScrollingPane3扩展了JPanel{
    私有静态最终长serialVersionUID=1L;
    private DefaultListModel listModel=新的DefaultListModel();
    私有JList JList=新JList(列表模型);
    公共滚动窗格3(){
    setLayout(新的BorderLayout());
    添加(jList,BorderLayout.CENTER);
    jList.setBackground(空);
    }
    公共void SetText(字符串text_x){
    listModel.addElement(text_x);
    }
    }
    
    请参见编辑以回答…………您好?谢谢。滚动窗格2正是我所需要的。请参阅编辑以回答…………您好?谢谢。滚动窗格2正是我所需要的。