为什么会发生这种情况?ScrollbarPane

huangapple go评论55阅读模式
英文:

Why is this happening? ScrollbarPane

问题


import com.sun.org.apache.bcel.internal.generic.JsrInstruction;

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();

        // 设置框架
        frame.add(panel);
        frame.setSize(700, 700);
        frame.setMaximizedBounds(new Rectangle(0,0,1000,1000));
        frame.setPreferredSize(new Dimension(1000,1000));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        // 创建文本字段
        JTextField answer = new JTextField();
        answer.setBounds(100, 270,300, 30);
        answer.setFont(new Font("default", Font.BOLD, 18));
        JLabel input = new JLabel("输入");
        input.setBounds(100, 240, 80, 40);
        input.setFont(new Font("default", Font.ITALIC, 20));
        // 设置面板
        panel.setLayout(null);
        panel.add(answer);
        panel.add(input);

        JScrollPane inner_window = new JScrollPane();
        panel.add(inner_window);
        inner_window.setLayout(null);
        inner_window.setBounds(100, 300, 500, 300);
        inner_window.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        inner_window.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        // 允许窗口在中心打开 (frame.setLocationRelativeTo(null);)
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void SlowPrint(){

    }
}
英文:

I am getting this error

"Exception in thread "main" java.lang.IllegalArgumentException: invalid horizontalScrollBarPolicy
at javax.swing.JScrollPane.setHorizontalScrollBarPolicy(JScrollPane.java:554)
at com.company.Main.main(Main.java:39)"

What exactly am I doing incorrectly?


import com.sun.org.apache.bcel.internal.generic.JsrInstruction;

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();

        //set frame
        frame.add(panel);
        frame.setSize(700, 700);
        frame.setMaximizedBounds(new Rectangle(0,0,1000,1000));
        frame.setPreferredSize(new Dimension(1000,1000));
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        // create text field
        JTextField answer = new JTextField();
        answer.setBounds(100, 270,300, 30);
        answer.setFont(new Font("default", Font.BOLD, 18));
        JLabel input = new JLabel("input");
        input.setBounds(100, 240, 80, 40);
        input.setFont(new Font("default",Font.ITALIC, 20));
        //set panel
        panel.setLayout(null);
        panel.add(answer);
        panel.add(input);

        JScrollPane inner_window = new JScrollPane();
        panel.add(inner_window);
        inner_window.setLayout(null);
        inner_window.setBounds(100, 300, 500, 300);
        inner_window.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        inner_window.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);


        // allows window to open in center (frame.setLocationRelativeTo(null);)
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
    public static void SlowPrint(){

    }
}

答案1

得分: 1

你不能将垂直滚动条策略传递给 setHorizontalScrollBarPolicy
你可能想要使用 inner_window.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

英文:

You cannot pass a vertical scrollbar policy to a setHorizontalScrollBarPolicy.
You probably meant inner_window.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

huangapple
  • 本文由 发表于 2020年9月23日 18:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64025754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定