2013-05-08 131 views
2

試圖打造出一個GUI爲我的比賽,但無論怎樣佈局我和我的工作不能得到板的巢做我喜歡嵌套面板

我的目標是這個 http://i182.photobucket.com/albums/x202/NekoLLX/CharGenmockup-1.jpg http://i182.photobucket.com/albums/x202/NekoLLX/CharGenmockup2.jpg工作

和建築離瘋狂那些外觀極好修訂我有我的左邊,因爲我喜歡它,但現在正確的躲開我

總的想法是,點擊左側菜單的標題欄將colapase(設置可見爲false)與它們關聯的內容窗格

//http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html 
//http://stackoverflow.com/questions/16430922/working-with-nested-panels 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 
import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.*; 

public class JaGCharCreation { 

//set inital size of window 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
int initalWidth = (int) screenSize.width - 50; 
int initalHeight = (int) screenSize.height - 50; 

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

//set up thread safe invoking for GUI 
public JaGCharCreation() { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
      } 

      JFrame frame = new JFrame("Testing"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLayout(new BorderLayout()); 
      frame.add(new TestPane()); 
      frame.pack(); 
      //frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
      // Give the frame an initial size. 
      frame.setSize(initalWidth, initalHeight); 
     } 
    }); 
} 

//main panel to hold all others 
public class TestPane extends JPanel { 
    public TestPane() { 
     setLayout(new GridLayout(0, 2)); 
     add(createLeftPane()); 
     add(createRightPane()); 
    }//end of class for master frame 

    protected JPanel createLeftPane() { 
     JLabel CharName = new JLabel("Character Name"); 
     CharName.setFont(new Font("Impact", Font.BOLD, 30)); 
     CharName.setBorder(new EmptyBorder(0, 81,0, 00)); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.setBorder(new EmptyBorder(10, 10, 10, 10)); 
     panel.setBackground(Color.RED); 

     JPanel content = new JPanel(new GridBagLayout()); 
     content.setOpaque(false); 

     JPanel view3D = new JPanel(); 
     view3D.setBackground(Color.DARK_GRAY); 
     JPanel view2D = new JPanel(); 
     view2D.setBackground(Color.PINK); 
     JPanel viewIsometric = new JPanel(); 
     viewIsometric.setBackground(Color.YELLOW); 
     JPanel viewData = new JPanel(); 
     viewData.setBackground(Color.MAGENTA); 

     JPanel top = new JPanel(new GridLayout(0, 2)); 
     top.setBorder(new EmptyBorder(0, 80,0, 80)); 
     top.setBackground(Color.GREEN); 
     top.add(view3D); 
     top.add(view2D); 

     JPanel bottom = new JPanel(new GridLayout(2, 0)); 
     bottom.setBorder(new EmptyBorder(0, 80,0, 80)); 
     bottom.setBackground(Color.GREEN); 
     bottom.add(viewIsometric); 
     bottom.add(new JScrollPane(viewData)); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     gbc.weighty = 0.5f; 
     gbc.weightx = 1f; 
     gbc.fill = GridBagConstraints.BOTH; 

     content.add(top, gbc); 
     content.add(bottom, gbc); 

     panel.add(content); 
     panel.add(CharName, BorderLayout.NORTH); 

     return panel; 
    }//end left pane 

    protected JPanel createRightPane() { 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.setBackground(Color.BLUE); 

     //set up our image for the title bars 
     ImageIcon icon = new ImageIcon("GradientDetail.png"); 
     Image img = icon.getImage(); 
     img = img.getScaledInstance(initalWidth/2, 40, java.awt.Image.SCALE_SMOOTH); 
     final ImageIcon iconSM = new ImageIcon(img); 

     JPanel name_panel = new JPanel(new BorderLayout()) 
      { 
      protected void paintComponent(Graphics g) 
       { 
       // Dispaly image at full size 
       g.drawImage(iconSM.getImage(), 0, 0, null); 
       super.paintComponent(g); 
       } 
      }; 
     name_panel.setOpaque(false); 
     JLabel label = new JLabel(" Character Name"); 
     label.setFont(new Font("Impact", Font.BOLD, 30)); 
     label.setForeground(Color.white); 
     label.setOpaque(false); 
     JPanel name_panel_text = new JPanel(new BorderLayout()); 
     name_panel_text.setBackground(Color.WHITE); 

     name_panel.add(label, BorderLayout.NORTH); 
     panel.add(name_panel_text); 
     panel.add(name_panel); 
     return panel; 
    }//end right pane 

    //bassed from http://stackoverflow.com/questions/7340001/determine-clicked-jpanel-component-in-the-mouselistener-event-handling 
    public class MouseAdapterMod extends MouseAdapter { 
     // usually better off with mousePressed rather than clicked 
     public void mousePressed(MouseEvent e) { 
      if (e.getSource() == "name_panel"){ 

      } 
     } 
    } 
}//end master panel set 

} //結束大師班線沿線的

+0

此GUI可調整大小?如果是這樣,GUI的外觀是什麼樣的寬度/高度 - 分配的額外空間在哪裏? – 2013-05-08 01:04:28

+1

我懷疑你主要的問題是,兩個底部窗格沒有首選大小... – MadProgrammer 2013-05-08 01:08:13

+1

@MadProgrammer好點。你也可以使用'panel.add(new JLabel(new ImageIcon(new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB))));'給***一個首選大小。 – 2013-05-08 01:23:28

回答

5

東西...

enter image description here

說實話,我試着下面的代碼,但迷路了,所以我重新編寫它...

基本上,你依靠setSize這將被佈局經理忽略和更改,因爲他們認爲合適。

本例使用GridBagLayoutweighty調整分配給前(2D/3D)的意見和底部意見的空間,但你也應該看看壓倒一切的最終部件的getPreferredSize,爲客戶提供更好的提示,佈局經理。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 

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) { 
       } 

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

    public class TestPane extends JPanel { 

     public TestPane() { 

      setLayout(new GridLayout(0, 2)); 
      add(createLeftPane()); 
      add(createRightPane()); 

     } 

     protected JPanel createLeftPane() { 

      JPanel panel = new JPanel(new BorderLayout()); 
      panel.setBorder(new EmptyBorder(10, 10, 10, 10)); 
      panel.setBackground(Color.RED); 

      JPanel content = new JPanel(new GridBagLayout()); 
      content.setOpaque(false); 

      JPanel view3D = new JPanel(); 
      view3D.setBackground(Color.DARK_GRAY); 
      JPanel view2D = new JPanel(); 
      view2D.setBackground(Color.PINK); 
      JPanel viewIsometric = new JPanel(); 
      viewIsometric.setBackground(Color.YELLOW); 
      JPanel viewData = new JPanel(); 
      viewData.setBackground(Color.MAGENTA); 

      JPanel top = new JPanel(new GridLayout(0, 2)); 
      top.setBorder(new LineBorder(Color.GREEN, 2)); 
      top.add(view3D); 
      top.add(view2D); 

      JPanel bottom = new JPanel(new GridLayout(2, 0)); 
      bottom.add(viewIsometric); 
      bottom.add(new JScrollPane(viewData)); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      gbc.weighty = 0.5f; 
      gbc.weightx = 1f; 
      gbc.fill = GridBagConstraints.BOTH; 

      content.add(top, gbc); 
      content.add(bottom, gbc); 

      panel.add(content); 
      panel.add(new JLabel("Character name"), BorderLayout.NORTH); 

      return panel; 

     } 

     protected JPanel createRightPane() { 

      JPanel panel = new JPanel(); 
      panel.setBackground(Color.BLUE); 

      return panel; 

     } 

    } 

} 
+0

精彩的,正是什麼佈局(內置)愛好者會使用:-) – 2013-05-08 02:36:43

+0

非常,非常好!把它當作我的基地,我正在修補它以添加我想要的填充物,但是謝謝!是非常好,乾淨! – NekoLLX 2013-05-09 22:49:34