Можно ли получить прозрачный фон у JPanel? - Java

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

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

Доброго времени суток, у JPanel есть возможность задать прозрачность ?
Листинг программы
  1. package Meny;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JButton;
  11.  
  12. public class Class_Meny
  13. {
  14. public String button_name[] = {"1","2","3","4","5","6","7","8","9","10"};
  15. public myFon fon = new myFon();
  16. public Class_Meny()
  17. {
  18. JFrame window = new JFrame("Welcom");
  19. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. window.add(new panel_button(), BorderLayout.SOUTH);
  21. window.add(fon);
  22. window.setSize(640, 480);
  23. window.setVisible(true);
  24. }
  25.  
  26. public class panel_button extends JPanel
  27. {
  28. public JPanel meny_panel = new JPanel();
  29. public panel_button()
  30. {
  31. meny_panel.setPreferredSize(new Dimension(500, 100));
  32. meny_panel.setLayout(new GridLayout(2, 5, 10, 10));
  33. for (int i = 0; i < button_name.length; i++)
  34. {
  35. if (i == 100)
  36. {
  37. JLabel button_null = new JLabel(button_name[i]);
  38. meny_panel.add(button_null);
  39. }
  40. else
  41. {
  42. JButton button = new JButton(button_name[i]);
  43. button.setFocusable(false);
  44. meny_panel.add(button);
  45. ActionListener actionListener = new ButtonPushActionListener();
  46. button.addActionListener(actionListener);
  47. }
  48. }
  49. add(meny_panel);
  50. }
  51.  
  52. private class ButtonPushActionListener implements ActionListener
  53. {
  54. public void actionPerformed(ActionEvent e)
  55. {
  56. System.out.println("SOURCE " + this.toString());
  57. }
  58. }
  59. }
  60.  
  61. public static void main(String[] args)
  62. {
  63. new Class_Meny();
  64. }
  65. }

Решение задачи: «Можно ли получить прозрачный фон у JPanel?»

textual
Листинг программы
  1. package Meny;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Desktop;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.InputStreamReader;
  12. import javax.swing.JFrame;
  13. import javax.swing.JButton;
  14.  
  15.  
  16.  
  17. public class Class_Buttons
  18. {
  19.     public myFon fon = new myFon();
  20.     public int numButton = 10;
  21.     public JFrame window = new JFrame("Welcom");
  22.     public String nam[] = new String[numButton];
  23.     public String way[] = new String[numButton];
  24.     public int a = -1;
  25.  
  26.    
  27.     public Class_Buttons()
  28.     {
  29.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         window.setLayout(new BorderLayout());
  31.         panel_load();
  32.         window.add(fon);
  33.         window.setSize(640, 480);
  34.         window.setVisible(true);
  35.     }
  36.    
  37.    
  38.     private void panel_load()
  39.     {
  40.         int f = -1;
  41.  
  42.        
  43.         try
  44.         {
  45.             FileInputStream fstream = new FileInputStream("data/info.txt");
  46.             BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  47.             String strLine;
  48.        
  49.             while (true)
  50.             {
  51.                 if ((strLine = br.readLine()) != null)
  52.                 {
  53.                     f++;
  54.                     nam[f] = strLine.split(";")[0];
  55.                     way[f] = strLine.split(";")[1];
  56.                 }
  57.                 else
  58.                 {
  59.                      panel_button();
  60.                      break;
  61.                 }
  62.             }  
  63.         }
  64.         catch (Exception e)
  65.         {
  66.             //...
  67.         }
  68.     }
  69.  
  70.  
  71.     private void panel_button()
  72.     {
  73.         int button_height = 50;
  74.         int button_width = 100;
  75.         int button_lintel = 5;
  76.         int button_x = 105;
  77.         int button_y = 335;
  78.        
  79.    
  80.         for (int s = 0; s < numButton/(numButton/2); s++)
  81.         {
  82.             for (int i = 0; i < numButton/2; i++)
  83.             {
  84.                
  85.                 a++;
  86.                 JButton button = new JButton(nam[a]);
  87.                 button.setBounds(((button_width+button_lintel)*i)+button_x, ((button_height+button_lintel)*s)+button_y, button_width, button_height);
  88.                 button.setFocusable(false);
  89.                 if (way[a] == null)
  90.                 {
  91.                     button.setBackground(new Color(255, 255, 255, 50));
  92.                     button.setContentAreaFilled(false);
  93.                     button.setText("");
  94.                 }
  95.                 window.add(button);
  96.                 ActionListener actionListener = new ButtonPushActionListener();
  97.                 button.addActionListener(actionListener);
  98.             }
  99.         }
  100.     }
  101.        
  102.        
  103.     private class ButtonPushActionListener implements ActionListener
  104.     {
  105.         int g = a;
  106.        
  107.        
  108.         public void actionPerformed(ActionEvent e)
  109.         {
  110.             try
  111.             {
  112.                 Desktop.getDesktop().open(new File(way[g]));
  113.             }
  114.             catch (Exception e1)
  115.             {
  116.                 //...
  117.             }
  118.         }
  119.     }
  120.  
  121.        
  122.     public static void main(String[] args)
  123.     {
  124.         new Class_Buttons();
  125.     }
  126. }

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


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

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

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

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

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

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