我是Swing的新手。我正在使用Eclipse IDE在其中构建一个带有JScrollPane的JFrame。 JScrollPane内部是Border Layout中的JPanel。我尝试使用下面的代码向JFrame添加一个JButton(称为“submitAnswers”),但由于某种原因,该按钮仅出现在我计算机上的帧的末尾,而不是出现在其他计算机上(我的朋友在他的计算机上试过它) Mac和我在像我这样的单独Windows操作系统上尝试过它。我尝试过的一些建议的解决方案和其他没有工作的网站包括:
另外,我的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);
}
答案 0 :(得分:0)
我稍微重构了你的代码,使用方法来创建GUI的各个组件。您可以找到完整的代码at this ideone link
我第一次将代码复制到我的机器时看到的是,唯一可见的是按钮。因此,我使用自己的方法创建所有组件,然后使用Border Layout将它们添加到框架和面板中。这使我能够将指令放在NORTH部分,南部的按钮,然后主要部分将进入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);
}