2014-09-02 82 views
1

我的工作有問題。 我想要發生的事情是我想讓自己的形象成爲特定的或永久的位置。 但每次調整窗口大小時,圖像也會改變其位置。當我使用.setResizable(false)時,我看不到文字和圖像。圖像的永久位置?

import java.awt.*; 
import javax.swing.*; 

public class ProgDraft extends JFrame { 
private ImageIcon image1; 
private JLabel label1; 

ProgDraft() { 

    JPanel panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); 

    ImageIcon pics = new ImageIcon(getClass().getResource("l.png")); 

    JLabel logo = new JLabel(pics); 
    panel.setBounds(100,100,100,100); 
    panel.add(logo); 


    JLabel title = new JLabel("Testing Title"); 
    panel.add(title); 

    getContentPane().setLayout(new BorderLayout()); 


    getContentPane().add(panel); 
    getContentPane().add(logo, BorderLayout.CENTER); 
    getContentPane().add(title, BorderLayout.NORTH); 
} 
} 

這裏的主要

import javax.swing.*; 
    import java.awt.*; 

    public class ProgDraftMain { 
    public static void main(String args[]) { 
    ProgDraft gui = new ProgDraft(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setResizable(false); 
    gui.setVisible(true); 

    //gui.pack(); 
    gui.setSize(500 , 300); 
    } 
    } 

謝謝!

+0

這將使問題,因爲它是相同的:**的getContentPane()加(面板)。 (),添加(logo,BorderLayout.CENTER); **這兩個組件都添加了BorderLayout.CENTER(CENTER是默認的) – Ben 2014-09-02 10:58:18

回答

2

您已將徽標添加到面板,但您嘗試再次將徽標添加到內容窗格(與標題相同)。第一次添加到面板會被取消,這是不好的,因爲您需要FlowLayout來保持位置。每個組件只能有一個父代。不要將徽標添加到內容窗格。只需添加面板,並使面板的佈局new FlowLayout(FlowLayut.LEADING)。這將把標籤放在面板的最左邊。然後,您可以爲標籤添加一個空邊框以向標籤添加間距

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Font; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ProgDraftMain { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       ProgDraft gui = new ProgDraft(); 
       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       gui.setResizable(false); 
       gui.setSize(500 , 300); 
       gui.setVisible(true);   
      } 
     }); 
    } 
} 

class ProgDraft extends JFrame { 
    private ImageIcon image1; 
    private JLabel label1; 

    ProgDraft() { 

     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout(FlowLayout.LEADING)); 

     ImageIcon pics = new ImageIcon(getClass().getResource("stackoverflow.png")); 

     JLabel logo = new JLabel(pics); 
     logo.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0)); 
     panel.add(logo); 
     panel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

     JLabel title = new JLabel("Testing Title", JLabel.CENTER); 
     Font font = new Font("impact", Font.PLAIN, 30); 
     title.setFont(font); 

     getContentPane().setLayout(new BorderLayout()); 

     getContentPane().add(panel, BorderLayout.CENTER); 
     getContentPane().add(title, BorderLayout.PAGE_START); 
    } 
} 

瞭解如何使用不同的佈局管理器。不要試圖依靠像素完美的位置。見Laying out Components Within a Container

enter image description here

也看看How to Use Borders

+0

cool!這是一個很大的幫助!謝謝,我一定會閱讀你分享的鏈接。再次感謝你! – Naomi 2014-09-02 11:33:03