2014-01-27 107 views
2

程序根據屏幕分辨率或計算機大小以其大小運行。當我在具有特定尺寸監視器的計算機上運行它時,它會相應地改變。我的問題是JPanel或框架內的任何對象的位置和大小,以便在屏幕大小改變時採用。JPanel的位置和大小根據屏幕大小而變化

因此,無論何時我將在任何具有不同尺寸的顯示器上展示我的程序時,組件仍將按照我最初設計的方式進行組織和放置。但是在這裏我正在使用一個名爲displayMenu的JPanel進行測試。其中它顯示綠色的面板。

package saves.project; 

import com.sun.awt.AWTUtilities; 
import java.awt.*; 
import java.awt.image.BufferedImage; 

import java.io.*; 

import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.BorderFactory; 

import javax.imageio.ImageIO; 

public class Homepage extends JFrame{ 

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

Border grayline = BorderFactory.createLineBorder(Color.GRAY);; 

int width = screenSize.width, height = screenSize.height; 

public Homepage() throws IOException{ 

    super("Homepage"); 
    displayMenu(); 
    displayBackground(); 

} 

public static BufferedImage resize(BufferedImage image, int width, int height) { 

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); 
    Graphics2D g2d = (Graphics2D) bi.createGraphics(); 
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); 
    g2d.drawImage(image, 0, 0, width, height, null); 
    g2d.dispose(); 
    return bi; 

} 

public void displayBackground() throws IOException{ 

    JPanel pBackground = new JPanel(); 
    pBackground.setSize(screenSize); 
    pBackground.setLayout(new FlowLayout()); 
    add(pBackground); 

    BufferedImage header = ImageIO.read(new File("res\\bg.jpg")); 
    BufferedImage resizedImage = resize(header,width,height); 
    ImageIcon image = new ImageIcon(resizedImage); 
    JLabel lblheader = new JLabel(image, JLabel.CENTER); 
    pBackground.add(lblheader); 

} 

public void displayMenu() { 

    JPanel pTitle = new JPanel(); 
    pTitle.setLayout(null); 
    pTitle.setBounds(width/3, (height/2)+20, width/2, height/2); 
    pTitle.setBackground(Color.GREEN); 
    add(pTitle); 

} 

public void CreateAndShowGUI() { 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    setUndecorated(true); 
    setVisible(true); 

} 

} 

此外,它看起來像我的背景並沒有完全傳播到整個框架。頂部有一條白線,其餘的是背景。我該怎麼辦?感謝幫助!

+2

不要使用的setSize或組件的setBounds,他們應該留給,佈局管理器。考慮使用GridBagLayout之類的東西來更好地控制佈局。您也可能會發現,當HomePage是構造函數時,組件的大小未知 – MadProgrammer

+0

感謝您的回答!你能提供更多細節嗎?提前致謝^^我只是一個新手在這裏:D – mushroomgroover

回答

7

經過了@MadProgrammer評論:

你真的需要學習如何使用佈局曼格斯。設置大小不是要走的路,因爲它們將在不同的機器上執行不同的操作。

關於Layout Manager的一個重要知識是哪些佈局尊重其內部組件的首選大小。不尊重尺寸的產品會拉伸組件。一些佈局可能不會拉伸它們的組件,但是當主容器被拉伸時,它們將放置在開放空間內的默認位置。

爲了獲得理想的結果,有時還需要使用不同佈局來嵌套容器,這樣可以利用兩個或多個佈局。

我知道這不是對您問題的真正答案,但我認爲您仍然可以通過使用佈局管理器來了解您的問題,以及如何實現您嘗試的目標。

下面我剛剛構建了一些主要佈局管理器的不同特性的快速示例。你可以玩它。請注意主JFrame正在使用默認的BorderLayout。我只明確地將佈局設置爲BorderLayout,以便您可以看到哪個佈局導致了該效果。

另請參閱Laying out Components Withing a Container以瞭解有關如何使用不同佈局管理器的更多信息。避免使用空佈局並嘗試自己定位所有內容。讓佈局爲你做,因爲Swing是用於佈局管理器的。


enter image description here

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class TestLayoutManagers { 

    private JPanel northFlowLayoutPanel; 
    private JPanel southBorderLayoutPanel; 
    private JPanel centerGridBagLayoutPanel; 
    private JPanel westBoxLayoutPanel; 
    private JPanel eastGridLayoutPanel; 

    private final JButton northButton = new JButton("North Button"); 
    private final JButton southButton = new JButton("South Button"); 
    private final JButton centerButton = new JButton("Center Button"); 
    private final JButton eastButton = new JButton("East Button"); 
    private final JButton westButton = new JButton("West Button"); 

    public TestLayoutManagers() { 
     northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     southBorderLayoutPanel = new JPanel(new BorderLayout()); 
     centerGridBagLayoutPanel = new JPanel(new GridBagLayout()); 
     eastGridLayoutPanel = new JPanel(new GridLayout(1, 1)); 
     Box box = Box.createHorizontalBox(); 
     westBoxLayoutPanel = new JPanel(); 

     northFlowLayoutPanel.add(northButton); 
     northFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Flow Layout")); 

     southBorderLayoutPanel.add(southButton); 
     southBorderLayoutPanel.setBorder(BorderFactory.createTitledBorder("Border Layout")); 

     centerGridBagLayoutPanel.add(centerButton); 
     centerGridBagLayoutPanel.setBorder(BorderFactory.createTitledBorder("GridBag Layout")); 

     eastGridLayoutPanel.add(eastButton); 
     eastGridLayoutPanel.setBorder(BorderFactory.createTitledBorder("Grid Layout")); 

     box.add(westButton); 
     westBoxLayoutPanel.add(box); 
     westBoxLayoutPanel.setBorder(BorderFactory.createTitledBorder("Box Layout")); 

     JFrame frame = new JFrame("Test Layout Managers"); 
     frame.setLayout(new BorderLayout());  // This is the deafault layout 
     frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START); 
     frame.add(southBorderLayoutPanel, BorderLayout.PAGE_END); 
     frame.add(centerGridBagLayoutPanel, BorderLayout.CENTER); 
     frame.add(eastGridLayoutPanel, BorderLayout.LINE_END); 
     frame.add(westBoxLayoutPanel, BorderLayout.LINE_START); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       TestLayoutManagers testLayoutManagers 
         = new TestLayoutManagers(); 
      } 
     }); 
    } 
} 
+1

老實說,我總是覺得像downvoting,這樣的帖子,仍然建議使用BorderLayout.NORTH/SOUTH /東/西/而不是'BorderLayout.PAGE_START/PAGE_END/LINE_START/LINE_END'。即使Java鼓勵使用後者,因爲1.4 –

+0

@nIcEcOw已修復! –

+0

哈哈,但它仍然說'BorderLayout.SOUTH'應該是'BorderLayout.PAGE_END','BorderLayout.LINE_START'爲'BorderLayout.EAST','BorderLayout.LINE_END'爲'BorderLayout.WEST' :-) –

相關問題