我正在尝试更改JFrame
的背景。
我尝试将setBackground(Color)
方法用于所有JPanel
个对象,并且只覆盖按钮和所有其他字段之间的区域。谁能在这帮助我?
输出img:
代码:
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";
}
}
答案 0 :(得分:1)
只是颜色
getContentPane().setBackground(Color.LIGHT_GRAY); //for example
在背景上放置图片
//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