paintComponent가있는 JPanel을 JFrame에 추가하는 데 문제가 있습니다.
이것이 프레임에 추가하는 유일한 경우 작동합니다. 그러나 레이아웃 관리자를 추가하고 JFrame에 다른 구성 요소를 추가하자마자 그림이있는 패널이 더 이상 표시되지 않습니다. ,프레임에 paintComponent 패널을 추가 할 수 없습니다.
내가 안녕하세요 칠려고하고 있지 않다 현실에서 (부호를 그립니다 패널이 :
는이
작동 코드이며, JPanel을 성공적으로 표시됩니다 ...이 명확하게하려면public class SignFrame extends JFrame {
// the constructor method
public SignFrame() {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
: 여기에 단순히 코드)
public class SignPanel2 extends JPanel {
public int hello;
public void paintComponent(Graphics comp) {
Graphics g = (Graphics) comp;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(70, 250, 150, 150);
g.setColor(Color.BLACK);
if (hello > 0)
g.drawString("h",135, 270);
if (hello > 1)
g.drawString("h e",135, 270);
if (hello > 2)
g.drawString("h e l",135, 270);
if (hello > 3)
g.drawString("h e l l",135, 270);
if (hello > 4)
g.drawString("h e l l o",135, 270);
}
}
난에 패널을 넣어 프레임이다
메인 클래스
public class TheSignMain {
public static void main (String[] args) {
SignFrame signframe = new SignFrame();
}
}
위의 작품을 완벽하게 정상적으로 나에게 그것에서 원하는 그림과 프레임을 제공합니다.
프레임에 다른 구성 요소를 추가하고 레이아웃 관리자를 추가하면 더 이상 그림이 표시되지 않습니다. repaint()를 사용하더라도.
레이아웃 관리자를 포함해야합니다. 그렇지 않으면 그림이있는 패널을 추가하지만 다른 구성 요소는 추가하지 않습니다. 이것은 현재 프레임 클래스가 보이는 방식이며, 여기에서 문제가 발생합니다.
공용 클래스 SignFrame 내가 자바에 완전히 새로운 오전, 그래서 어떤 도움이 많이 이해할 수있을 것이다 JFrame의 {
// the constructor method
public SignFrame() {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// need a layout manager to decide the arrangement of the components on the container
FlowLayout flo = new FlowLayout();
// designate the layout manager to the container
content.setLayout(flo);
// make components
JPanel buttons = new JPanel();
JButton play = new JButton("Play");
JButton pause = new JButton("Pause");
JButton stop = new JButton("Stop");
// add components to a panel
buttons.add(play);
buttons.add(pause);
buttons.add(stop);
// add panel to frame container
content.add(buttons);
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
을 확장합니다. 모든 코드와 도움에 감사드립니다.
+1하지만, 좋은 질문입니다. 오늘 무언가를 배울 수있게 도와주었습니다 :-) –
@Gagandeep ... 나는 당신의 대답에 대해 무엇을했는지 모르겠습니다. JB Nizet 옆에있는 틱 아이콘을 클릭하여 허용 된 답변 (즉, 문제가 해결됨)을 표시했으나 회신이 사라졌습니다. 다른 답장은 삭제하지 않을 것이라고 생각했습니다. 사과드립니다. 오늘 등록 만 했으므로 포럼이 운영되는 방식에 익숙하지 않습니다. – phy2sma
Nah는 고의적이었습니다. 나는 그 자신을 삭제했습니다. 당신이 원했던 것과 JB Nizet이 당신에게 말했던 것이 그토록 좋습니다. 그냥 미소 지어주세요, 걱정할 것 :-) –