JScrollPane不适用于我的JPanel [英] JScrollPane doesn't work for my JPanel
本文介绍了JScrollPane不适用于我的JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我必须说我已经检查了这些问题并且没有找到我的答案:
first of all I must say that I have checked these questions and didn't find my answer :
以及其他许多问题喜欢这样
and many other questions like so
我也检查了这些教程和示例:
also I have checked these tutorials and examples:
和许多其他网站。但我无法解决我的问题。
and many other sites. but I couldn't fix my problem.
和
这是我的代码的简单类型:
and this is the simple kind of my code:
public class Question extends JFrame {
public Question() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLayout(new BorderLayout());
setSize(d.width, d.height);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(d.width, d.height));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setLayout(new BoxLayout(panel, 1));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("kjdh"));
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(500, 500));
getContentPane().add(scrollPane);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new Question();
}
}
但 JScrollPane
没有出现。我测试过很多东西。我已经改变了将面板
和 scrollPane
添加到我的框架
但它不起作用。任何人都可以帮助我吗?
but the JScrollPane
doesn't appear. I have tested many things. I have changed the way adding panel
and scrollPane
to my frame
but it didn't work. can any one help me plz?
推荐答案
- 不要在面板上设置首选大小。请参阅我是否应避免在Java Swing中使用setPreferred / Maximum / MinimumSize方法?出于原因。
- 仅将 滚动窗格添加到内容窗格。
- Don't set a preferred size on the panel. See Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing? for the reasons why.
- Add only the scroll pane to the content pane.
- 使用默认布局(
BorderLayout
)的内容窗格将默认将组件放入CENTER
约束(如果没有提供),CENTER
区域只能接受单个组件。 - 除此之外该面板已经添加到滚动窗格中,它已经显示在其中,并且只能出现在一个容器中。
- A content pane using the default layout (
BorderLayout
) will default to putting the component in theCENTER
constraint if none is supplied, and theCENTER
area can only accept a single component. - Besides that, the panel has already been added to the scroll pane, it will already appear inside it, and can only appear in a single container.
setSize
,但 setExtendedState
。 DISPOSE_ON_CLOSE
。
setSize
, but setExtendedState
.DISPOSE_ON_CLOSE
.
import java.awt.*;
import javax.swing.*;
public class Question {
public Question() {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setLayout(new BoxLayout(panel, 1));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("kjdh"));
}
JScrollPane scrollPane = new JScrollPane(panel);
f.getContentPane().add(scrollPane);
f.pack();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Question();
}
};
SwingUtilities.invokeLater(r);
}
}
这篇关于JScrollPane不适用于我的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文