저는 현재 Lego Mindstorm NXT와 상호 작용할 수있는 간단한 GUI 인터페이스를 구현하고 있습니다. 현재 내 문제는 내 인터페이스의 페인트 문제입니다. 내 MainUI가로드 될 때 GridPanel을 설정하는 GirdPanel()이라는 메서드를 호출합니다. JFrame을 확장 한 MainUI가이 패널을 JFrame 호출에 추가합니다. 다음은이 문제와 관련된 MainUI의 전체 코드입니다.paint가 호출 된 후 JPanel이 JFrame에 표시되지 않습니다.
public MainUI(){
setSize(700, 600);
PathPanel pathPanel = new PathPanel(controller);
add(pathPanel, BorderLayout.WEST);
CurrentPath.getInstance().addPathDataListener(pathPanel);
CurrentPath.getInstance().addPointSelectionListener(pathPanel);
gridPanel();
add(gridPanel, BorderLayout.CENTER);
robotControlBar();
add(robotControls, BorderLayout.NORTH);
setJMenuBar(menuPanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void gridPanel(){
gridPanel = new JPanel(new BorderLayout());
WGraph graph = new WGraph();
gridPanel.add(graph, BorderLayout.CENTER);
}
WGraph는 JPanel을 확장하고이 프로그램의 그래프 표시를 제어하는 클래스입니다.
public class WGraph extends JPanel{
public WGraph(){
//WGraph Panel property setup
setLayout(new BorderLayout());
//Variable creation
points = new ArrayList<Dot>();
//Label to display coordinates of selected point
pointDisplay = new JLabel("Selected point at: None Chosen");
//testPoints(); //Comment Out when test dots not needed.
//Create Graph Panel
panel = new JPanel();
panel.setBackground(PANEL_COLOR);
//Mouse Listeners for Panel
MouseEventHandler mouseListener = new MouseEventHandler();
panel.addMouseListener(mouseListener);
panel.addMouseMotionListener(mouseListener);
//Adding components to the WGraph panel
add(pointDisplay, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
repaint();
}
public void paintComponent(Graphics g){
// invokes default painting for JFrame; must have this!
super.paintComponent(g);
// paint on the canvas rather than the JFrame
Graphics pg = panel.getGraphics();
System.out.println("*"); //Print out to see when repaint has been called. for testing only
int width = panel.getWidth();
int height = panel.getHeight();
pg.setColor(GRID_COLOR);
for (int i = 50; i < width; i+=50) {
pg.drawLine(i, 0, i, height);
}
for (int i = 50; i < width; i+=50) {
pg.drawLine(0, i, width, i);
}
Dot previousPoint = null;
for (int i = 0; i < points.size(); i++) {
Dot currentPoint = points.get(i);
currentPoint.draw(pg);
if (previousPoint != null) {
pg.setColor(Dot.DESELECTED_COLOR);
pg.drawLine(new Float(previousPoint.getCenter().x).intValue(),
new Float(previousPoint.getCenter().y).intValue(),
new Float(currentPoint.getCenter().x).intValue(),
new Float(currentPoint.getCenter().y).intValue());
}
previousPoint = currentPoint;
}
}
결국 내 문제를 설명 할 수 있습니다. 문제는 그래프 패널에 예상대로 표시되지 않는 문제입니다. 이유를 확인하려고합니다. 현재 프로그램이로드 될 때 다음과 같이 나타납니다. LINK1 그래프가 전혀 표시되지 않지만 JComboBox를 드롭 다운하면 나타납니다. LINK2 JComboBox에 항목이 선택되어 닫히면 다시 시작됩니다. LINK3 그러나 대화를 시도 할 때 다시 사라집니다. LINK4 댓글
내 JFrame 또는 JPanel 구성에서 눈에 보이는 오류가 있습니까? 무슨 일이 일어나는지 알아내는 데 도움이되는 제안이 있으십니까?
사이드 노트 : 프레임이 처음로드 될 때 페인트 기능이 세 번 호출됩니다. JComboBox가 열릴 때 한 번 더. JComboBox가 닫힐 때 한 번 더. 그리고 마침내 클릭 할 때 그래프를 사용하지 않을 때가 많습니다.
LINK1 : http://imgur.com/Z3MNx5k – ErichNova
LINK2 : HTTP : //imgur.com/z9Dy6jK – ErichNova
LINK3 : HTTP : // imgur. com/UeJM5EK – ErichNova