[英]How do you add a picture in an applet (java) after a button is pressed?
I have found an odd way of putting a picture in an applet just by itself, but it doesn't seem to work when i put the code in the buttonListener for the picture to show when a button is pressed.我发现了一种将图片单独放在小程序中的奇怪方法,但是当我将代码放在 buttonListener 中以便在按下按钮时显示图片时,它似乎不起作用。 If you could also give me the simplest code for putting a picture in an applet, it would be very much appreciated!
如果您也能给我一个最简单的将图片放入小程序的代码,将不胜感激!
the code that works:有效的代码:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class gamedone extends JApplet {
public void init() {
Container cp = getContentPane();
cp.setBackground(Color.black);
Container content_pane = getContentPane();
Image img = getImage(getCodeBase(), "portal-cake.jpg");
DrawingPanel drawing_panel = new DrawingPanel(img);
// Add the DrawingPanel to the content pane.
content_pane.add(drawing_panel);
} // init
}
class DrawingPanel extends JPanel
{
Image img;
DrawingPanel (Image img)
{ this.img = img; }
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
but when this is the program i added it to, and the button doesn't make it work:但是当这是我添加的程序时,按钮不起作用:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class TypeInNames extends JApplet{
JButton StartButton;
JTextField name1, name2;
String player1, player2;
String reply;
Container cp = getContentPane();
public void init()
{
setSize(350, 400);
setLayout(null);
cp.setBackground(Color.black);
StartButton = new JButton("Start Game!");
name1 = new JTextField("Player 1",35);
name2 = new JTextField("Player 2",35);
//(x, y, width, height);
StartButton.setBounds(115,200,120,30);
name1.setBounds(115,140,120,20);
name2.setBounds(115,170,120,20);
startGame();
}
public void startGame()
{
add(StartButton);
add(name1);
add(name2);
StartButton.addActionListener(new ButtonListener());
}
public void game()
{
}
public void endGame()
{
Container cp = getContentPane();
cp.setBackground(Color.black);
Container content_pane = getContentPane();
Image img = getImage(getCodeBase(), "portal-cake.jpg");
DrawingPanel drawing_panel = new DrawingPanel(img);
// Add the DrawingPanel to the content pane.
content_pane.add(drawing_panel);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == StartButton)
{
player1 = name1.getText();
player2 = name2.getText();
remove(StartButton);
remove(name1);
remove(name2);
endGame();
repaint();
}
}
}
}
Not sure about this but you could try the following:对此不确定,但您可以尝试以下操作:
You should call setBounds()
on your drawing_panel
你应该在你的
drawing_panel
上调用setBounds()
images load asynchronously;图片异步加载; you may want to use an
ImageObserver
to know when it has loaded, then repaint
.您可能想使用
ImageObserver
来知道它何时加载,然后repaint
。 You could override your DrawingPanel.imageUpdate()
method.您可以覆盖您的
DrawingPanel.imageUpdate()
方法。
is the tree being updated?树正在更新吗? you may need to call
getContentPane().invalidate()
after adding the component.您可能需要在添加组件后调用
getContentPane().invalidate()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.