英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论