试图覆盖绘画帮助:(
一直出错。我的代码是在错误的地方吗?
我不知道在哪里粘贴我的代码我目前在公开场合使用它。我忘了把东西放在我的主体里吗?
如果有人可以提供帮助,那就太棒了!
我的代码如下。
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.Panel;
/**
* Created by IntelliJ IDEA.
* User: Cara
* Date: 09/01/2015
* Time: 15:52
* Program using SWING components to create a Christmas themed Calculator.
*/
public class GridBag2 extends JFrame implements ActionListener
{
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("");
public Image img;
public JPanel panel;
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(335, 340);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
input.setEditable(false);
getContentPane().add(panel);
try {
img = ImageIO.read(new File("C:\\Users\\Cara\\Pictures\\christmas.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
panel = new JPanel(new GridBagLayout()){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
};
}//ChristmasCalculator
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
答案 0 :(得分:1)
覆盖JPanel的paintComponent()
方法,如下所示,然后删除你称之为背景的JLabel,因为这不需要。
private Image img;
private JPanel panel;
try {
img = ImageIO.read(new File("C:\\Users\\Cara\\Pictures\\christmas.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
panel = new JPanel(new GridBagLayout()){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
};
编辑:谢谢Andrew Thompson的建议。