我是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);'而不指定佈局中按鈕應該出現的位置。 –