JTabbedPane и все все - Java

Узнай цену своей работы

Формулировка задачи:

Доброго времени суток, помогите пожалуйста разобраться с вкладками
Листинг программы
  1. package Working;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Toolkit;
  5. import javax.swing.ImageIcon;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTabbedPane;
  10.  
  11. public class Meny
  12. {
  13. public JFrame window = new JFrame("As in the war");
  14. public JTabbedPane jtp = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
  15.  
  16. public Meny()
  17. {
  18. start();
  19. }
  20.  
  21. public void start()
  22. {
  23. startJFrame();
  24. startPane();
  25. }
  26.  
  27. public void startJFrame()
  28. {
  29. window.setSize(winW, winH);
  30. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. window.getContentPane().setBackground(Color.white);
  32. window.setLayout(null);
  33. window.setResizable(false);
  34. window.setLocationRelativeTo(null);
  35. window.setVisible(true);
  36. }
  37.  
  38. public void startPane()
  39. {
  40. JLabel fonTab0 = new JLabel();
  41. jtp.addTab("Вкладка0", fonTab0);
  42. jtp.addTab("Вкладка1", fonTab0);
  43. jtp.addTab("Вкладка2", fonTab0);
  44. window.add(jtp);
  45. window.repaint();
  46. }
  47.  
  48. public static void main(String[] args)
  49. {
  50. new Meny();
  51. }
  52. }

Решение задачи: «JTabbedPane и все все»

textual
Листинг программы
  1. public class Meny {
  2.     public JFrame window = new JFrame("As in the war");
  3.     public JTabbedPane jtp = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
  4.     private int winW = 500;
  5.     private int winH = 400;
  6.  
  7.     public Meny() {
  8.         start();
  9.     }
  10.  
  11.  
  12.     public void start() {
  13.         startPane();
  14.         startJFrame();
  15.  
  16.     }
  17.  
  18.  
  19.     public void startJFrame() {
  20.         window.setSize(winW, winH);
  21.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         window.getContentPane().setBackground(Color.white);
  23.         //       window.setResizable(false);
  24.         //       window.setLocationRelativeTo(null);
  25.         window.setVisible(true);
  26.     }
  27.  
  28.  
  29.     public void startPane() {
  30.         JPanel jPanel0 = new JPanel(new FlowLayout());
  31.         JPanel jPanel1 = new JPanel(new FlowLayout());
  32.         JPanel jPanel2 = new JPanel(new FlowLayout());
  33.  
  34.         JLabel fonTab0 = new JLabel("Label 0");
  35.         JLabel fonTab1 = new JLabel("Label 1");
  36.         JLabel fonTab2 = new JLabel("Label 2");
  37.  
  38.         jPanel0.add(fonTab0);
  39.         jPanel0.add(fonTab1);
  40.  
  41.         jPanel1.add(new JCheckBox("CBox 0"));
  42.         jPanel1.add(new JCheckBox("CBox 1"));
  43.  
  44.         jPanel2.add(new JButton("Button 0"));
  45.         jPanel2.add(new JButton("Button 1"));
  46.  
  47.         JButton jButton = (JButton) jPanel2.getComponent(1);
  48.  
  49.         jButton.addActionListener(new ActionListener() {
  50.             @Override
  51.             public void actionPerformed(ActionEvent e) {
  52.                 jPanel2.add(new JButton("New Button"));
  53.                 window.validate();
  54.             }
  55.         });
  56.  
  57.         JButton jButton2 = (JButton) jPanel2.getComponent(0);
  58.  
  59.         jButton2.addActionListener(new ActionListener() {
  60.             @Override
  61.             public void actionPerformed(ActionEvent e) {
  62.                 jPanel2.remove(jPanel2.getComponents().length-1);
  63.                 window.validate();
  64.                 window.repaint();
  65.             }
  66.         });
  67.  
  68.         window.setLayout(new BorderLayout());
  69.         jtp.add("Вкладка0", jPanel0);
  70.         jtp.add("Вкладка1", jPanel1);
  71.         jtp.add("Вкладка2", jPanel2);
  72.         window.add(jtp);
  73.         window.repaint();
  74.     }
  75.  
  76.  
  77.     public static void main(String[] args) {
  78.         new Meny();
  79.     }
  80. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.818 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут