我对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);
  }


}

最佳答案

在main()中的BackgroundPanel BP = new BackgroundPanel();之后添加以下内容:

frame.add(BP);


您需要将组件添加到框架。否则将不会调用诸如paint()之类的方法。

10-05 19:13