簡體   English   中英

Java JButton不會遍歷容器中的JLabel(帶有圖像)

[英]Java JButton are not going over the JLabel ( with image in) in a container

我已經將圖像加載到JLabel但是當我將標簽添加到容器中時,它會填充整個JFrame ,這很好,但是其他2個按鈕不在圖像頂部,並且當我單擊按鈕的含義時,什么也沒發生,因此就算是要添加按鈕,也好像按鈕不存在。 您只能看到圖像。

這是我的代碼如下:

public  void add(Container pane){

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
    URL resource = getClass().getResource("Graphitebackground.v2.jpg");


      ImageIcon i2 = new ImageIcon(resource);  
   JLabel label = new JLabel(i2);
    label.setPreferredSize(new Dimension(1000,1000));
    label.setVisible(true);
    pane.add(label);
    JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

JLabel label2 = new JLabel("");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;

pane.add(label2, c);

JLabel Label3 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;

pane.add(Label3, c);

JLabel Label4 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;

pane.add(Label4, c);

JButton b1 = new JButton("Yes");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
pane.add(b1, c);
    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            try {
                Command();
            } catch (AWTException ex) {

            } catch (InterruptedException ex) {

            }
        }
    });

    JButton b2= new JButton("No");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(b2, c);
    b2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            for (Window window : Window.getWindows()) {
                window.dispose();
            }

        }
    });


JLabel button1 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 1;   //2 columns wide
c.gridy = 2;
   //third row
pane.add(button1, c);

}



public  void Start1(){











   JFrame frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    //Set up the content pane.
    add(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    frame.setVisible(true);

}
ImageIcon i2 = new ImageIcon(resource);  
JLabel label = new JLabel(i2);
label.setPreferredSize(new Dimension(1000,1000));
label.setVisible(true);
pane.add(label);
...
pane.setLayout(new GridBagLayout());

關於以上代碼的注釋:

  1. 不要設置首選大小。 每個組件將根據組件的屬性確定自己的首選大小。 在這種情況下,首選大小將是圖像的大小。

  2. 無需setVisible(true)。 默認情況下,Swing組件是可見的(頂級容器(如JFrame,JDialog ...除外))。

  3. 在設置布局管理器之前,請勿嘗試將組件添加到面板中。 它可能對某些布局經理有用,但這是一個壞習慣,並可能導致問題。

當我將標簽添加到容器中時,它會填充整個JFrame,這很好,但是其他2個按鈕不在圖像頂部

Swing實際上以與添加到面板中相反的順序繪制組件。 因此,因為標簽是第一個添加的標簽,所以它是最后繪制的標簽,它會繪制在按鈕的頂部。

問題是您需要更改組件的層次結構。 它應該是:

    • 背景圖片(標簽)
      • 按鈕1
      • 按鈕2

因此,您的邏輯應類似於:

JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
background.add(button1, constraint...);
background.add(button2, constraint...);
frame.add(background);

請注意,JLabel不是真正的容器,因此按鈕必須適合圖像的大小,否則將無法正確顯示。

對於真實的背景圖像,您應該將圖像繪制在JPanel上,然后將按鈕添加到面板中。 有關此方法的示例,請參見背景面板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM