1
저는 작업중인 프로그램을 약간 변경했습니다. 훌륭한 제안이있을 때 JScrollPanel 내의 JTextArea를 JList로 변경했습니다. 어떤 이유로 JList의 내용을 코딩 했더라도 영역은 공백으로 표시되고 약 1 행 높이 표시됩니다. 각 JList의 기본 크기는 (200, 200)으로 설정되어 있습니다. 어떤 아이디어?JList가 JPanel 내에 나타나지 않습니다.
코드 :
package SwingAssign2;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
public class SwingAssign2 {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SwingAssign2 window = new SwingAssign2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SwingAssign2() {
initialize();
frame.pack();
}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings("unchecked")
private void initialize() {
frame = new JFrame();
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelTop = new JPanel();
FlowLayout fl_panelTop = (FlowLayout) panelTop.getLayout();
fl_panelTop.setAlignment(FlowLayout.LEFT);
frame.getContentPane().add(panelTop, BorderLayout.NORTH);
//panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
final JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Something", "Selection 1", "Selection 2", "Selection 3", "Selection 4"}));
panelTop.add(comboBox);
comboBox.setPreferredSize(new Dimension(160, 20));
textField = new JTextField();
panelTop.add(textField);
textField.setColumns(40);
textField.setBorder(BorderFactory.createLoweredBevelBorder());
JPanel panelBottom = new JPanel();
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
panelBottom.setLayout(new BorderLayout(0, 0));
final JLabel status = new JLabel("Status");
status.setHorizontalAlignment(SwingConstants.CENTER);
panelBottom.add(status, BorderLayout.SOUTH);
status.setBorder(BorderFactory.createLoweredBevelBorder());
JScrollPane scrollPaneB = new JScrollPane();
scrollPaneB.setSize(200, 200);
String[] listB = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
"More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content",
"More and more content" };
JList listBottom = new JList(listB);
listBottom.setVisibleRowCount(12);
scrollPaneB = new JScrollPane(listBottom);
panelBottom.add(scrollPaneB);
scrollPaneB.add(listBottom);
scrollPaneB.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
scrollPaneB.setVisible(true);
listBottom.setVisible(true);
JPanel panelCenter = new JPanel();
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
panelCenter.setLayout(new GridLayout(0, 2, 0, 0));
JScrollPane scrollPaneL = new JScrollPane();
scrollPaneL.setSize(200, 200);
String[] listL = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
"More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content",
"More and more content" };
JList listLeft = new JList(listL);
listLeft.setVisibleRowCount(12);
scrollPaneL = new JScrollPane(listLeft);
panelCenter.add(scrollPaneL);
scrollPaneL.add(listLeft);
scrollPaneL.setBorder(BorderFactory.createTitledBorder("Top Left Panel"));
scrollPaneL.setVisible(true);
listLeft.setVisible(true);
JScrollPane scrollPaneR = new JScrollPane();
scrollPaneR.setSize(200, 200);
String[] listR = { "Some content on the right panel", "More content", "Some more content", "More and more content", "More and more content", "More and more content",
"More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content", "More and more content",
"More and more content" };
JList listRight = new JList(listR);
listRight.setVisibleRowCount(12);
scrollPaneR = new JScrollPane(listRight);
panelCenter.add(scrollPaneR);
scrollPaneR.add(listRight);
scrollPaneR.setBorder(BorderFactory.createTitledBorder("Top Right Panel"));
scrollPaneR.setVisible(true);
listRight.setVisible(true);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
textField.setText((String) comboBox.getSelectedItem());
status.setText("comboBox Action Listener Fired");
}
}
});
}
}
덕분 단순히 뷰포트 방법을 추가 고정. setSize도 삭제했습니다. –
@KevinSchultz : 천만에요. 수정 사항 및 ** ** ** 섹션을 참조하십시오. –
시간이 있다면이 링크에서 또 다른 질문이 있습니다. 다시 한 번 감사드립니다! http://stackoverflow.com/questions/24499976/correctly-adding-actionlistener-to-jlist-to-populate-jtextfiled –