0
因此,我在AP计算机科学课上,我们的最终项目是制作一个程序,显示我们学到的一堆不同概念。其中两个正在显示图像,并添加按钮。将图像添加到JFrame中现有的JPanel中
我决定只做一个俗气的基于决策的RPG,显示if-else分支。我想出了如何获得一个菜单,其中包含启动按钮,并打开一个输入对话框。但我无法弄清楚如何将图像添加到该按钮所在的同一个JFrame中。您知道它是在按钮上方或下方显示图像。我学会了如何显示图像,但这些例子都是只显示图像的扩展类。我无法弄清楚如何在现有的代码中调用某种绘图或bufferedimage方法,或者在何处放置它。也许我可以打电话给另一个有图像代码的课程?这是我到目前为止。
public class Smashing extends JPanel
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Input Dialog Box Frame");
JButton button = new JButton("Start Nigel's Adventure");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String str = JOptionPane.showInputDialog("What should Nigel do? : Enter a cardinal direction ex. n");
if (str.equals("n"))
{
JOptionPane.showMessageDialog(null, "Nigel comes upon a tree ");
String str2 = JOptionPane.showInputDialog("What should Nigel do? :");
if (str2.equals("climb"))
JOptionPane.showMessageDialog(null, "Nigel climbs up the tree ");
if (str2.equals("s"))
JOptionPane.showMessageDialog(null, "Nigel returns to the strating position ");
}
if (str.equals("s"))
{
JOptionPane.showMessageDialog(null, "Nigel comes upon boulder ");
String str3 = JOptionPane.showInputDialog("What should Nigel do? :");
}
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我进口了一堆包了,我只是没有包括在代码中,哦,这也是一个普通的应用程序类,不是一个小程序。任何帮助是极大的赞赏。
完美,JLabel实际上工作。我将它添加到我的JPanel中,现在只有一件事是它使整个面板成为一个按钮!将解决它,谢谢。 – Cal 2011-05-13 13:00:15