JButton (스크린 샷의 오른쪽 상자)이있는 JDialog가 있습니다. JButton을 클릭하면 다른 JFrame에서 JPanel의 크기를 변경하고 싶습니다 (스크린 샷의 왼쪽 상자).JButton을 통해 JPanel 크기를 변경하십시오.
내 문제는 현재 내 응용 프로그램이 JPanel의 크기를 새 크기 (20, 20)로 변경하지 않는다는 것입니다.이 때 "click me"버튼을 클릭하면됩니다. 버튼을 클릭하면 내 testtext "Hello world"가 콘솔에 표시됩니다.
예 스크린 샷 : 스크린 샷이 표시되지 않는 경우
대체 링크 :
Dialog.java (JDialog의)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Dialog extends JDialog implements ActionListener{
public Dialog(){
JDialog d = new JDialog();
d.setSize(300, 300);
JButton b = new JButton("click me");
b.addActionListener(new Generator());
d.add(b);
d.setVisible(true);
}
}
: 여기
http://www.dachaufsetzer.net/files/images/other/java-question-1.jpg 내 코드입니다
Gener (Generator.java를 사용 JFrame의) ator.java (Frame.java에서 사용되는 인 JPanel)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Generator extends JPanel implements ActionListener{
int width;
int height;
public Generator(){
}
public Generator(int w, int h){
this.width = w;
this.height = h;
super.setBackground(Color.red);
super.setPreferredSize(new Dimension(this.width, this.height));
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(0, 0, 50, 50);
}
@Override
public void actionPerformed(ActionEvent e) {
super.setPreferredSize(new Dimension(20, 20));
System.out.println("Hello world");
}
}
Frame.java
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Frame extends JFrame {
public Frame(){
JFrame f = new JFrame();
f.setSize(400, 400);
Generator g = new Generator(2000, 2000);
JScrollPane scrollPane = new JScrollPane(g);
scrollPane.setPreferredSize(new Dimension(300,300));
f.add(scrollPane);
f.setVisible(true);
}
}
메인 .java (메인 클래스)
public class Main {
public static void main(String[] args) {
Frame window = new Frame();
Dialog d = new Dialog();
}
}
지원해 주셔서 감사합니다.
큰 도움을 주신 Flood2d에게 감사드립니다. 이제는 분명합니다! – spiceflo