나는 Jamal의 탁구 게임 을 수정하여 자바에서 장난을 치며 이제 전체 창이 문자 그대로 800x600 이라는 것을 알았습니다. 그러나 그것은 내가 원하는 것이 아닙니다! 프레임 을 800x600으로 하고 그 주변을 창 8의 멋진 테두리로 만들고 싶습니다 . Java에 800x600 JFrame을 요청한 다음 794x571 프레임을 사용하는 것은 말이되지 않습니다!
창을 설정하는 기본 코드 :
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
table = new Table(this);
add(table);
}
내가 알아 낸 이유는 라켓 / 패들 속도를 변경하고 라켓 / 패들이 창의 양쪽 끝에 붙어서 코드를 수정 한 후 패들이 더 이상 달라 붙지 않았기 때문입니다. 보이는 프레임 (라켓 / 패들을 멈추기 위해 로직에서 0과 창의 높이를 사용했기 때문에)
이제 실제로 얼마나 많은 Java 창이 크기가 잘못되었는지 궁금합니다.
크리에이트 JPanel
포함하는을 Table
, 그 안에, 그것의 오버라이드 (override) getPreferredSize
방법을 원하는 원하는 크기를 반환
public class TableWrapperPanel extends JPanel {
private Table table;
public TableWrapperPanel(Table table) {
setLayout(new BorderLayout());
this.table = table;
add(table);
}
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
이제 메인 메소드에서의 인스턴스를 생성하고 TableWrapperPanel
"창"에 추가합니다. 그런 다음 호출 pack
하면 콘텐츠 주변에 창이 압축됩니다.
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableWrapperPanel wrapper = new TableWrapper(table);
add(wrapper );
pack();
setVisible(true);
}
또는 메서드 자체 Table
를 재정의 하는 사용자 지정 버전을 만들 수 getPreferredSize
있습니다.
public class SizableTable extends Table {
//... Constructors ...
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
메서드 Table
에서의 인스턴스를 만드는 대신의 인스턴스를 main
만들고 SizableTable
창에 추가 한 다음 pack을 호출합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다