2014-04-03 34 views
0

我正在嘗試創建一個應用程序,其中WEST區域的BorderLayout區域和CENTER中的相應面板上列出了數字列表。問題是,用卡布局調整大小和jpanel上的圖標

  • 我需要WEST區域更寬。現在,它包含JListJPanel中,將其重新調整爲默認大小。首選尺寸?不確定!
  • 我需要在1或2的右端有一個圖標或字母字符'x'。所以,當我關閉它時,我可以關閉CENTER上相應的面板。

CODE:

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.EventQueue; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.Vector; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.MatteBorder; 
import java.awt.Color; 

public class Sample1 extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Sample1 frame = new Sample1(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public Sample1() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JPanel panel = new JPanel(); 
     panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); 
     contentPane.add(panel, BorderLayout.WEST); 

     Vector<String> str = new Vector<>(); 
     str.add("1"); 
     str.add("2"); 
     final JList list = new JList(); 
     list.setListData(str); 
     panel.add(list); 

     final JPanel panel_1 = new JPanel(); 
     panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); 
     contentPane.add(panel_1, BorderLayout.CENTER); 
     panel_1.setLayout(new CardLayout(0, 0)); 

     JPanel panel_2 = new JPanel(); 
     panel_1.add(panel_2, "1"); 

     JLabel lblNewLabel = new JLabel("First"); 
     panel_2.add(lblNewLabel); 

     JPanel panel_3 = new JPanel(); 
     panel_1.add(panel_3, "2"); 

     JLabel lblNewLabel_1 = new JLabel("Second"); 
     panel_3.add(lblNewLabel_1); 


     list.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e){ 
       CardLayout layout = (CardLayout) panel_1.getLayout(); 
       layout.show(panel_1, list.getSelectedValue().toString()); 
      } 
     }); 
    } 

} 

回答

1

也許使用JTable中有兩列。第二列可以包含關閉按鈕。有關此方法的示例,請參見Table Button Column

否則,您需要將面板添加到WEST。該面板將包含JList和JList旁邊的一些其他組件,以充當關閉按鈕。這種方法的問題是讓組件排隊並讓按鈕知道要關閉哪個面板。

0

您是否嘗試過使用GridLayoutBoxLayout也嘗試使用單獨的方法設置您的GUI。

設置GUI代碼:

private void setupGUI() { 
     JPanel westP = setupWestPane(); 

     getContentPane().add(westPa, BorderLayout.WEST);   
    } 

設置西窗格代碼:

private JPanel setupWestPane() { 
    JPanel westP = setupWestPanel(); 


    JPanel p = new JPanel(new GridLayout(1, 2, 20, 0)); 
    p.setOpaque(false); 
    p.add(westP); 
    return p; 
} 

設置西面板代碼:

private JPanel setupPlayerPanel() { 
     list1 = new JList(); 
     list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

     // If you want to add a listener to the listbox 
     // list1.addListSelectionListener(this); 
     JScrollPane jsp = new JScrollPane(playerList); 

     JPanel p = new JPanel(new BorderLayout()); 
     p.setOpaque(false); 
     p.add(new JLabel("List"), BorderLayout.NORTH); 
     p.add(jsp, BorderLayout.CENTER); 

     return p; 
    } 

上面的代碼只是一個例子。