Java JPanel和JScrollPane在Swing应用程序中不可见
经过很长时间,我正在开发一个Swing应用程序。我可能忘记了一些基本知识,但我正在试图弄清楚为什么在运行应用程序时面板和滚动窗格不可见。帮我找出我犯的错误。我有一个Java JPanel和JScrollPane在Swing应用程序中不可见,java,swing,Java,Swing,经过很长时间,我正在开发一个Swing应用程序。我可能忘记了一些基本知识,但我正在试图弄清楚为什么在运行应用程序时面板和滚动窗格不可见。帮我找出我犯的错误。我有一个JFrame和一个JMenuBar在顶部,然后需要一个面板。请注意,JFrame还需要一个JScrollPane下面是我的代码 public class FrontEndView { private JFrame mainFrame; private JMenuBar menubar; private JMenu menuFile,
JFrame
和一个JMenuBar
在顶部,然后需要一个面板。请注意,JFrame
还需要一个JScrollPane
下面是我的代码
public class FrontEndView {
private JFrame mainFrame;
private JMenuBar menubar;
private JMenu menuFile, menuEdit,menuView,menuHelp;
private JMenuItem menuItemA,menuItemB;
private JPanel mainPanel;
private JScrollPane scrollPane;
private JPanel panel1;
public FrontEndView(){
this.prepareGUI();
}
public void prepareGUI(){
mainFrame=new JFrame("WALL-E");
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
mainFrame.setSize(xSize,ySize);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(true);
mainFrame.setLayout(new BorderLayout());
menubar=new JMenuBar();
menuFile=new JMenu("File");
menuEdit=new JMenu("Edit");
menuView=new JMenu("View");
menuHelp=new JMenu("Help");
menuItemA=new JMenuItem("SubMenu-A");
menuItemB=new JMenuItem("SubMenu-B");
menuFile.add(menuItemA);
menuFile.add(menuItemB);
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuView);
menubar.add(menuHelp);
mainFrame.add(menubar,BorderLayout.NORTH);
mainPanel=new JPanel();
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(1, 2));
scrollPane=new JScrollPane(mainPanel);
mainFrame.add(scrollPane);
mainFrame.setVisible(true);
}
public static void main(String[] args){
FrontEndView swingControlDemo = new FrontEndView();
}
}
运行代码后,注意到它没有显示任何内容的主要原因是您没有向
主面板添加任何JComponent
。因此,尝试添加一些组件将显示:
mainPanel.add(new JLabel("I'm Visible!"));
一些提示:
您不应将主面板
既添加到大型机
,也添加到滚动窗格
。仅将面板添加到视口
区域(通过将面板传递给JScrollPane
的构造函数,已经完成了此操作)。然后将滚动窗格
添加到大型机的中心
因此,删除这一行,它应该可以工作:
mainFrame.add(mainPanel, BorderLayout.CENTER);
同样与具体问题无关,将菜单栏添加到框架的北方向是不好的。改用JFrame#setJMenuBar
方法
更改此项:
mainFrame.add(menubar,BorderLayout.NORTH);
为此:
mainFrame.setJMenuBar(menubar);
祝您好运滚动窗格是通过这种方式添加的,只需对代码进行一点修改。现在您可以在面板上看到一个标签,它再次位于主面板上
mainFrame.add(menubar,BorderLayout.NORTH);
JLabel l=new JLabel("Label on panel which is again inside scrollPane");
mainPanel=new JPanel();
// mainFrame.add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(1, 2));
mainPanel.add(l);
scrollPane=new JScrollPane(mainPanel);
//scrollPane.setVisible(true);
//scrollPane.setSize(10, 20);
mainFrame.add(scrollPane);
如果你想再做一件事
scrollPane=new JScrollPane(mainPanel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
无需在JScrollPane
上调用setVisible
,默认可见;无需在JScrollPane
上调用setSize
,框架的布局管理器将决定此thnx@mad程序员已获得它。由于main panel
为空(首选大小为0x0),默认情况下JScrollPane
在需要滚动条之前不会实际显示滚动条,你怎么知道它们是否可见?