我有一个 JList 的问题。当我选择一个项目时,它会自行调整大小。如何将 JList 设置为固定大小?
这是我选择任何内容之前的屏幕截图
这是之后
这是我的代码:
public class AgendaView extends JFrame {
private JLabel firstNameLabel, lastNameLabel, adressLabel, phoneNumberLabel, extraInfoLabel;
private Button editButton, addButton, deleteButton, showButton;
private JPanel labels, gui, buttons;
private DefaultListModel model;
private JList list;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem newItem, saveItem, saveAsItem, exitItem, openItem;
private Agenda agenda;
private JScrollPane scrollPane;
public AgendaView() {
super("***Agenda View***");
menuBar = new JMenuBar();
menu = new JMenu("Menu");
menu.add(new JSeparator());
newItem = new JMenuItem("New");
saveItem = new JMenuItem("Save");
saveItem.setEnabled(false);
saveAsItem = new JMenuItem("Save as..");
saveAsItem.setEnabled(false);
exitItem = new JMenuItem("Exit");
openItem = new JMenuItem("Open");
saveItem.add(new JSeparator());
exitItem.add(new JSeparator());
menu.add(newItem);
menu.add(openItem);
menu.add(saveItem);
menu.add(saveAsItem);
menu.add(exitItem);
gui = new JPanel(new BorderLayout(2, 2));
gui.setBorder(new TitledBorder("Owner"));
labels = new JPanel(new GridLayout(0, 1, 1, 1));
labels.setBorder(new TitledBorder("Contact "));
buttons = new JPanel(new GridLayout(1, 0, 1, 1));
editButton = new Button("Edit");
addButton = new Button("Add");
deleteButton = new Button("Delete");
showButton = new Button("Show");
editButton.setEnabled(false);
addButton.setEnabled(false);
deleteButton.setEnabled(false);
showButton.setEnabled(false);
buttons.add(showButton);
buttons.add(editButton);
buttons.add(addButton);
buttons.add(deleteButton);
firstNameLabel = new JLabel("First name: ");
lastNameLabel = new JLabel("Last name: ");
adressLabel = new JLabel("Adress: ");
phoneNumberLabel = new JLabel("Phone number: ");
extraInfoLabel = new JLabel("Extra info: ");
labels.add(firstNameLabel);
labels.add(lastNameLabel);
labels.add(adressLabel);
labels.add(phoneNumberLabel);
labels.add(extraInfoLabel);
model = new DefaultListModel();
list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setVisibleRowCount(-1);
list.addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent lse) {
String name = list.getSelectedValue().toString();
String[] split = name.split(" ");
Contact contact = agenda.searchContactbyName(split[0], split[1]);
firstNameLabel.setText("First name: " + contact.getFirstName());
lastNameLabel.setText("Last name: " + contact.getLastName());
adressLabel.setText("Adress: " + contact.getAdress());
phoneNumberLabel.setText("Phone number: " + contact.getPhoneNumber());
if (contact.getType().compareTo("Acquaintance") == 0) {
extraInfoLabel.setText("Occupation: " + contact.getExtraInfo());
} else if (contact.getType().compareTo("Colleague") == 0) {
extraInfoLabel.setText("Email: " + contact.getExtraInfo());
} else if (contact.getType().compareTo("Friend") == 0) {
extraInfoLabel.setText("Birthdate: " + contact.getExtraInfo());
} else {
extraInfoLabel.setVisible(false);
}
}
});
scrollPane = new JScrollPane(list);
gui.add(labels, BorderLayout.CENTER);
gui.add(scrollPane, BorderLayout.WEST);
gui.add(buttons, BorderLayout.SOUTH);
add(gui);
menuBar.add(menu);
setJMenuBar(menuBar);
Here is where I display the GUI:
public class JavaLab3_pb1Java {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
Agenda agenda = new Agenda();
AgendaView agendaView = new AgendaView();
agendaView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
agendaView.setSize(500, 300);
agendaView.pack();
agendaView.setVisible(true);
}
}