当我想更改它的背景时,我无法更新我的“面板”。
在我的计划中,只要您在该面板中达到一定数量的点击次数,该面板就会更改其背景,但我无法这样做。
这是我的代码。
这是构造函数/ gui类
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;;
public class demo {
JFrame frame = new JFrame ("Idle Game Test!");
JPanel backGroundPanel = new JPanel ();
static JPanel statusPanel = new JPanel();
JPanel buttonPanel = new JPanel ();
JPanel bigPanel = new JPanel ();
static JTextArea message = new JTextArea (34,43);
//MessageDisplay msg = new MessageDisplay ();
//Constructor
demo () {
//gets the dimension of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
backGroundClass bgc = new backGroundClass ();
frame.setSize (850,700);
frame.setResizable (false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
//get size in pixels
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width - w)/2;
int y = (dim.height - h)/2;
//set the location
frame.setLocation (x,y);
backGroundPanel.setLayout (null);
backGroundPanel.setBackground (Color.DARK_GRAY);
frame.add (backGroundPanel);
statusPanel.setSize(250, 600);
statusPanel.setLocation (15,55);
statusPanel.add(bgc.panel);
statusPanel.addMouseListener(new mouseEvent ());
backGroundPanel.add (statusPanel);
buttonPanel.setSize (550,100);
buttonPanel.setLocation (280,555);
backGroundPanel.add (buttonPanel);
JScrollPane scroll = new JScrollPane (message, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
DefaultCaret caret = (DefaultCaret)message.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
message.setWrapStyleWord(true);
message.setLineWrap(true);
message.setBackground (Color.gray);
message.setEditable (true);
message.setForeground(Color.white);
message.setFont(new Font ("Georgia", Font.PLAIN, 12));
message.setEditable(false);
//message.setSize (500,500);
bigPanel.add(scroll);
bigPanel.setSize (550, 490);
bigPanel.setLocation (280,55);
backGroundPanel.add (bigPanel);
}
}
使面板具有背景
的类import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
class backGroundClass extends JPanel {
//ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
int controlNumber = 0 ;
String []imagePath= {"Image1Ulquiora.jpg","Image2Diva.jpg","Image2DivaSized.jpg","Image3GirlInSword.jpg"};
ImageIcon icon = new ImageIcon (getClass().getResource(imagePath[controlNumber]));
ImagePanel panel = new ImagePanel (icon.getImage());
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
以及假设更新面板的动作事件(我不确定这是否正确或是什么XD)
import java.awt.event.*;
public class mouseEvent implements MouseListener {
backGroundClass bgc = new backGroundClass ();
@Override
public void mouseClicked(MouseEvent e) {
if (!MainProg.flag){
demo.message.append(Integer.toString(e.getClickCount()));
}
if (e.getClickCount() > 5) {
bgc.controlNumber = 3;
bgc.panel.repaint(); //this does not work :<
demo.statusPanel.revalidate(); //this also
demo.statusPanel.repaint(); //same
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
我在创建w /背景图像的面板上尝试了重绘,并且面板保持了创建图像的面板(冗余XD),但不幸的是它们都不起作用。
还有其他方法来访问这些静态变量吗?我实现它们的方式有效但我确实认为还有其他方式更合适或更好但我无法弄清楚。 我希望有人可以提前帮助我,感谢更多力量!