JPanel背景帮助。我尝试过但都失败了

时间:2015-01-14 23:40:17

标签: java swing jpanel

我需要将图像设置为我创建的JPanel的背景。我做了一个基本的计算器。

我尝试了一个有人告诉我尝试的代码,它将图像设置为背景但现在我丢失了计算器按钮等。我猜这是因为我将它添加到我的JFrame中?

但我不知道从哪里开始。

我是新手,我知道我的代码可能不是那么好,但它现在有效。它会被观察和批评,但是现在我需要添加这样的背景:/ 请有人帮忙,谢谢!

请原谅名称GridBag2。

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

  /**
 * Created by IntelliJ IDEA.
 * Date: 09/01/2015
 * Time: 15:52
 */
public class GridBag2 extends JFrame implements ActionListener
{


   private JPanel panel=new JPanel(new GridBagLayout());


   private GridBagConstraints gBC = new GridBagConstraints();

   private JButton zeroButton = new JButton("0");
   private JButton oneButton = new JButton("1");
   private JButton twoButton = new JButton("2");
   private JButton threeButton = new JButton("3");
   private JButton fourButton = new JButton("4");
   private JButton fiveButton = new JButton("5");
   private JButton sixButton = new JButton("6");
   private JButton sevenButton = new JButton("7");
   private JButton eightButton = new JButton("8");
   private JButton nineButton = new JButton("9");

   private JButton addButton = new JButton("+");
   private JButton subButton = new JButton("−");
   private JButton multButton = new JButton(" X ");
   private JButton divideButton = new JButton(" ÷ ");
   private JButton equalButton= new JButton("=");
   private JButton clearButton = new JButton("C");


   private JTextArea input=new JTextArea("");



   Double number1,number2,result;
   int add=0,sub=0,mult=0,divide=0;



   public GridBag2()
   {


      zeroButton.addActionListener(this);
      oneButton.addActionListener(this);
      twoButton.addActionListener(this);
      threeButton.addActionListener(this);
      fourButton.addActionListener(this);
      fiveButton.addActionListener(this);
      sixButton.addActionListener(this);
      sevenButton.addActionListener(this);
      eightButton.addActionListener(this);
      nineButton.addActionListener(this);

      addButton.addActionListener(this);
      subButton.addActionListener(this);
      multButton.addActionListener(this);
      divideButton.addActionListener(this);
      equalButton.addActionListener(this);
      clearButton.addActionListener(this);



      gBC.insets = new Insets(5, 5, 5, 5);


      gBC.gridx = 1;
      gBC.gridy = 0;
      gBC.gridwidth = 4;
      gBC.fill = GridBagConstraints.BOTH;
      panel.add(input, gBC);


      gBC.gridx = 2;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(zeroButton, gBC);


      gBC.gridx = 1;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(oneButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(twoButton, gBC);


      gBC.gridx = 2;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(threeButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(fourButton, gBC);

      gBC.gridx = 2;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(fiveButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(sixButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(sevenButton, gBC);

      gBC.gridx = 2;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(eightButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(nineButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(addButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(subButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(divideButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(multButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(equalButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(clearButton, gBC);

      setVisible(true);
      setSize(300, 340);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      input.setEditable(false);

      getContentPane().add(panel);


      setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));




   }//GridBag2


   public void actionPerformed(ActionEvent e)
   {

      Object source = e.getSource();

      if(source==clearButton)
      {
         number1=0.0;
         number2=0.0;
         input.setText("");
      }//if

      if (source == zeroButton)
      {
         input.setText(input.getText() + "0");
      }//if

      if (source == oneButton)
      {
         input.setText(input.getText() + "1");
      }//if
      if (source == twoButton)
      {
         input.setText(input.getText() + "2");
      }//if
      if (source == threeButton)
      {
         input.setText(input.getText() + "3");
      }//if
      if (source == fourButton)
      {
         input.setText(input.getText() + "4");
      }//if
      if (source == fiveButton)
      {
         input.setText(input.getText() + "5");
      }//if
      if (source == sixButton)
      {
         input.setText(input.getText() + "6");
      }//if
      if (source == sevenButton)
      {
         input.setText(input.getText() + "7");
      }//if
      if (source == eightButton)
      {
         input.setText(input.getText() + "8");
      }//if
      if (source == nineButton)
      {
         input.setText(input.getText() + "9");
      }//if
      if (source == addButton)
      {
         number1=number_reader();
         input.setText("");
         add=1;
         sub=0;
         divide=0;
         mult=0;
      }//if
      if (source == subButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=1;
         divide=0;
         mult=0;
      }//if
      if (source == divideButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=0;
         divide=1;
         mult=0;
      }//if
      if (source == multButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=0;
         divide=0;
         mult=1;
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(add>0)
         {
            result=number1+number2;
            input.setText(Double.toString(result));
         }//if
      }//if

      if(source==equalButton)
      { number2=number_reader();

         if(sub>0)
         {
            result=number1-number2;
            input.setText(Double.toString(result));
         }//if
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(divide>0)
         {
            result=number1/number2;
            input.setText(Double.toString(result));
         }//if
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(mult>0)
         {
            result=number1*number2;
            input.setText(Double.toString(result));
         }//if
      }//if
   }//actionPerformed


   public double number_reader()
   {
      Double num1;
      String s;
      s=input.getText();
      num1=Double.valueOf(s);

      return num1;
   }//number_reader



   public static void main(String[] args){

      GridBag2 gui = new GridBag2();


   }//main



}//class

2 个答案:

答案 0 :(得分:0)

setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));

此行不会将JLabel添加到您panel。这一行使JLabel成为JFrame的根控件。 使用JLabel替换 JFrame的内容窗格 - 一个JFrame只有一个内容窗格。

您可能希望以与其他控件相同的方式对待它。类似的东西:

JLabel background = new JLabel(new new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg"));
gBC.gridx = some number;
gBC.gridy = some number;
gBC.gridwidth = some number;
gBC.gridheight = some number;
panel.add(background, gBC);

答案 1 :(得分:-2)

getContentPane().add(panel);

setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));

我的猜测是你需要切换这两行的顺序。您已将JPanel添加到JFrame的默认内容窗格中,但之后您将内容窗格设置为其他内容(JLabel),从而有效地丢弃了您添加了内容窗格的旧内容窗格小组到。

另一方面,在图像文件名中使用java.io.File.separator是一种很好的做法,而不是将反斜杠硬编码到字符串中。所以你会写:

"C:"+File.separator+"Users"+File.separator+"Cara"+File.separator+"Pictures"+File.separator+"christmas.jpg"

同时确保在代码顶部导入java.io.File。这可以确保它不会在使用正斜杠而不是像Windows那样的反斜杠的基于unix的系统上中断。