【发布时间】:2020-05-05 22:35:28
【问题描述】:
我下面的 java 代码具有一个图像和一个按钮,我只想添加另一个按钮,该按钮与当前按钮所在的 x 轴位于同一 x 轴上。我不知道该怎么做。我以为我试图操纵 abc.weightx 并对其进行更改,但没有任何效果。我在下面附上了一张我正在尝试做的事情的图片。
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