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();
}
}
を試してみてください。panel.setBackground(new Color(r、g、b、a));アルファプロパティです.... http://stackoverflow.com/questions/3562225/java-set-opacity-in-jpanel質問を繰り返す? – DarkV1
完全な透明度のためには、親パネルに追加する子パネルで 'setOpaque(false)'を使うだけです。バックグラウンドで再生したり、アルファ値が0のColorを使用しないでください。「firstpanel.setOpache関数を使用してみました」 - 「mainPanel」に「firstPanel」を追加して、「mainPanel」も透明にする必要があります背景パネルの色を表示したい場合 – camickr
@camickrありがとう、それはついに働いた。そして、私はmainPanelの透明性を忘れていました。 firstPanelとsecondPanelのサイズがmainPanel内で圧縮されているのをなぜ知っていますか? – Chuck