首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridBagLayout Java

GridBagLayout Java
EN

Stack Overflow用户
提问于 2013-12-19 01:04:07
回答 2查看 268关注 0票数 0

嗨,我再次放入我的原始代码,只是为了让你看到我正在谈论的GridBagConstrainsts,我试图将每个图像贴在面板的南边,彼此紧贴在一起

代码语言:javascript
复制
package prototype;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.WindowConstants;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import javax.imageio.ImageIO;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
//Declare the class which extends JFrame and
//implements ActionListener to enable bottons to respond whenever clicked or selected
public class Master extends JFrame implements ActionListener {

    //create the bottons visible by the user
    JButton check = new JButton("");
    JButton playList = new JButton("");
    JButton update = new JButton("");
    JButton quit = new JButton("");
    JCheckBox tick = new JCheckBox("Tick");

    JPanel top = new JPanel();

    public static void main(String[] args) {

        //declare object of the class
        Master jf = new Master();
    }

    public Master() {
        setLayout(new BorderLayout());
        setSize(1050, 400);
        setTitle("Master");

        // close application only by clicking the quit button
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //show the frame in the middle of the screen when run
        setLocationRelativeTo(null);

        top.add(new JLabel("Select an option by clicking one of the buttons below"));
        add("North", top); // add the text above to the upper part of the frame (North)
        JPanel bottom = new JPanel();
        bottom.setLayout(new GridBagLayout());
        bottom.add(check);
        check.addActionListener(this);
        bottom.add(playList);
        playList.addActionListener(this);
        bottom.add(update);
        update.addActionListener(this);
        bottom.add(quit);
        quit.addActionListener(this);
        add("South", bottom);

        //make the frame non resizable but visible
        setResizable(true);
        setVisible(true);

        try{
       Image  img = ImageIO.read(getClass().getResource("gui/Exit.png"));
       Image resize = img.getScaledInstance(290, 180, 18);
       quit.setIcon(new ImageIcon(resize));
       img = (bottom, new JLabel("NAME"), 0,0,1,1, GridBagConstraints.SOUTH);


        }catch(Exception e){
        }
        try{
       Image  img = ImageIO.read(getClass().getResource("gui/Untitled.png"));
       Image resize = img.getScaledInstance(290, 180, 18);
       check.setIcon(new ImageIcon(resize));

        }catch(Exception e){
        }
        try{
       Image  img = ImageIO.read(getClass().getResource("gui/CPL.png"));
       Image resize = img.getScaledInstance(290, 180, 18);
       playList.setIcon(new ImageIcon(resize));

        }catch(Exception e){
        }
        try{
       Image  img = ImageIO.read(getClass().getResource("gui/UpdateLib.png"));
       Image resize = img.getScaledInstance(290, 180, 18);
       update.setIcon(new ImageIcon(resize));

        }catch(Exception e){
        }

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == check) {
            new CheckLibrary();
        } else if (e.getSource() == update) {
            new UpdateLibrary();
        } else if (e.getSource() == quit) {
            System.exit(0);
        } else if (e.getSource() == playList)   {
            new CreatePlaylist();

    }
}
}
EN

回答 2

Stack Overflow用户

发布于 2013-12-19 01:09:43

为此,您需要使用GridBagConstraintsanchorweighty属性。

在下一个示例中,我将JTextField设置为南边:

代码语言:javascript
复制
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Example extends JFrame {

    public Example (){
        JTextField f = new JTextField(20);

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.SOUTHEAST;
        c.weighty = 1;
        add(f,c);
    }

    public static void main(String...strings ){
        Example e = new Example();
        e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        e.pack();
        e.setLocationRelativeTo(null);
        e.setVisible(true);
    }

}

如果您需要按组件填充所有水平空间,请添加下一步:

代码语言:javascript
复制
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;

在我的示例中,对于Image,您可以使用JLabel而不是JTextField

代码语言:javascript
复制
JLabel l = new JLabel(new ImageIcon(getClass().getResource(PATH_TO_IMAGE)));
票数 0
EN

Stack Overflow用户

发布于 2013-12-19 01:11:21

您不能将图像直接添加到JPanel。相反,您可以将图像设置为JPanel或JLabel的图像图标,并将其添加到您正在尝试制作的任何内容中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20664147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档