Java摇摆 - 没有显示的背景图象

时间:2016-07-02 10:10:18

标签: java swing class windowbuilder

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

}

3 个答案:

答案 0 :(得分:1)

在主{(1)中BackgroundPanel BP = new BackgroundPanel();之后添加此内容:

frame.add(BP);

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

答案 1 :(得分:1)

嗯......嗯,让我们看看 只需几处修改就可以重写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);

由于我现在没有办法运行它,我仍然不知道这是否解决了你的问题...你告诉我

答案 2 :(得分:0)

这可能有点太晚了,但我认为您可能需要将BackgroundPanel不透明布尔值设置为true。

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

您可能需要在任何想要拥有"背景"的区域进行此操作。颜色或图像。