2014-09-11 41 views
1

在我的Java应用程序中,我需要显示带有两个垂直面板的窗口。第一个面板在垂直列中显示一些图标。点击图标后,第二个面板应显示相应的面板。那些熟悉Videolan VLC的人,VLC中的首选项对话框显示类似的东西。使用Java Swing作为选项卡控件的两个面板显示器

我搜索了网。看起来我可以使用CardLayout工作。但是,我想知道框架是否提供了更好的构造。我认为这是一种常见的设计模式,框架中必须有一些已经建立的东西。问候。

+0

是的,'LINE_START'位置的面板列表中的'JList'(或@icza提到的'JToolBar'),'CENTER'中的'CardLayout'( 'BorderLayout')是我用于该组件的。在模态对话框中显示它。 – 2014-09-11 06:00:46

回答

2

对于“图标”面板,您可以使用JToolBar。您可以在工具栏中像这样指定垂直方向:

new JToolBar(SwingConstants.VERTICAL); 

可以增加为有BorderLayout布局管理器面板的西部或东部侧面的工具栏和内容添加到面板的中心。

作为替代你可以使用一个JTabbedPane和标签位置是左边是这样的:

new JTabbedPane(JTabbedPane.LEFT); 

这样,你就不会需要管理2个板。而选项卡窗格负责为您切换内容。如果你只想要的图标,你可以添加标签无文字:

tabbedPane.addTab("", icon, component); // empty string or you can use null 
2
  • JTabbedPane,设置为显示向左
  • JList左边的键值,以JPanel与上一个CardLayout对。从JList每个值可以保持为CardLayout关键这将显示相​​应的窗格
  • JTree,正确配置,以及让你展现hirarcal configuraiton元素...

当然,你可以只滚自己的...

Preferences

import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       System.out.println(getClass().getResource("/playlist.png")); 
       System.out.println(getClass().getResource("playlist.png")); 

       JPanel options = new JPanel(new GridBagLayout()); 
       GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.gridwidth = GridBagConstraints.REMAINDER; 
       gbc.anchor = GridBagConstraints.WEST; 
       gbc.weightx = 1; 
       gbc.fill = GridBagConstraints.HORIZONTAL; 
       gbc.insets = new Insets(1, 1, 1, 1); 
       options.add(new ConfigPane("Playlist", 
           new String[]{"Playlist", "/playlist.png"}, 
           new String[]{"Media Library", "/library.png"}), gbc); 
       options.add(new ConfigPane("My Computer", 
           new String[]{"My Videos", "/movie.png"}, 
           new String[]{"My Music", "/music.png"}, 
           new String[]{"My Pictures", "/pictures.png"}), gbc); 
       options.add(new ConfigPane("Devices", 
           new String[]{"Discs", "/disc.png"}), gbc); 
       options.add(new ConfigPane("Local Network", 
           new String[]{"Universal Plug'n'Play", "/lan.png"}, 
           new String[]{"Network streams", "/lan.png"}), gbc); 
       options.add(new ConfigPane("Internet", 
           new String[]{"Podcasts", "/podcast.png"}, 
           new String[]{"Assemblee Noationale", "/assembleenationale.png"}, 
           new String[]{"Free Music Charts", "/fmc.png"}, 
           new String[]{"Freebox TV", "/network.png"}, 
           new String[]{"Icecast Radio Directory", "/icecast.png"}, 
           new String[]{"Jamendo Selections", "/jamendo.png"}, 
           new String[]{"Channels", "/metachannels.png"} 
       ), gbc); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       gbc = new GridBagConstraints(); 
       gbc.gridx = 0; 
       gbc.gridy = 0; 
       frame.setLayout(new GridBagLayout()); 
       frame.add(new JScrollPane(options), gbc); 
       gbc.weighty = 1; 
       gbc.gridy++; 
       frame.add(new JPanel(), gbc); 

       gbc.gridx++; 
       gbc.gridy = 0; 
       gbc.weightx = 1; 
       gbc.gridheight = GridBagConstraints.REMAINDER; 
       frame.add(new JPanel(), gbc); 

       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class ConfigPane extends JPanel { 

     private JLabel title; 

     public ConfigPane(String name, String[]... options) { 
      setLayout(new GridBagLayout()); 
      title = new JLabel(name); 
      title.setFont(title.getFont().deriveFont(Font.BOLD)); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      gbc.anchor = GridBagConstraints.WEST; 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(1, 1, 1, 1); 

      add(title, gbc); 

      for (String[] option : options) { 
       System.out.println(option[0]); 
       JLabel lblOption = new JLabel(option[0]); 
       lblOption.setIcon(new ImageIcon(getClass().getResource(option[1]))); 
       add(lblOption, gbc); 
      } 
     } 

    } 

} 

当然,你需要知道什么时候上的每个选项时,用户点击和相应地更新主屏幕...

相关问题