2016-07-08 6 views
1

私が把握しようとしていることは2つあります。まず、Jcomponentの背景を透明にする方法を理解したいと思います。 JPanelを背景として色を設定した別のJPanelに配置すると、他のJPanel内に設定されているJPanelは、取り除かれないような白い背景になります。私はfirstpanel.setOpache関数を使って再描画を試みましたが、何もしません。JComponentsの背景を正確に透明にする方法

もう1つのJPanel内のJPanelを別のJPanel内に配置すると、サイズが圧縮されることに気付きました。以下の画像は、私が何を記述しようとしているかを示します。私は、JPanelのサイズを圧縮しないで、それを他のJPanelの2つのレベルに入れられるようにするために何をすべきかを知りたい。以下のコードは私が練習しているものです。

import javax.swing.*; 
import java.awt.*; 


public class LearningFrame extends JFrame { 

private JLabel userLabel; 

private LearningFrame(){ 
    JPanel backGroundPanel = new JPanel(); 
    JPanel mainPanel = new JPanel(); 
    JPanel firstPanel = new JPanel(); 
    JPanel secondPanel = new JPanel(); 

    userLabel = new JLabel("User"); 
    userLabel.setFont(new Font("Arial",1 ,24)); 

    backGroundPanel.setBackground(new Color(247,211,53)); 
    // backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS)); 

    setContentPane(backGroundPanel); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(1200,800); 
    setLocationRelativeTo(null); 

    //backGroundPanel.setLayout(null); 
    mainPanel.setLayout(new GridLayout(1,2)); 

    firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS)); 

    // firstPanel.setBackground(new Color(211,43,185)); 
    secondPanel.setBackground(new Color(34,233,44)); 


    JButton button = new JButton("Click"); 
    JButton button2 = new JButton("Click"); 
    JButton button3 = new JButton("Click"); 
    JButton button4 = new JButton("Click"); 


    firstPanel.add(button); 
    firstPanel.add(button2); 
    firstPanel.add(userLabel); 
    secondPanel.add(button3); 
    secondPanel.add(button4); 

    mainPanel.add(firstPanel); 
    mainPanel.add(secondPanel); 

    backGroundPanel.add(mainPanel); 


    setVisible(true); 

} 

public JPanel logPanel() { 

    JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 

    JTextField userTextField = new JTextField(14); 
    JTextField passTextField = new JTextField(14); 


    userLabel = new JLabel("Username: "); 
    JLabel passLabel = new JLabel("Password: "); 
    passLabel.setFont(new Font("Arial", Font.BOLD, 24)); 
    userLabel.setFont(new Font("Arial", Font.BOLD, 24)); 
    logInPanel.add(userLabel); 
    logInPanel.add(userTextField); 
    logInPanel.add(passLabel); 
    logInPanel.add(passTextField); 

    logInPanel.setOpaque(true); 
    logInPanel.repaint(); 

    return logInPanel; 
} 

public static void main(String[] args){ 

    LearningFrame x = new LearningFrame(); 
} 

} 

This image is of two JPanels within the main JPanel, the JPanels expands and are not compressed

This image is of two JPanels within a JPanel that is within another JPanel, the two JPanels are compressed.

+1

を試してみてください。panel.setBackground(new Color(r、g、b、a));アルファプロパティです.... http://stackoverflow.com/questions/3562225/java-set-opacity-in-jpanel質問を繰り返す? – DarkV1

+0

完全な透明度のためには、親パネルに追加する子パネルで 'setOpaque(false)'を使うだけです。バックグラウンドで再生したり、アルファ値が0のColorを使用しないでください。「firstpanel.setOpache関数を使用してみました」 - 「mainPanel」に「firstPanel」を追加して、「mainPanel」も透明にする必要があります背景パネルの色を表示したい場合 – camickr

+0

@camickrありがとう、それはついに働いた。そして、私はmainPanelの透明性を忘れていました。 firstPanelとsecondPanelのサイズがmainPanel内で圧縮されているのをなぜ知っていますか? – Chuck

答えて

0

ただ、コンポーネントの背景が見えなくなりますが、あなたがまだ配置されているすべてのコンポーネントを表示することができます。これは、ある

firstPanel.setOpaque(false); 

を使用その中。