제가 만들고있는 Java 응용 프로그램은 Swing 프레임 워크를 사용합니다. Swing을 처음 접했고 파일에서 일부 텍스트를 표시하려고합니다. 문제는 아래 코드를 사용하여 모든 텍스트가 표시되는 것이 아니라는 것입니다.Swing JPanel 크기 조정 : 텍스트 파일의 내용에 맞지 않습니다. 텍스트를 자릅니다.
지금까지 내가 모든 것을 포함하는 JFrame의 구성 요소를 만든코드
: 그 내
//initial frame that holds everything
frame = new JFrame(WINDOW_NAME);
frame.setSize(new Dimension((SCREEN_WIDTH.intValue()/100)*80, (SCREEN_HEIGHT.intValue()/100)*80));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
, 나는 사용자가를 통해 읽을 수 있도록 스크롤 창을 만들었습니다 세로로 스크롤하여 텍스트 파일의 내용. 그것은 꽤 큰 텍스트 파일입니다 - 책에서 몇 장.
JScrollPane scrollPane = new JScrollPane(contentPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
frame.setContentPane(scrollPane);
스크롤 창에는 JPanel 개체가 있습니다. 나는 이것을 위해 GridBagLayout 레이아웃 관리자를 활용했습니다. 패널 자체에는 텍스트 파일의 URL로 인스턴스화 한 EditorPane이 있습니다.
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
buildAndAddComponentsToFrame(contentPane);
private void buildAndAddComponentsToFrame(Container container) {
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets((frame.getHeight()/100)*10, (frame.getWidth()/100)*10, (frame.getHeight()/100)*10, (frame.getWidth()/100)*10);
//create a non-editable editor pane to hold the contents of the religious texts
JEditorPane textContainer = new JEditorPane();
textContainer.setEditable(false);
//TODO load the content of the pane from a URL (local)
File textFile = new File(*my text file*);
try {
textContainer.setPage(textFile.toURI().toURL());
} catch (IOException e) {
e.printStackTrace();
}
container.add(textContainer,c);
}
이 작동 - 나는 전체의 파일의 내용이 아닌를 읽을 수 있어요 : 그것은 단지는 JPanel의 객체의 높이에 맞 할 수 있는지 보여줍니다. ScrollPane 안에 텍스트 내용을 넣고 싶지는 않지만 JPanel의 높이를 텍스트 파일의 내용에 상대적으로 만드십시오.
이것을 달성 할 방법이 있습니까? 저는 컨테이너를 사용하여 "텍스트 영역"을 마이크로 소프트 워드 문서처럼 보이게했습니다. 삽입물은 jframe의 중간에있는 종이 문서처럼 보입니다. 이는 의도적 인 것입니다.
어떤 도움을 주셔서 감사합니다 - 미안하지만 설명이 약간 모호했습니다. 설명 할 수있는 스크린 샷을 추가했습니다.
왜 JTextPane을 JScrollPane의 뷰포트에 두지 않습니까? –
프레임 중간에 흰색 콘텐트 영역을 유지하고 싶기 때문에 검색 막대와 같이 내용 창 주위에 다른 구성 요소를 추가해야합니다. –
여전히 그 작업을 수행 할 수 있지만 JScrollPane을 둘러 쌀 수는 있습니다. –