我是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);
}
请编辑您的问题包括[mctre(http://stackoverflow.com/help/mcve)表现出您所描述的问题;为了保持一致性,请使用'UIManager'图标,如[此处]所示(http://stackoverflow.com/a/12228640/230513)。 – trashgod
'testContentPanel.setPreferredSize(new Dimension(900,2060));'1)Java GUI可能需要在多种平台上工作,使用不同的屏幕分辨率并使用不同的PLAF。因此,它们不利于组件的准确放置或大小。为了组织强大的图形用户界面,请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[空格]的布局填充和边框(http: //stackoverflow.com/q/17874717/418556)。 2)这比我的屏幕更高! –
每当您有一个带有BorderLayout的面板时,请确保在添加组件时使用特定的位置,例如BorderLayout.SOUTH。目前,您有'submitAnswersPanel.add(submitAnswers);'而不指定布局中按钮应该出现的位置。 –