private static final long serialVersionUID = 1L;
String[] options = {"1","2","4","8","16","20","40","100","400"} ;
int[] optionsNum = {1,2,4,8,16,20,40,100,400};
JComboBox<String> box = new JComboBox<>(options);
JLabel prompt = new JLabel("How complex do you want the circle to be?");
ImageIcon image;
Circle p = new Circle(1);
int boxindex = 0;
public CircleDrawer(){
image = new ImageIcon(p.getImage());
box.setSelectedIndex(boxindex);
setLayout(new FlowLayout());
add(new JLabel(image));
add(prompt);
add(box);
pack();
setSize(851, 950);
setTitle("Circle Drawer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == box){
boxindex = box.getSelectedIndex();
p.setComplexity(optionsNum[boxindex]);
image = new ImageIcon(p.getImage());
add(new JLabel(image));
validate();
}
}
public static void main(String[] args) {
CircleDrawer f = new CircleDrawer();
f.setVisible(true);
}
기본적으로이 코드가 있습니다. Circle
이라는 클래스를 참조합니다.이 클래스는 원의 바깥 쪽 가장자리에있는 일부 점을 계산하고 paintComponent
을 사용하여 그립니다. 이 클래스에는 메서드 paintComponent
을 가져와 BufferedImage에 넣는 메서드 getImage
이 있습니다.이 ActionListener를 JFrame을 완전히 다시 그리는 방법?
public BufferedImage getImage() {
BufferedImage hello = new BufferedImage(805, 805, BufferedImage.TYPE_INT_ARGB);
Graphics g = hello.getGraphics();
paintComponent(g);
return hello;
}
이렇게.
내가 겪고있는 문제는 완전히 JFrame
을 다시 그릴 수있는 방법을 찾을 수 없다는 것입니다. 나는 removeAll()
를 사용하여 actionPerformed
방법 내에서 JFrame
을 삭제 시도하고 완전하게 모든 것을 다시 다음 repaint
, revalidate
, 아니면 그냥 validate
그 (add
구성 요소, pack
, setSize
, setTitle
등 모든) 프레임을 설정했습니다.
add
있는 경우
다음 validate
가, 내가 업데이트되는 이미지를 볼 수 있지만 단지 (나는 FlowLayout
를 사용하여 그것을 기대하는 것과 같은) JFrame
의 말에 압정으로 고정 얻고 있지만, 그건 내가 필요한 행동이 아니야. 단지 일종의 일임을 보여줍니다.
내 질문은 다음과 같습니다. 사용자가 JComboBox
내부의 옵션을 변경하면 JFrame
을 다시 그릴 수있게하려면 어떻게해야합니까?
:
추가하면 코드의 일반적인 구조는 눈에 보이는 GUI에서 구성 요소를 (제거) /). –