Можно ли получить прозрачный фон у JPanel? - Java
Формулировка задачи:
Доброго времени суток, у JPanel есть возможность задать прозрачность ?
package Meny; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; public class Class_Meny { public String button_name[] = {"1","2","3","4","5","6","7","8","9","10"}; public myFon fon = new myFon(); public Class_Meny() { JFrame window = new JFrame("Welcom"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(new panel_button(), BorderLayout.SOUTH); window.add(fon); window.setSize(640, 480); window.setVisible(true); } public class panel_button extends JPanel { public JPanel meny_panel = new JPanel(); public panel_button() { meny_panel.setPreferredSize(new Dimension(500, 100)); meny_panel.setLayout(new GridLayout(2, 5, 10, 10)); for (int i = 0; i < button_name.length; i++) { if (i == 100) { JLabel button_null = new JLabel(button_name[i]); meny_panel.add(button_null); } else { JButton button = new JButton(button_name[i]); button.setFocusable(false); meny_panel.add(button); ActionListener actionListener = new ButtonPushActionListener(); button.addActionListener(actionListener); } } add(meny_panel); } private class ButtonPushActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("SOURCE " + this.toString()); } } } public static void main(String[] args) { new Class_Meny(); } }
Решение задачи: «Можно ли получить прозрачный фон у JPanel?»
textual
Листинг программы
package Meny; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Desktop; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import javax.swing.JFrame; import javax.swing.JButton; public class Class_Buttons { public myFon fon = new myFon(); public int numButton = 10; public JFrame window = new JFrame("Welcom"); public String nam[] = new String[numButton]; public String way[] = new String[numButton]; public int a = -1; public Class_Buttons() { window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new BorderLayout()); panel_load(); window.add(fon); window.setSize(640, 480); window.setVisible(true); } private void panel_load() { int f = -1; try { FileInputStream fstream = new FileInputStream("data/info.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; while (true) { if ((strLine = br.readLine()) != null) { f++; nam[f] = strLine.split(";")[0]; way[f] = strLine.split(";")[1]; } else { panel_button(); break; } } } catch (Exception e) { //... } } private void panel_button() { int button_height = 50; int button_width = 100; int button_lintel = 5; int button_x = 105; int button_y = 335; for (int s = 0; s < numButton/(numButton/2); s++) { for (int i = 0; i < numButton/2; i++) { a++; JButton button = new JButton(nam[a]); button.setBounds(((button_width+button_lintel)*i)+button_x, ((button_height+button_lintel)*s)+button_y, button_width, button_height); button.setFocusable(false); if (way[a] == null) { button.setBackground(new Color(255, 255, 255, 50)); button.setContentAreaFilled(false); button.setText(""); } window.add(button); ActionListener actionListener = new ButtonPushActionListener(); button.addActionListener(actionListener); } } } private class ButtonPushActionListener implements ActionListener { int g = a; public void actionPerformed(ActionEvent e) { try { Desktop.getDesktop().open(new File(way[g])); } catch (Exception e1) { //... } } } public static void main(String[] args) { new Class_Buttons(); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д