JScrollPane 不起作用/显示滚动条
Posted
技术标签:
【中文标题】JScrollPane 不起作用/显示滚动条【英文标题】:JScrollPane doesn't work/show scroll Bars 【发布时间】:2014-12-14 02:00:12 【问题描述】:我有一个JFrame
,它包含一个JPanel
,它包含一个JScrollPane
,它包含另一个JPanel
,有两个组件(JPanels
)。
出于某种原因,当我使用 WindowBuilder 的预览选项查看框架时,JScrollPane
显示 horizontal 滚动条,但是当我编译并运行应用程序时,它没有。
这是它的样子:
来自预览选项:
编译时:
这是我的代码:
package home;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.SystemColor;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Font;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.ScrollPaneConstants;
public class AtmManeger implements Serializable
private JFrame frmAtmManeger;
public int NumOfOpenAtmMachines = 0;
private final AtmManeger frame = this;
private ArrayList<ATMmachine> ATMs = new ArrayList<ATMmachine>();//Array list of all the ATM machines that were opened
private AtmAccountDataBase atmDataBase = new AtmAccountDataBase();
private JLabel contLabel = null;
/**
* Launch the application.
*/
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
public void run()
try
AtmManeger window = new AtmManeger();
window.frmAtmManeger.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the application.
*/
public AtmManeger getFrame()
return this.frame;
public AtmManeger()
initialize();
/**
* Initialize the contents of the frame.
*/
private void initialize()
try
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (ClassNotFoundException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
catch (InstantiationException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
catch (IllegalAccessException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
catch (UnsupportedLookAndFeelException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
frmAtmManeger = new JFrame();
frmAtmManeger.setMinimumSize(new Dimension(615, 420));
frmAtmManeger.getContentPane().setBackground(SystemColor.activeCaption);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(10, 10));
frmAtmManeger.setContentPane(mainPanel);
JScrollPane scrollPane = new JScrollPane();
mainPanel.add(scrollPane, BorderLayout.CENTER);
JPanel subPanel = new JPanel();
subPanel.setSize(new Dimension(1190, 350));
subPanel.setBackground(SystemColor.activeCaption);
subPanel.setPreferredSize(new Dimension(1190, 350));
scrollPane.setViewportView(subPanel);
subPanel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel = new JPanel();
subPanel.add(panel,"cell 0 0,grow");
panel.setBorder(null);
panel.setBackground(SystemColor.textHighlight);
panel.setLayout(new MigLayout("", "[100.00%,grow]", "[71px][13.54%][10.09%][41px][][grow]"));
JPanel panel_1 = new JPanel();
subPanel.add(panel_1, "cell 1 0,grow");
frmAtmManeger.setTitle("ATM Maneger");
frmAtmManeger.setBounds(700, 400, 609, 420);
frmAtmManeger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我制作了另一个与这个非常相似的应用程序,只是那个固有的JFrame
。我找不到这段代码和上面的代码有什么区别。代码:
package home;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JButton;
public class test02 extends JFrame
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
public void run()
try
test02 frame = new test02();
frame.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the frame.
*/
public test02()
try
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (Exception e)
// Handle exception
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 432, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 220));
panel.setBackground(Color.RED);
scrollPane.setViewportView(panel);
panel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel_1 = new JPanel();
panel.add(panel_1, "cell 0 0,grow");
panel_1.setLayout(new MigLayout("", "[][][][][][][]", "[][][][][][]"));
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel, "cell 2 1");
JButton btnNewButton_1 = new JButton("New button");
panel_1.add(btnNewButton_1, "cell 3 3");
JButton btnNewButton = new JButton("New button");
panel_1.add(btnNewButton, "cell 2 5");
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1, "cell 6 5");
JPanel panel_2 = new JPanel();
panel.add(panel_2, "cell 1 0,grow");
所以这可能只是一个愚蠢的错误,但它是什么?
【问题讨论】:
我不使用MigLayout
,但如果您愿意放弃它,我会发布答案。
【参考方案1】:
我没有看到您更改滚动条策略。 由于没有溢出,如果不更改该设置,您将不会看到滚动条出现。
尝试:
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
【讨论】:
我的问题是水平滚动条。但无论如何,它仍然不起作用。但是感谢您的帮助:) 所以即使使用 setHorizonalScrollBarPolicy 也无法修复它?【参考方案2】:我已经编写了一个程序来根据要求显示面板。
代码:
private void createUI()
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JScrollPane pane = new JScrollPane();
JPanel leftPnl = new JPanel(new BorderLayout());
JPanel rightPnl = new JPanel(new BorderLayout());
leftPnl.add(new JLabel("Left"), BorderLayout.CENTER);
leftPnl.setBorder(new LineBorder(Color.black, 5));
leftPnl.setPreferredSize(new Dimension(400, 400));
rightPnl.add(new JLabel("Right"), BorderLayout.CENTER);
rightPnl.setBorder(new LineBorder(Color.black, 5));
rightPnl.setPreferredSize(new Dimension(400, 400));
panel.add(leftPnl);
panel.add(rightPnl);
panel.add(pane);
frame.setPreferredSize(new Dimension(400, 400));
frame.setTitle("ScrollPane Example");
frame.add(new JScrollPane(panel));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
【讨论】:
实际上我遇到了问题..当我通过将ScrollPane
放在面板上并将其添加到框架中来做同样的事情时,即使我的滚动条也不可见。但是在我编写的示例中,我将滚动窗格直接添加到框架中,而不是将其添加到面板中。
我尝试直接在 JFrame 中添加 scrollPane,它可以工作,但是窗口构建器没有显示框架。我不明白为什么相同的代码在 test02 类中工作,而不是在这个类中......以上是关于JScrollPane 不起作用/显示滚动条的主要内容,如果未能解决你的问题,请参考以下文章