java - 如何更改由 JPanel 组成的 JFrame 中的背景?

标签 java swing import

我正在尝试更改我的 JFrame 的背景。 我尝试对所有 JPanel 对象使用 setBackground(Color) 方法,并且仅覆盖按钮和所有其他字段之间覆盖的区域。有人可以帮我吗?

输出图像:

代码:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JOptionPane;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
public class calculator extends JFrame implements ActionListener, KeyListener
{
   public JButton[] dig=new JButton[10];
   public JButton sin,cos,tan,toNegative,add,subtract,divide,multiply,quad,clear,equals,result,back;
   public JTextField txt,a,b,c;
   public JPanel inputField,digits,quadSwitcher,eqCls,extras,zero,addSubt;
   public int width=280,height=400;
   public static String input="";
   private ImageIcon img;
   private Color color1=Color.ORANGE;
   public calculator()
   {
       super("Calculator");try{
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
       img=new ImageIcon("calc.png");
       this.setIconImage(img.getImage());
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       this.setLayout(new FlowLayout());
       setSize(width,height);
       txt=new JTextField(null,20);
       txt.setEditable(true);
       inputField=new JPanel();
       txt.setPreferredSize(new Dimension(width-50,30));
       inputField.add(txt);
       this.add(inputField);
       txt.addKeyListener(this);
       for(int i=0;i<10;i++)
        dig[i]=new JButton(i+"");
       sin=new JButton("sin");
       cos=new JButton("cos");
       tan=new JButton("tan");
       toNegative=new JButton("+/-");
       add=new JButton("add");
       subtract=new JButton("Subtract");
       add=new JButton("Add");
       multiply=new JButton("Multiply");
       quad=new JButton("Quadratic Equation");
       quad.addActionListener(this);
       divide=new JButton("Divide");
       equals=new JButton("=");
       quadSwitcher=new JPanel();
       quadSwitcher.add(quad);
       this.add(quadSwitcher);
       digits=new JPanel();
       digits.setLayout(new GridLayout(3,3,5,5));
       for(int i=9;i>=1;i--)
        digits.add(dig[i]);
       extras=new JPanel();
       extras.setLayout(new GridLayout(2,3,4,4));
       extras.add(sin);
       extras.add(cos);
       extras.add(tan);
       extras.add(toNegative);
       extras.add(multiply);
       extras.add(divide);
       this.add(digits);
       zero=new JPanel();
       dig[0].setPreferredSize(new Dimension((width/2)-10,25));
       zero.add(dig[0]);
       this.add(zero);
       this.add(extras);
       addSubt=new JPanel();
       addSubt.setLayout(new GridLayout(1,2,10,0));
       addSubt.setPreferredSize(new Dimension(width-35,30));
       addSubt.add(add);
       addSubt.add(subtract);
       this.add(addSubt);
       eqCls=new JPanel();
       eqCls.setLayout(new GridLayout(1,2,10,0));
       eqCls.setPreferredSize(new Dimension(width-35,30));
       clear=new JButton("clear");
       eqCls.add(equals);
       eqCls.add(clear);
       clear.addActionListener(this);
       this.add(eqCls);
       for(int i=0;i<10;i++)
        dig[i].addActionListener(this);
       sin.addActionListener(this);
       cos.addActionListener(this);
       tan.addActionListener(this);
       toNegative.addActionListener(this);
       equals.addActionListener(this);
       add.addActionListener(this);
       subtract.addActionListener(this);
       multiply.addActionListener(this);
       divide.addActionListener(this);
       setVisible(true);
       this.setBackground(Color.ORANGE);
       inputField.setBackground(color1);
       quadSwitcher.setBackground(color1);
       eqCls.setBackground(color1);
       addSubt.setBackground(color1);
       digits.setBackground(color1);
       extras.setBackground(color1);
       zero.setBackground(color1);
       inputField.setOpaque(true);
       JOptionPane.showMessageDialog(this,"Developed By Saksham Puri.");
   }
   public static void main(String args[])
   {
       calculator ob=new calculator();
    }
   @Override
   public void keyPressed(KeyEvent e)
   {
      int keyCode=e.getKeyCode();
      if(keyCode==KeyEvent.VK_ENTER){
       input=txt.getText();
       txt.setText(performOperation(input));}
   }
   @Override
   public void actionPerformed(ActionEvent e)
   {
       String str=e.getActionCommand();
       if(Character.isDigit(str.charAt(0))) txt.setText(txt.getText()+""+str);
       else if(str.equalsIgnoreCase("clear"))
        txt.setText("");
       else if(str.equalsIgnoreCase("tan")) txt.setText(txt.getText()+""+str);
        else if(str.equalsIgnoreCase("sin"))txt.setText(txt.getText()+""+str);
         else if(str.equalsIgnoreCase("cos"))txt.setText(txt.getText()+""+str);
          else if(str.equalsIgnoreCase("add"))txt.setText(txt.getText()+""+"+");
           else if(str.equalsIgnoreCase("subtract"))txt.setText(txt.getText()+""+"-");
            else if(str.equalsIgnoreCase("+/-"))txt.setText(txt.getText()+""+"-");
             else if(str.equalsIgnoreCase("multiply"))txt.setText(txt.getText()+""+"*");
              else if(str.equalsIgnoreCase("divide"))txt.setText(txt.getText()+""+"/");
               else if(str.equalsIgnoreCase("=")){input=txt.getText(); txt.setText(performOperation(input));}
                else if(str.equalsIgnoreCase("Quadratic Equation")) setQuad();
                 else if(str.equalsIgnoreCase("back"))back();
                  else if(str.equalsIgnoreCase("calculate")){back(); txt.setText(calcQuad()); revalidate(); repaint();}
   }
   public void back()
   {
       inputField.removeAll();
       quadSwitcher.removeAll();
       inputField.add(txt);
       quadSwitcher.add(quad);
       revalidate();
       repaint();
   }
   public String calcQuad()
   {
       return Quad.solveQuad(Integer.parseInt(a.getText()),Integer.parseInt(b.getText()),Integer.parseInt(c.getText()));
   }
   public void setQuad()
   {
       inputField.removeAll();
       quadSwitcher.removeAll();
       a=new JTextField("a",4);
       a.setEditable(true);
       b=new JTextField("b",4);
       b.setEditable(true);
       c=new JTextField("c",4);
       c.setEditable(true);
       inputField.add(a);
       inputField.add(new JLabel("x^2   +  "));
       inputField.add(b);
       inputField.add(new JLabel("x     +  "));
       inputField.add(c);
       result=new JButton("Calculate");
       back=new JButton("Back");
       back.addActionListener(this);
       result.addActionListener(this);
       quadSwitcher.add(result);
       quadSwitcher.add(back);
       revalidate();
       repaint();
   }
   @Override
   public void keyReleased(KeyEvent e){}
   public void keyTyped(KeyEvent e){}
   public String performOperation(String str)
   {
       return "Performed Operation";
   }
}

最佳答案

  1. 只是颜色

    getContentPane().setBackground(Color.LIGHT_GRAY); //for example
    
  2. 将图像放在背景上

     //create a JComponent to store your image 
     class ImagePanel extends JComponent
     {
    private Image image;
    public ImagePanel(String str) {
        BufferedImage image=null;
        try {
            image = ImageIO.read(new File(str));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.image = image;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
    }
    
    //in your JFrame class
    ImagePanel contentPane = new ImagePanel("./background.png");
    this.setContentPane(contentPane);
    getContentPane().setBackground(Color.LIGHT_GRAY);// just in case your image does not fit the entire view
    

关于java - 如何更改由 JPanel 组成的 JFrame 中的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151268/

相关文章:

java - 用于简单图像编辑的 SWING 组件

java - AWT 和 Swing 的 future ?

html - 可滚动的 html JLabel

database - Oracle -- 将数据导入到具有不同名称的表中?

sql-server - 将accdb格式的Access数据库导入SQL Server 2014(Express版)

java - 说明 数组列表

java - 数组的累积和

java - JBoss 5.1.0 企业版。随机出现 java.lang.NoClassDefFoundError

java - 从 Java 中的 httprequest 获取重定向

php - Sublime Text 导入 php 类有什么解决方案吗? Netbeans 上类似于 "Fast Import"的东西?