Java 如何在GridBagLayout上放置背景图像
我第一次尝试与布局经理合作,他们只是在踢我的牙。我正在尝试制作一个背景图像,然后使用GridBagLayout将按钮放在顶部,如果有更好的layoutmanager,请告诉我。至于试图学习如何使用布局管理器,这是非常困难的,任何学习参考也将不胜感激。 这就是它现在的样子, 我可以获得显示完整图像的框架,但当我使用gridlayout管理器时,它可以做到这一点Java 如何在GridBagLayout上放置背景图像,java,image,swing,jpanel,gridbaglayout,Java,Image,Swing,Jpanel,Gridbaglayout,我第一次尝试与布局经理合作,他们只是在踢我的牙。我正在尝试制作一个背景图像,然后使用GridBagLayout将按钮放在顶部,如果有更好的layoutmanager,请告诉我。至于试图学习如何使用布局管理器,这是非常困难的,任何学习参考也将不胜感激。 这就是它现在的样子, 我可以获得显示完整图像的框架,但当我使用gridlayout管理器时,它可以做到这一点 public void addComponentsToPane(Container pane){ BackgroundImage
public void addComponentsToPane(Container pane){
BackgroundImage image = new BackgroundImage();
JButton button1, button2, button3, button4, button5;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if(shouldFill){
c.fill = GridBagConstraints.NONE;
}
button1 = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
button1.setOpaque(false);
pane.add(button1, c);
button2 = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
button2.setOpaque(false);
pane.add(button2, c);
button3 = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
button3.setOpaque(false);
pane.add(button3, c);
button4 = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button4, c);
button5 = new JButton("button 1");
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 = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button5, c);
c.ipadx = 800;
c.ipady = 400;
pane.add(image, c);
}
这就是我想让它看起来像的东西。根据您的需求,请查看以获得两种解决方案:
有一种方法可以做到这一点:使用JLabel作为带有图像的容器(这有点不寻常,但实际上效果相当好):
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.net.MalformedURLException;
导入java.net.URL;
导入javax.swing.Box;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.SwingUtilities;
公共类TestBackgroundImage{
私有静态最终字符串BACKHGROUND\u IMAGE\u URL=”http://www.okyn.org/wp-content/uploads/2013/04/League_of_Legends.jpeg";
受保护的void initUI()引发错误的FormedUrlexception{
JFrame=newjframe(TestBackgroundImage.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
最终图像图标backgroundImage=新图像图标(新URL(BACKHGROUND_IMAGE_URL));
JLabel主面板=新JLabel(背景图像){
@凌驾
公共维度getPreferredSize(){
维度大小=super.getPreferredSize();
维度lmPrefSize=getLayout()。preferredLayoutSize(此);
size.width=Math.max(size.width,lmPrefSize.width);
size.height=Math.max(size.height,lmPrefSize.height);
返回大小;
}
};
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.插图=新插图(10,10,10,10);
gbc.weightx=1.0;
gbc.anchor=GridBagConstraints.WEST;
gbc.gridwidth=GridBagConstraints.rements;
对于(int i=0;i<5;i++){
添加(新的JButton(“按钮”+(i+1)),gbc);
}
//让我们放置一个填充底部组件,将其余部分推到顶部
gbc.weighty=1.0;
添加(Box.createGlue(),gbc);
添加框架(主面板);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
新建TestBackgroundImage().initUI();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
}
}
结果是:
无法处理组件的分层(因此,如果您有重叠的GridBagLayout
/gridx
对,它不会将其作为分层处理,我认为输出几乎是不确定的gridy
- 尽量避免使用
,因为这会使代码难以维护。在gridx/gridy
上使用相对值更容易维护gridwidth/gridheight
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.net.MalformedURLException;
导入java.net.URL;
导入javax.swing.Box;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.SwingUtilities;
公共类TestBackgroundImage{
私有静态最终字符串BACKHGROUND\u IMAGE\u URL=”http://www.okyn.org/wp-content/uploads/2013/04/League_of_Legends.jpeg";
受保护的void initUI()引发错误的FormedUrlexception{
JFrame=newjframe(TestBackgroundImage.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
最终图像图标backgroundImage=新图像图标(新URL(BACKHGROUND_IMAGE_URL));
JLabel主面板=新JLabel(背景图像){
@凌驾
公共维度getPreferredSize(){
维度大小=super.getPreferredSize();
维度lmPrefSize=getLayout()。preferredLayoutSize(此);
size.width=Math.max(size.width,lmPrefSize.width);
size.height=Math.max(size.height,lmPrefSize.height);
返回大小;
}
};
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.插图=新插图(10,10,10,10);
gbc.weightx=1.0;
gbc.anchor=GridBagConstraints.WEST;
gbc.gridwidth=GridBagConstraints.rements;
对于(int i=0;i<5;i++){
添加(新的JButton(“按钮”+(i+1)),gbc);
}
//让我们放置一个填充底部组件,将其余部分推到顶部
gbc.weighty=1.0;
添加(Box.createGlue(),gbc);
添加框架(主面板);
框架
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TestBackgroundImage {
private static final String BACKHGROUND_IMAGE_URL = "http://www.okyn.org/wp-content/uploads/2013/04/League_of_Legends.jpeg";
protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame(TestBackgroundImage.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL));
JLabel mainPanel = new JLabel(backgroundImage) {
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
Dimension lmPrefSize = getLayout().preferredLayoutSize(this);
size.width = Math.max(size.width, lmPrefSize.width);
size.height = Math.max(size.height, lmPrefSize.height);
return size;
}
};
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (int i = 0; i < 5; i++) {
mainPanel.add(new JButton("Button " + (i + 1)), gbc);
}
// Let's put a filler bottom component that will push the rest to the top
gbc.weighty = 1.0;
mainPanel.add(Box.createGlue(), gbc);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TestBackgroundImage().initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}