【发布时间】:2014-03-28 07:34:16
【问题描述】:
我是 Swing 的新手。我正在使用 Eclipse IDE 构建一个带有 JScrollPane 的 JFrame。 JScrollPane 内部是 Border Layout 中的 JPanel。我尝试使用下面的代码向 JFrame 添加一个 JButton(称为“submitAnswers”),但由于某种原因,该按钮仅出现在我的计算机上的帧末尾,但没有出现在其他计算机上(我的朋友在他的Mac,我在像我这样的单独的 Windows 操作系统上尝试过)。我尝试过的一些建议的解决方案以及其他无效的解决方案包括:
- 使用 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);
}
【问题讨论】:
-
testContentPanel.setPreferredSize(new Dimension(900, 2060));1) Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于精确放置或确定组件的大小。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 2) 这比我的屏幕还高! -
每当您有一个带有 BorderLayout 的面板时,请确保在添加组件时使用特定位置,例如 BorderLayout.SOUTH。目前,您有 'submitAnswersPanel.add(submitAnswers);` 没有指定按钮应该出现在布局中的哪个位置。
-
另外,我会将 testContentPanel 直接传递给 JScrollPane 构造函数。 (如果这些建议中的任何一个解决了您的问题,请告诉我,我会将它们添加为答案)
-
试试'testFrame.getContentPane().add(testScrollPane);'和'testFrame.getContentPane().setLayout(new BorderLayout());' (将 getContentPane() 添加到两者)
标签: java swing layout boxlayout preferredsize