GUI부분인데.. 거의다 했는데 마지막 한군데서막히내요..
하람
스윙에서 먼저 콤보 박스에서 사각형,원,타원중 하나를 선택해서 ok버튼을 누르면
그에 해당하는 도형이 밑에 그려지게 만든건데요..
ok버튼은 제대로 작동을 하는데
콤보밖스에 감시자를 달지 않았는데도 콤보밖스에 마우스를 클릭할때마다 패널의 paint함수가 계속 호출되는 현상이
일어나고 있습니다.. 간단한거일줄 알고 고쳐볼려고 했는데 도저히 안되네요...
어떻게 수정해야 할지 알려주세요..
그리고 이너클래스를 처음으로사용해 봤는데 이런식으로 사용하는게 맞는지도 알려주시면 감사합니다.^^
========================================================================================================
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class Choose extends JFrame{
private JPanel cp;
private Container c;
private JLabel label;
private JPanel p;
private JButton ok;
private JComboBox combo;
private Vector vecCombo;
private ActionListener buttonlistener = new Listener();
public Choose(){
super(도형 그리기);
setData();
setGUI();
setListener();
}
public void setData(){
c = getContentPane();
label = new JLabel(만든이 : 유영남);
ok = new JButton(ok);
vecCombo = new Vector();
vecCombo.add(선택);
vecCombo.add(사각형);
vecCombo.add(원);
vecCombo.add(타원);
combo = new JComboBox(vecCombo);
p = new JPanel(new FlowLayout());
cp = new CPanel();
}
public void setGUI(){
p.add(combo);
p.add(ok);
c.add(North,p);
c.add(Center,cp);
c.add(South,label);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
public void setListener(){
ok.addActionListener(buttonlistener);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
dispose();
System.exit(0);
}
});
}
public class Rect{
int x,y,w,h;
public Rect(){
this(0,0,0,0);
}
public Rect(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint(Graphics g){
g.drawRect(x,y,w,h);
}
}
public class Circle{
int x,y,w,h;
public Circle(){
this(0,0,0,0);
}
public Circle(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paint(Graphics g){
g.drawOval(x,y,w,h);
}
}
public class CPanel extends JPanel{
public CPanel(){
}
public void paint(Graphics g){
System.out.println(1);
String str = (String)combo.getSelectedItem();
if(str.equals(사각형)){
Rect r = new Rect(50,50,100,100);
r.paint(g);
}else if(str.equals(원)){
Circle c = new Circle(100,100,50,50);
c.paint(g);
}else if(str.equals(타원)){
Circle c = new Circle(100,100,100,50);
c.paint(g);
}else{
}cp.setVisible(true);
}
}
public class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
Graphics g = cp.getGraphics();
cp.setVisible(false);
cp.paint(g);
}
}
public static void main(String args[]){
new Choose();
}
}
-
참이
현재 combo의 선택과 좌표를 저장하는 것을 만들어서 OK눌렀을때 갱신되고 그 외에는 같은 좌표에 같은 모양을 반복적으로 그리도록 하면 안될까용?
-
환찬
아..원래 그런거였구나..-ㅁ-.. 그럼 혹시 그걸 막을수 있는 방법이 있나요? 버튼을 눌렀을때만 화면이 바뀌도록
하는게 목적인데 if문을 이용하면 될려나..ㅋ -
사과
콤보밖스에 감시자를 달지 않았는데도 콤보밖스에 마우스를 클릭할때마다 패널의 paint함수가 계속 호출되는 현상이 일어나고 있습니다 --- 원래 그런겁니다. ㅡ ㅡ
콤보박스가 열였다고 닫히면서 자신이 속해 있는 컴포넌트를 내부적으로 repaint() 호출하게끔 되어 있습니다.
만약, 이렇게 안된다면, 콤보박스가 열였다 닫히면 흔적이 남아있겠죠..