2014-02-25 32 views
1

我是Swing的新手。我正在使用Eclipse IDE构建一个带有JScrollPane的JFrame。 JScrollPane内部是边框布局中的JPanel。我尝试使用下面的代码向JFrame中添加一个JButton(称为“submitAnswers”),但出于某种原因,按钮只出现在我的计算机上的框架的末尾,而不出现在其他计算机上(我的朋友在他的电脑上试过Mac和我在像我这样的独立Windows操作系统上尝试过)。我尝试过的一些建议的解决方案以及其他尚未运作的网站包括:JButton出现在一台计算机上,但不是其他(BorderLayout)

  • 使用pack()方法。原因:由于JPanel的首选大小高于JFrame(因此我使用了JScrollPane),所以打包JFrame只会导致文本在桌面上不可见。
  • 在内容JPanel上放置按钮。原因是:我不知道。它不会出现在另一台台式电脑或我朋友的Mac电脑上。
  • 使用BorderLayout.SOUTH而不是BorderLayout.PAGE_END。原因:完全没有变化。该按钮仍然可以在我的电脑上看到,但在其他人看不到。
  • 将按钮直接放在JFrame上。原因是:我不知道。

另外,我的JFrame嵌套在一个静态方法中;因此,我只包含了我遇到的具体方法的相关代码。

有没有人有过这个问题?我真的很感激你的洞察力。

代码:

public static void createTestPage() { 

    JFrame testFrame = new JFrame("testing...1,2,3"); 

    //Customizes icon to replace java icon 
    try { 
     testFrame.setIconImage(ImageIO.read(new File("src/icon.png"))); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    //Centers location of introFrame to center of desktop 
    Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize(); 
    testFrame.setLocation(screenDimensions.width/16,screenDimensions.height/14); 

    //Size and display the introFrame. 
    Insets insets = testFrame.getInsets(); 

    //Format size of screen itself 
    testFrame.setSize(1200 + insets.left + insets.right, 
        400 + insets.top + 250 + insets.bottom); 

    //Temporarily set screen so that it cannot be resized 
    testFrame.setResizable(false); 

    //Set background color of testFrame 
    testFrame.getContentPane().setBackground(new Color(75, 0, 130)); 

    testFrame.setLayout(new BorderLayout()); 

    //Set layout of testFrame 
    testFrame.setLayout(new BorderLayout(10, 1)); 

    //Test content 
    JPanel testContentPanel = new JPanel(); 
    testContentPanel.setBackground(new Color(75, 0, 130)); 
    testContentPanel.setSize(new Dimension(900,2060)); 
    testContentPanel.setPreferredSize(new Dimension(900, 2060)); 

    //Test content pane layout 
    testContentPanel.setLayout(new BoxLayout(testContentPanel, BoxLayout.PAGE_AXIS)); 

    //Create panel to hold instructions text 
    JPanel instructionsPanel = new JPanel(); 
    instructionsPanel.setBackground(new Color(75, 0, 130)); 
    instructionsPanel.setLayout(new BorderLayout(10,1)); 

    //Create JPanel for submit answers button 
    JPanel submitAnswersPanel = new JPanel(new BorderLayout()); 
    submitAnswersPanel.setBackground(new Color(75, 0, 130)); 
    submitAnswersPanel.setVisible(true); 

    //Create button to submit personality test answers 
    JButton submitAnswers = new JButton("Submit Answers"); 
    submitAnswers.setVisible(true); 
    submitAnswers.setBorder(new EmptyBorder(10, 400, 10, 400)); 

    //Add submitAnswers button to panel 
    submitAnswersPanel.add(submitAnswers); 

    //Add submitAnswersPanel to test content panel 
    testContentPanel.add(submitAnswersPanel); 


     //Create scroll pane to allow for scrollable test (contents cannot fit one page) 
    JScrollPane testScrollPane = new JScrollPane(); 
    testScrollPane.setViewportView(testContentPanel); 

    //Get rid of horizontal scroll bar and add vertical scrollbar 
    testScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    testScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

    //Speed up scrolling 
    testScrollPane.getVerticalScrollBar().setUnitIncrement(16); 

    testFrame.add(testScrollPane); 

    //Experiment to show button 
    testFrame.setVisible(true); 
    } 
+2

请编辑您的问题包括[mctre(http://stackoverflow.com/help/mcve)表现出您所描述的问题;为了保持一致性,请使用'UIManager'图标,如[此处]所示(http://stackoverflow.com/a/12228640/230513)。 – trashgod

+1

'testContentPanel.setPreferredSize(new Dimension(900,2060));'1)Java GUI可能需要在多种平台上工作,使用不同的屏幕分辨率并使用不同的PLAF。因此,它们不利于组件的准确放置或大小。为了组织强大的图形用户界面,请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[空格]的布局填充和边框(http: //stackoverflow.com/q/17874717/418556)。 2)这比我的屏幕更高! –

+0

每当您有一个带有BorderLayout的面板时,请确保在添加组件时使用特定的位置,例如BorderLayout.SOUTH。目前,您有'submitAnswersPanel.add(submitAnswers);'而不指定布局中按钮应该出现的位置。 –

回答

0

我已经重构你的代码一点点使用这种方法来创建GUI的各个组成部分。你可以找到完整的代码at this ideone link

当我第一次将你的代码复制到我的机器上时,我所看到的仅仅是按钮。因此,我使用自己的方法创建所有组件,然后使用Border Layout将它们添加到框架和面板中。这使我能够将指令放在NORTH部分,SOUTH部分的按钮,然后主要位将放在CENTER部分。

有一点要注意有关部分:(从文档)

的组分根据它们的优选的尺寸和容器的大小的限制布局。 NORTH和SOUTH组件可能水平拉伸; EAST和WEST组件可能会被垂直拉伸; CENTER组件可以水平和垂直拉伸以填充剩下的空间。

因此,您应该将要缩放的组件添加到CENTER部分。

我的主要方法现在看起来是这样的:

public static void main(final String[] args) { 
    final JButton submitAnswers = createSubmitAnswersButton(); 
    final JPanel instructionsPanel = createInstructionsPanel(); 

    final JPanel testContentPanel = createContentPanel(); 
    testContentPanel.add(instructionsPanel, BorderLayout.NORTH); 
    testContentPanel.add(submitAnswers, BorderLayout.SOUTH); 

    final JScrollPane scrollingContentPane = createScrollPaneFor(testContentPanel); 

    final JFrame testFrame = createJFrame(); 
    testFrame.add(scrollingContentPane, BorderLayout.CENTER); 

    testFrame.setVisible(true); 
} 
+0

你好丹,感谢您的建议格式。我编辑了代码,使所有边框布局,并将按钮放在BorderLayout.SOUTH和BorderLayout.NORTH中的指令,并在BorderLayout.CENTER中放置了大约4个JLabels,8个JRadioButtons和4个JTable。但是,我仍然有同样的问题和更多。首先,按钮只出现在我的电脑上,但不出现在另一台台式电脑上。其次,只有最后2个单选按钮出现在中心。我的代码如下。 – user3051111

+0

Frame上的内容窗格是单个面板。它可以使用BorderLayout容纳5个组件。那些组件可以是JPanels,它们可以容纳更多的组件。如果将一个JTable添加到框架的BorderLayout.CENTER,然后将另一个添加到框架的相同区域,则最终会丢失组件。您需要将它们添加到单个面板并滚动窗格,然后将它们嵌套以实现所需的显示。或者使用另一个布局管理器,例如[GroupLayout](http://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.html) –

+0

我已经用这个嵌套面板的东西[在这个ideone链接](https://ideone.com/8QExa1) –

相关问题