그래서 저는 AP Computer Science 학급에 속해 있습니다. 우리의 최종 프로젝트는 우리가 배웠던 다양한 개념을 보여주는 프로그램을 만드는 것입니다. 두 개는 이미지 표시 및 버튼 추가입니다.JFrame 내부의 기존 JPanel에 이미지 추가
나는 if-else 브랜칭을 보여주는 치즈 맛의 결정 기반 RPG를 만들기로 결정했습니다. 시작 단추가있는 메뉴를 얻는 방법을 알아 냈고 입력 대화 상자를 엽니 다. 그러나 단추가있는 동일한 JFrame에 이미지를 추가하는 방법을 알 수는 없습니다. 단추의 위나 아래에 이미지가 표시됩니다. 이미지를 표시하는 방법을 배웠지 만 예제는 이미지 만 표시하는 확장 클래스입니다. 기존 코드 또는 배치 할 위치에서 draw 또는 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 label = new JLabel(image)
당신의 이미지가 JLabel의 추가 배치하는 방법을 결정하려면 다음의 JLabel을 만들
ImageIcon image = new ImageIcon("path_to_image");
//or
ImageIcon image = new ImageIcon (data) //where data is byte[] data
:
완벽한, JLabel 실제로 작동합니다. 내 JPanel에 추가했는데, 이제는 전체 패널을 단추로 만듭니다! 고마워. 고마워. – Cal