在我的项目中,如果用户调整框架大小或具有较低的屏幕分辨率,则包含所有信息的 JPanel 将大于 JFrame。
通过示例代码:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class scroll{
public static void main(String[] args) {
JFrame frame = new JFrame("Example");
frame.setSize(500, 500);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setBounds(5, 5, 450, 600);
frame.add(panel);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
有人可以解释一下如何为此添加 JScrollPane,请查看此处和更广泛的网络,但似乎找不到答案或教程。
最佳答案
我希望这能给你答案:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class panel{
public static void main(String[] args) {
JFrame frame = new JFrame("Example");
frame.setSize(500, 500);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setPreferredSize(new Dimension(450,600));
JScrollPane scrollPane = new JScrollPane(panel);
frame.setContentPane(scrollPane);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
关于java - 添加 JScrollpane,其中 JPanel 比 JFrame 大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683529/