몇 가지 문제 : 당신은 구성 요소, textArea_1라고 여기 JTextArea에 추가하려는
- , 여러 컨테이너, 여기의 contentPane 및 JScrollPane. 하나의 구성 요소에만 구성 요소를 추가 할 수 있기 때문에 Swing에서이를 수행 할 수 없습니다. contentPane가 아닌 JScrollPane에만 추가 한 다음 GUI에 스크롤 창을 추가하십시오.
- setBounds를 통해 JTextArea의 크기를 제한하면 JScrollPane이 작동하지 않는다는 것을 거의 보장합니다. 이렇게하면 JTextArea가 표시된 것보다 더 많은 텍스트를 보유 할 때 JTextArea가 확장되는 것을 방지 할 수 있기 때문입니다. 대신 JScrollPane의 행과 열 속성을 설정하고 경계는 설정하지 마십시오. 이것은 스크롤 구획 내에 표시해야하는 행과 열의 수입니다.
- null 레이아웃을 사용 중입니다 만, JScrollPane의 사이즈를 지정하지 않기 때문에, 디폴트로 [0, 0] - 의 사이즈가됩니다. 이것이 JTextArea가 사라지는 이유입니다.. 널 레이아웃에서는 모든 구성 요소 크기와 위치를 완벽하게 지정해야합니다.
- null 레이아웃을 사용하여 GUI를 설정하고 있습니다. null 레이아웃과
setBounds()
은 복잡한 GUI를 만드는 가장 쉽고 좋은 방법처럼 Swing 초보자처럼 보일 수 있지만 더 많은 스윙 GUI를 사용하면 더 어려울 수 있습니다. 그들은 GUI의 크기를 조정할 때 구성 요소의 크기를 조정하지 않으며, 향상 시키거나 유지 보수 할 왕가이며, 스크롤 패널에 배치 될 때 완전히 실패합니다. 원래 플랫폼과 다른 모든 플랫폼이나 화면 해상도에서 볼 때 끔찍한 것처럼 보입니다. . 예를 들어
: 프로그램이 JFrame의를 확장하는 경우
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class MyGuiPanel extends JPanel {
// some row and column values for our JTextArea
private static final int TXT_AREA_ROWS = 25;
private static final int TXT_AREA_COLS = 80;
// create the JTextArea, passing in the rows and columns values
private JTextArea textArea = new JTextArea(TXT_AREA_ROWS, TXT_AREA_COLS);
public MyGuiPanel() {
// create the JScrollPane, adding our JTextArea
JScrollPane scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// lets add some buttons to the bottom of the GUI just for fun
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
buttonPanel.add(new JButton("Save"));
buttonPanel.add(new JButton("Open"));
buttonPanel.add(new JButton("Delete"));
buttonPanel.add(new JButton("Exit"));
// let's add a title to the top:
JLabel title = new JLabel("This is my Applications's Title", SwingConstants.CENTER);
title.setFont(title.getFont().deriveFont(Font.BOLD, 24)); // and make
// the text
// *BIG*
// use a BorderLayout for our GUI
setLayout(new BorderLayout(5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(scroll, BorderLayout.CENTER); // add the scrollpane to the center
add(buttonPanel, BorderLayout.PAGE_END); // the button panel to the
// bottom
add(title, BorderLayout.PAGE_START); // and the title JLabel to the top
}
private static void createAndShowGui() {
// create our GUI JPanel
MyGuiPanel mainPanel = new MyGuiPanel();
// create a JFrame to add it to
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel); // add the GUI to the JFrame
frame.pack(); // tell the layout managers to do their work
frame.setLocationByPlatform(true);
frame.setVisible(true); // display the GUI
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
또한, 디스플레이 JFrames를 작성하고 당신을 강제로 이렇게함으로써 당신은 구석에 자신을 그림하는 것을 이해는 종종 더 많은 유연성이다 를 요구했다. 사실, 내가 작성한 스윙 GUI 코드의 대부분은 이 아니고은 JFrame을 확장한다는 사실을 감안할 때 실제로이 작업을 수행하는 것은 드뭅니다. 보다 일반적으로 GUI 클래스는 JPanels를 생성 할 때 사용할 수 있습니다. JPanels는 JFrames 또는 JDialogs 또는 JTabbedPanes에 배치하거나 CardLayouts를 통해 필요할 때마다 스왑 할 수 있습니다. 이렇게하면 GUI 코딩의 유연성이 크게 향상됩니다.