在jFrame的同一x轴上创建2个按钮

问题描述 投票:0回答:1

下面的我的Java代码以图像和下面的按钮为特征,我只想添加与当前按钮所在的x轴在同一x轴上的另一个按钮。我不知道该怎么做。我以为我试图操纵abc.weightx并对其进行更改,但这没有任何效果。我在下面附有一张我要尝试做的照片。

enter image description here

    import java.awt.GridBagConstraints;
   import java.awt.GridBagLayout;
    import java.io.IOException;
    import java.net.URL;
   import javax.swing.ImageIcon;
    import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;

    class Main extends JFrame {

public static void main(String[] args0) {

    try {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        frame.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 0.5;
        gbc.weighty = 0.4;
        gbc.fill = GridBagConstraints.BOTH;

        URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
        ImageIcon image = new ImageIcon(url);
        JLabel imageLabel = new JLabel(image);
        frame.add(imageLabel, gbc);

        gbc.weightx = 0.9;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.NONE;







        JButton b = new JButton("Click Here");
        frame.add(b, gbc);

        frame.pack();
        frame.setVisible(true);

    } catch (IOException e) {
        e.printStackTrace();
    }
}}
java swing layout layout-manager
1个回答
0
投票

将按钮置于中心流布局中。将流布局放在边框布局的页面末端。

© www.soinside.com 2019 - 2024. All rights reserved.