簡體   English   中英

Java搖擺-未顯示背景圖片

[英]Java Swing - Background Image Not Showing

我對Java很陌生,正在嘗試在Swing中創建一個程序。 一切正常,但背景圖像未顯示。 添加背景圖像的代碼似乎很好(我對此有所幫助)。 我主要關心的是我放置BackgroundPanel類和我調用的類的實例(在main中,這是在主文檔中引用BackgroundPanel的唯一位置)。 我還為BackgroundPanel類本身提供了一個單獨的文檔(兩個文檔都粘貼在下面)。

有人可以指引我正確的方向嗎? 我取消了導入和打包信息,因為它們在這里占用了大量空間。 謝謝!

這是我的主要代碼:

public class InvitationCard extends JFrame {

private JPanel contentPane;
private JTextField txtMood;
private JPanel panel;
private JTextPane textPaneBody;
private JTextPane textPaneNames;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                InvitationCard frame = new InvitationCard();
                frame.setVisible(true);
                BackgroundPanel BP = new BackgroundPanel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */

public InvitationCard() {
    setBackground(Color.WHITE);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 637, 490);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
    gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
    gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0 };
    gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 0.0, 
Double.MIN_VALUE };
    contentPane.setLayout(gbl_contentPane);

    panel = new JPanel();
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.fill = GridBagConstraints.VERTICAL;
    gbc_panel.anchor = GridBagConstraints.WEST;
    gbc_panel.gridwidth = 2;
    gbc_panel.gridheight = 5;
    gbc_panel.insets = new Insets(0, 0, 0, 5);
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    contentPane.add(panel, gbc_panel);
    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[] { 130, 0 };
    gbl_panel.rowHeights = new int[] { 26, 0, 0, 0 };
    gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
    gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
    panel.setLayout(gbl_panel);

    Font font1 = new Font("Helvetica", Font.BOLD, 12);
    Font font2 = new Font("Courier", Font.BOLD, 12);
    Font font3 = new Font("nouradilla.regular", Font.BOLD, 12);
    Font font4 = new Font("GearedSlab-Bold", Font.PLAIN, 12);

    txtMood = new JTextField();
    GridBagConstraints gbc_txtMood = new GridBagConstraints();
    gbc_txtMood.insets = new Insets(0, 0, 5, 0);
    gbc_txtMood.anchor = GridBagConstraints.NORTHWEST;
    gbc_txtMood.gridx = 0;
    gbc_txtMood.gridy = 1;
    panel.add(txtMood, gbc_txtMood);
    txtMood.setEditable(false);
    txtMood.setText("Mood: ");
    txtMood.setColumns(10);

    JComboBox comboBox = new JComboBox();
    GridBagConstraints gbc_comboBox = new GridBagConstraints();
    gbc_comboBox.gridx = 0;
    gbc_comboBox.gridy = 2;
    panel.add(comboBox, gbc_comboBox);
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // get item from dropdown
            String item = (String) comboBox.getSelectedItem();
            if (item == "Whimsical") {
                textPaneNames.setFont(font1);
                textPaneBody.setFont(font1);
            } else if (item == "Traditional") {
                textPaneNames.setFont(font2);
                textPaneBody.setFont(font2);
            } else if (item == "Modern") {
                textPaneNames.setFont(font3);
                textPaneBody.setFont(font3);

            } else if (item == "Crazy") {
                textPaneNames.setFont(font4);
                textPaneBody.setFont(font4);

            }

        }

    });

    comboBox.setModel(
            new DefaultComboBoxModel(new String[] { "Select", "Whimsical",      
"Traditional", "Modern", "Crazy" }));

    textPaneNames = new JTextPane();
    textPaneNames.setText("FIRST1 LAST1\n&\nFIRST2 LAST2");
    GridBagConstraints gbc_textPaneNames = new GridBagConstraints();
    gbc_textPaneNames.insets = new Insets(0, 0, 5, 0);
    gbc_textPaneNames.fill = GridBagConstraints.HORIZONTAL;
    gbc_textPaneNames.gridx = 3;
    gbc_textPaneNames.gridy = 0;
    contentPane.add(textPaneNames, gbc_textPaneNames);

    textPaneBody = new JTextPane();
    textPaneBody.setText(
            "REQUEST THE HONOR OF YOUR PRESENCE \nAT THEIR WEDDING 
CEREMONY\n\nFRIDAY, JANUARY SECOND\nTWO-THOUSAND AND SEVENTEEN\nSIX-THIRTY IN 
THE EVENING\n\nADDRESS GOES HERE\n903 ADDRESS LANE\nCITY, STATE\n\n\nRECEPTION 
TO FOLLOW");
    GridBagConstraints gbc_textPaneBody = new GridBagConstraints();
    gbc_textPaneBody.anchor = GridBagConstraints.NORTH;
    gbc_textPaneBody.insets = new Insets(0, 0, 5, 0);
    gbc_textPaneBody.fill = GridBagConstraints.HORIZONTAL;
    gbc_textPaneBody.gridx = 3;
    gbc_textPaneBody.gridy = 1;
    contentPane.add(textPaneBody, gbc_textPaneBody);

    // centers the names and body text
    StyledDocument doc = textPaneBody.getStyledDocument();
    StyledDocument doc2 = textPaneNames.getStyledDocument();

    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    doc2.setParagraphAttributes(0, doc2.getLength(), center, false);
 }

}

//這是BackgroundPanel.java:

public class BackgroundPanel extends JPanel {

  Image image;
  public BackgroundPanel()
  {
    try
    {
      image = javax.imageio.ImageIO.read(new  
java.net.URL(getClass().getResource("/satin.jpg"), "/satin.jpg"));
    }
    catch (Exception e) { /*handled in paintComponent()*/ }
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g); 
    if (image != null)
      g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
  }

}

BackgroundPanel BP = new BackgroundPanel();之后添加此權限BackgroundPanel BP = new BackgroundPanel(); 在您的main()中:

frame.add(BP);

您需要將組件添加到框架。 否則將不會調用諸如paint()之類的方法。

嗯...讓我們看一下重寫BackgroundPanel的一些修改,我應該有這個

public class BackgroundPanel extends JComponent {
    //every code you put in this class
}

如果我是正確的,這應該使您能夠做到這一點

setContentPane(new BackgroundPanel());

如果這樣做不起作用,請告訴我,以便為您提供更好的服務。

編輯

再次檢查您的代碼,我認為這是您必須要做的

public InvitationCard(){
    setContentPane(new BackgroundPanel());
    /* then every other code follows,
       except the "setContentPane(contentPane)"
       you can comment that out
    */

    this.add(contentPane);
}

而且您的BackgroundPanel類仍然可以保留為JPanel的子類,這樣更好

不需要frame.add(BP); 不再。

由於我現在沒有辦法運行它,所以我仍然不知道這是否可以解決您的問題...您告訴我

這可能為時已晚,但是我認為您可能需要將BackgroundPanel Opaque布爾值設置為true。

    BackgroundPanel BP = new BackgroundPanel();
    BP.setOpaque(true);

您可能需要對要具有“背景”顏色或圖像的任何區域執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM