2012-07-19 103 views
2

這裏是繪製GUI的地方(注意,類擴展了JFrame)。Java GUI自動調整大小

public Cache() { 
    SubstanceColorChooserUI col = new SubstanceColorChooserUI(); 
    while (mode == 0); 
    setResizable(false); 
    setTitle("Cache"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 483, 374); 
    setLocationRelativeTo(null); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(new BorderLayout()); 

    textField = new JTextField(); 
    textField.setBounds(10, 11, 328, 20); 
    contentPane.add(textField); 
    textField.setColumns(10); 

    JButton btnLoadCache = new JButton("Load cache"); 
    btnLoadCache.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      try { 
       String loc = textField.getText(); 
       if (loc.equals("")) { 
        JOptionPane.showMessageDialog(Cache.this, "Please specify a location for the cache.", "Unable to load", JOptionPane.ERROR_MESSAGE); 
        return; 
       } 
       if (!loc.endsWith("\\")) 
        loc = loc + "\\"; 
       cache = new Store(loc); 
       loadImages(); 
      } catch (Exception e) { 
       JOptionPane.showMessageDialog(Cache.this, "Cache failed to initialize.\n" + e.getMessage(), "Unable to load", JOptionPane.ERROR_MESSAGE); 
      } 
     } 
    }); 
    btnLoadCache.setBounds(351, 9, 112, 23); 
    contentPane.add(btnLoadCache); 

    JSplitPane splitPane = new JSplitPane(); 
    splitPane.setDividerLocation(150); 
    splitPane.setContinuousLayout(true); 
    splitPane.setBounds(10, 44, 453, 279); 
    contentPane.add(splitPane); 

    JPanel panellie = new JPanel(); 
    panellie.setLayout(new BorderLayout(0, 0)); 
    panel = new ImagePanel(null); 

    scrollPane_1 = new JScrollPane(); 
    panellie.add(scrollPane_1, "Center"); 

    scrollPane_1.setViewportView(panel); 
    splitPane.setRightComponent(panellie); 

    JPanel panel_1 = new JPanel(); 
    splitPane.setLeftComponent(panel_1); 
    panel_1.setLayout(new BorderLayout(0, 0)); 

    JScrollPane scrollPane = new JScrollPane(); 
    panel_1.add(scrollPane, BorderLayout.CENTER); 

    model = new DefaultListModel<ListedImage>(); 
    list = new JList<ListedImage>(model); 
    list.addListSelectionListener(new ListSelectionListener() { 
     public void valueChanged(ListSelectionEvent arg0) { 
      if (arg0.getValueIsAdjusting()) 
       return; 
      ListedImage img = list.getModel().getElementAt(list.getSelectedIndex()); 
      panel.setImage(img.getImage()); 
     } 
    }); 
    scrollPane.setViewportView(list); 

    progressBar = new JProgressBar(); 
    progressBar.setStringPainted(true); 
    progressBar.setDoubleBuffered(true); 
    progressBar.setBounds(10, 328, 453, 14); 
    contentPane.add(progressBar); 

} 

我怎麼會讓它所以當我setResizeable爲真,我開始拖動程序到更大和更小的,我怎麼讓這個框架內的組件(按鈕,標籤等)在整個幀調整大小時調整大小

+0

你厭倦了將控件錨定到JFrame嗎? – M2X 2012-07-20 06:29:49

回答

6

使用佈局管理器而不是爲每個組件設置邊界。

它將根據程序的不同而有所不同,您希望組件的移動方式如何。

看看this,並嘗試查看哪種佈局最適合您。

+1

問題是,我喜歡我的組件在一個確切的位置。我真的不喜歡使用一般點。 – 2012-07-19 18:25:14

+1

那麼,如果您使用絕對定位,您將無法輕鬆地隨着框架更改而更改組件大小。唯一的其他選擇是使所有邊界都依賴於幀。這是一個痛苦,我會強烈**建議反對它。從長遠來看,'LayoutManagers'是一個更好的選擇,它將爲您提供更專業的外觀。 – 2012-07-19 18:27:04

+0

我會嘗試LayoutManagers,感謝您的建議。 – 2012-07-19 19:06:16

1

您可以使用setSize()方法來設置你的JFrame的大小

2.您可以選擇將大小JFrame的設置isResizable(false)固定

使用的GroupLayout,通過NetBeans團隊在2005年開發,請嘗試使用Windows生成器臨由谷歌免費提供了。在這裏,您可以選擇GroupLayout,然後將組件拖放到JFrame中,即使您希望您可以手動編輯GUI代碼。

+0

我正在使用eclipse的窗口生成器插件 – 2012-07-19 18:38:06