2013-03-17 107 views
4

我有一個JList的問題。當我選擇一個項目時,它自己調整大小。我如何將JList設置爲固定大小?JList java調整大小

下面是截圖之前,我選擇什麼

Screenshot before

,這是後

Screenshot after

這裏是我的代碼:

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); 

    } 
} 
+1

而且代碼是...? – 2013-03-17 16:45:03

+0

很難說沒有總體佈局和聽衆的代碼。 – skuntsel 2013-03-17 16:46:12

+0

我認爲'JScrollPane'將解決這個問題,將你的列表添加到'JScrollPane'並添加滾動窗格到框架。你可以做'frame.add(new JScrollPane(list));' – Azad 2013-03-17 16:56:28

回答

5

您需要調用在添加所有組件之後,在調用setVisible(true)之前,在您的JFrame上添加3210。這將告訴GUI的佈局管理器管理它們的佈局,並將GUI的大小調整爲組件和佈局指定的首選大小。

編輯
切勿什麼叫setSize(...)。最好覆蓋getPreferredSize()。例如,我的sscce

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.event.*; 

public class AgendaView extends JFrame { 

    private static final int PREF_W = 500; 
    private static final int PREF_H = 300; 
    private JLabel firstNameLabel, lastNameLabel, adressLabel, phoneNumberLabel, 
     extraInfoLabel; 
    private JButton editButton, addButton, deleteButton, showButton; 
    private JPanel labels, gui, buttons; 
    private DefaultListModel<String> model; 
    private JList<String> list; 
    private JScrollPane scrollPane; 

    public AgendaView() { 

     super("***Agenda View***"); 

     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 JButton("Edit"); 
     addButton = new JButton("Add"); 
     deleteButton = new JButton("Delete"); 
     showButton = new JButton("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<String>(); 
     list = new JList<String>(model); 
     String[] eles = { "Ciprian Aftode", "Andrei Batinas", "Bogdan Fichitiu", 
      "Valentin Pascau" }; 
     for (String ele : eles) { 
     model.addElement(ele); 
     } 
     list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     // list.setVisibleRowCount(-1); 
     list.addListSelectionListener(new ListSelectionListener() { 
     @Override 
     public void valueChanged(ListSelectionEvent lse) { 
      firstNameLabel.setText("First name: first name"); 
      lastNameLabel.setText("Last name: last name"); 
      adressLabel.setText("Address: Address"); 
      phoneNumberLabel.setText("Phone number: PhoneNumber"); 
      extraInfoLabel.setText("Occupation: ExtraInfo"); 
     } 
     }); 

     int ebGap = 8; 
     list.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); 

     scrollPane = new JScrollPane(list); 

     gui.add(labels, BorderLayout.CENTER); 
     gui.add(scrollPane, BorderLayout.WEST); 
     gui.add(buttons, BorderLayout.SOUTH); 
     add(gui); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 


    private static void createAndShowGui() { 
     AgendaView frame = new AgendaView(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 

} 
+0

我試過了,它不起作用,謝謝你的回答。 – Jones 2013-03-17 17:06:15

+1

@CosminAcostachioaei:請顯示您嘗試這樣做的地方,因爲我無法在您上面發佈的代碼中看到它。事實上,你在哪裏顯示用上面的代碼創建的GUI? – 2013-03-17 17:07:13

+0

我在顯示GUI的地方添加了代碼。 – Jones 2013-03-17 17:15:00