2012-11-19 27 views
0

我做了一个Java swing应用程序。我的主要课程是整个SwingUtilities.invokeLater(new Runnable()的东西。这里的一切是我用过JSplitPane - 将应用程序拆分为两个?

我的第二类:

JPanel container = (JPanel) getContentPane(); 

然后通过调用添加了所有位..

container.add([name of component] 

我现在想获得这整个“应用”变成JSplitPane。因此,我希望我的应用程序在一边,而另一边在右边。

我该怎么做?

public class one{ 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
      JFrame f = new App("main"); 
      f.setSize(1920,1080); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      f.setVisible(true); 
} 


public class App extends JFrame { 
     SuppressWarnings("empty-statement") 
     public App(String title) { 
     super(title); 
     JPanel container = (JPanel) getContentPane(); 

     container.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     JButton new button = new JButton("new"); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 2; 
     //ENTER LOTS OF CONSTRAINTS 
     container.add(new, c); 

     JButton next to button = new JButton("next to"); 
     c.gridx = 1; 
     c.gridy = 1; 
     c.gridwidth = 2; 
     //ENTER LOTS OF CONSTRAINTS 
     container.add(new, c); 

     //I HAVE LOTS OF SWING COMPONENTS AND BUTTONS about 30 

} 

我希望所有这些都在拆分窗格的左侧?

我该怎么做?

+2

的一种方法是把内容转换成两个容器(例如'JPanel')并将它们添加到拆分窗格中,然后将拆分窗格添加到内容窗格中。为了更快地获得更好的帮助,请发布当前代码的[SSCCE](http://sscce.org/)(而不是代码片段和说明)。 –

+0

更好吗?我只是写了一些模拟代码来演示我的观点。 – Jay

+0

我看不出任何理由,为什么你应该创建'one'类来放置主要方法。 –

回答

5
  1. 不要从JFrame扩展,你不添加任何功能,而是将所有你的应用程序组件和逻辑来独立JPanel
  2. 创建JSplitPane的实例,添加“主”面板它,二级面板添加你给它
  3. 创建JFrame的实例,拆分窗格添加到它...

修订

enter image description here

public class TestSplitPane { 

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

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

       JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 
       pane.setLeftComponent(new MainPane()); 
       pane.setRightComponent(new JLabel("On the right")); 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(pane); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class MainPane extends JPanel { 

     public MainPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 
      JButton newButton = new JButton("new"); 
      c.gridx = 0; 
      c.gridy = 0; 
      c.gridwidth = 2; 
      //ENTER LOTS OF CONSTRAINTS 
      add(newButton, c); 

      JButton next = new JButton("next to"); 
      c.gridx = 1; 
      c.gridy = 1; 
      c.gridwidth = 2; 
      //ENTER LOTS OF CONSTRAINTS 
      add(next, c); 
     } 
    } 
} 
+0

有什么机会可以给我打个快速的SSCCE吗? – Jay

+0

完成........... – MadProgrammer

+0

我刚将这些方法应用于我的代码,它的工作! – Jay