我想創建一個這樣的框架。這是一個0.9不透明度的主框架,並擁有兩個JPanel
s。其中一個面板是黑色的,另一個面板沒有任何背景色e:g setOpaque(false);
。
這裏是我試過,但沒有奏效:
的JFrame
public class STBG {
JFrame frame = new JFrame("Panel");
BorderLayout bl = new BorderLayout();
public STBG(){
frame.setLayout(bl);
frame.setSize(800, 600);
frame.add(new LeftBar(), BorderLayout.WEST);
frame.add(new MainPanel(), BorderLayout.CENTER);
//frame.setUndecorated(true);
frame.setBackground(new Color(0, 255, 0, 1));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
new STBG();
}
}
左側面板
public class LeftBar extends JPanel{
JPanel menuPanel;
JLabel schedule, history, settings, aboutApp;
public LeftBar(){
setBackground(Color.BLACK);
setPreferredSize(new Dimension(100, 20));
setLayout(new BorderLayout());
menuPanel = new JPanel(new GridLayout(5,0));
menuPanel.setBackground(Color.black);
settings = new JLabel("Settings");
schedule = new JLabel("Schedule");
history = new JLabel("History");
menuPanel.add(settings);
menuPanel.add(schedule);
menuPanel.add(history);
aboutApp = new JLabel("About App");
add(menuPanel, BorderLayout.CENTER);
add(aboutApp, BorderLayout.SOUTH);
}
}
主面板
public class MainPanel extends JPanel{
public MainPanel(){
setLayout(new GridLayout(0,1));
add(new JLabel("TEXT"));
setOpaque(false);
setBackground(new Color(0, 0, 0, 1));
}
}
任何想法如何使圖像類似?
不使'JFrame'未裝飾,這是不可能的。此外,Swing不理解如何處理基於alpha的顏色,相反,您需要使面板透明並且「假」填充 – MadProgrammer
@MadProgrammer感謝您的快速響應。如果我使'JFrame'未修飾,那麼關閉按鈕和最小化會消失。我試圖避免爲他們定製按鈕。無論如何,你可以通過僞造填充來解釋你的意思嗎?還是一個例子?謝謝 – Dan
看看https://harryjoy.wordpress.com/2011/07/09/make-jframe-transparent/ – Anptk