gpt4 book ai didi

java - 图像背景,覆盖油漆?

转载 作者:行者123 更新时间:2023-12-01 12:01:28 25 4
gpt4 key购买 nike

尝试覆盖绘画帮助:(

总是出错。我的代码在错误的地方吗?

我不知道应该将我的代码粘贴到哪里,我目前已将其公开。我是否忘记在我的 main 中放入一些东西?

如果有人能提供帮助那就太好了!

我的代码如下。

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

最佳答案

重写 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);
}
};

编辑:谢谢安德鲁·汤普森的建议。

关于java - 图像背景,覆盖油漆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com