Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. private class OptionsPanel extends JPanel{
  2. private Galaga parent;
  3.  
  4. public OptionsPanel(Galaga p) {
  5. super();
  6. parent = p;
  7.  
  8. //layout components however you wish
  9. setLayout(null);
  10. JButton backButton = new JButton("<< Back");
  11. backButton.setBounds(5, 20, 100, 20);
  12. backButton.setFont(new Font("Arial", Font.PLAIN, 15));
  13. backButton.setForeground(Color.white);
  14. backButton.setBackground(Color.black);
  15. backButton.setOpaque(true);
  16. backButton.setBorderPainted(false);
  17.  
  18. backButton.addActionListener(new ActionListener() {
  19. @Override
  20. public void actionPerformed(ActionEvent e) {
  21. parent.getLayeredPane().remove(parent.getOptionsPanel());
  22. parent.getLayeredPane().add(parent.getButtonPanel(), new Integer(10));
  23. parent.invalidate();
  24. }
  25. });
  26. add(backButton);
  27.  
  28. JButton shipSelectButton = new JButton("Ship Selection");
  29. shipSelectButton.setBounds(10, 60, 200, 40);
  30. shipSelectButton.setFont(new Font("Arial", Font.PLAIN, 20));
  31. shipSelectButton.setForeground(Color.white);
  32. shipSelectButton.setBackground(Color.black);
  33. shipSelectButton.setOpaque(true);
  34. shipSelectButton.setBorderPainted(false);
  35. shipSelectButton.addActionListener(new ActionListener() {
  36. @Override
  37. public void actionPerformed(ActionEvent e) {
  38.  
  39. }
  40. });
  41. add(shipSelectButton);
  42. }
  43. }
  44. }
  45.  
  46. import java.awt.BorderLayout;
  47. import java.awt.Color;
  48. import java.awt.Dimension;
  49. import java.awt.EventQueue;
  50. import java.awt.Font;
  51. import java.awt.GridBagConstraints;
  52. import java.awt.GridBagLayout;
  53. import java.awt.GridLayout;
  54. import java.awt.Insets;
  55. import java.awt.event.ActionEvent;
  56. import java.awt.event.ActionListener;
  57. import javax.swing.JButton;
  58. import javax.swing.JFrame;
  59. import javax.swing.JPanel;
  60. import javax.swing.UIManager;
  61. import javax.swing.UnsupportedLookAndFeelException;
  62.  
  63. public class LayoutExamples {
  64.  
  65. public static void main(String[] args) {
  66. new LayoutExamples();
  67. }
  68.  
  69. public LayoutExamples() {
  70. EventQueue.invokeLater(new Runnable() {
  71. @Override
  72. public void run() {
  73. try {
  74. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  75. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  76. }
  77.  
  78. JFrame frame = new JFrame("Testing");
  79. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80. frame.setLayout(new GridLayout(0, 2));
  81. frame.add(new OptionsPanel());
  82. frame.add(new OtherPane());
  83. frame.pack();
  84. frame.setLocationRelativeTo(null);
  85. frame.setVisible(true);
  86. }
  87. });
  88. }
  89.  
  90. public class OtherPane extends JPanel {
  91.  
  92. public OtherPane() {
  93. setBackground(Color.RED);
  94. }
  95.  
  96. @Override
  97. public Dimension getPreferredSize() {
  98. return new Dimension(100, 200);
  99. }
  100.  
  101. }
  102.  
  103. public class OptionsPanel extends JPanel {
  104.  
  105. public OptionsPanel() {
  106. super();
  107.  
  108. //layout components however you wish
  109. setLayout(new GridBagLayout());
  110. GridBagConstraints gbc = new GridBagConstraints();
  111. gbc.gridx = 0;
  112. gbc.gridy = 0;
  113. gbc.insets = new Insets(8, 8, 8, 8);
  114. gbc.anchor = GridBagConstraints.NORTHWEST;
  115.  
  116. JButton backButton = new JButton("<< Back");
  117. backButton.setFont(new Font("Arial", Font.PLAIN, 15));
  118. backButton.setForeground(Color.white);
  119. backButton.setBackground(Color.black);
  120. backButton.setOpaque(true);
  121. backButton.setBorderPainted(false);
  122.  
  123. backButton.addActionListener(new ActionListener() {
  124. @Override
  125. public void actionPerformed(ActionEvent e) {
  126. }
  127. });
  128. add(backButton, gbc);
  129.  
  130. JButton shipSelectButton = new JButton("Ship Selection");
  131. shipSelectButton.setFont(new Font("Arial", Font.PLAIN, 20));
  132. shipSelectButton.setForeground(Color.white);
  133. shipSelectButton.setBackground(Color.black);
  134. shipSelectButton.setOpaque(true);
  135. shipSelectButton.setBorderPainted(false);
  136. shipSelectButton.addActionListener(new ActionListener() {
  137. @Override
  138. public void actionPerformed(ActionEvent e) {
  139.  
  140. }
  141. });
  142. gbc.insets = new Insets(12, 8, 12, 12);
  143. gbc.gridx = 0;
  144. gbc.gridy = 1;
  145. gbc.anchor = GridBagConstraints.NORTHWEST;
  146. gbc.weighty = 1;
  147. gbc.weightx = 1;
  148. add(shipSelectButton, gbc);
  149. }
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement