jjzjj

java - GridBagLayout 没有正确对齐图像

coder 2024-04-01 原文

最初我有一张甲板图像和图像下方的文本“Deck”,看起来不错

public class GuiTut extends JPanel { 
   private GridBagConstraints c = new GridBagConstraints();
   private JLabel deckLabel = new JLabel();

   public GuiTut() {
        setLayout(new GridBagLayout());  

        try {
            deck = ImageIO.read(new File("resources/images/deck.jpg"));  
        } catch (Exception e) {}   

        c.gridx = 0;
        c.gridy = 0;
        JLabel deckPic = new JLabel(new ImageIcon(deck));    
        add(deckPic, c);

        deckLabel.setText("Deck");
        c.gridx = 0;
        c.gridy = 1;
        add(deckLabel, c);
}

但是在我添加了 gridLayout 面板之后,我的整个 GUI 设计就乱七八糟了。 如您所见,我的甲板图片没有与我的 gridLayout 的第一行正确对齐 和我的文本“deck”被一些宽的空间隔开。

public class GuiTut extends JPanel { 
   private GridBagConstraints c = new GridBagConstraints();
   private JLabel deckLabel = new JLabel();
   private JPanel gridLayoutPanel = new JPanel(new GridLayout(2, 0));
   private JLabel[] label = new JLabel[14];

   public GuiTut() {
        setLayout(new GridBagLayout());  

        try {
            card = ImageIO.read(new File("resources/images/card.jpg"));  
        } catch (Exception e) {} 

        for(int i = 0; i < 14; i++) {   
            label[i] = new JLabel(new ImageIcon(card);
            gridLayoutPanel.add(label[i]);
        }

        try {
            deck = ImageIO.read(new File("resources/images/deck.jpg"));  
        } catch (Exception e) {}   

        c.gridx = 0;
        c.gridy = 0;
        JLabel deckPic = new JLabel(new ImageIcon(deck));    
        add(deckPic, c);

        deckLabel.setText("Deck");
        c.gridx = 0;
        c.gridy = 1;
        add(deckLabel, c);

        c.gridx = 1;
        c.gridy = 0;
        add(gridLayoutPanel, c);
}

我想要的是牌组图像与 gridLayout 的第一行对齐,文本 “deck” 就在牌组图像的正下方,但我不能好像没听懂。

最佳答案

热链接图片

  1. 对于甲板标签,您可以只设置标签的文本并设置垂直/水平文本位置

    JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL)));
    label.setText("DECK");
    label.setHorizontalTextPosition(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.BOTTOM);
    
  2. 对于甲板定位问题,您可以使用不同的布局管理器,例如外部面板的 BorderLayout,以及甲板面板的默认流布局。这些卡片仍将是 GridLayout。在需要时嵌套面板通常很有帮助

例子

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GuiTest {

    private static final String DECK_URL = "http://i.stack.imgur.com/xNffR.png";
    private static final String CARD_URL = "http://i.stack.imgur.com/uVS1D.png";

    private static JPanel createDeckPanel() {
        JPanel panel = new JPanel();
        try {
            JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL)));
            label.setText("DECK");
            label.setHorizontalTextPosition(JLabel.CENTER);
            label.setVerticalTextPosition(JLabel.BOTTOM);
            panel.add(label);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return panel;
    }

    private static JPanel createCenterPanel(int rows, int cols) {
        JPanel panel = new JPanel(new GridLayout(rows, cols));
        for (int i = 0; i < rows*cols; i++) {
            try {
                panel.add(new JLabel(new ImageIcon(new URL(CARD_URL))));
            } catch (Exception ex) {
                ex.printStackTrace();
            }   
        }
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(createDeckPanel(), BorderLayout.LINE_START);
                panel.add(createCenterPanel(2, 6));
                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}

关于java - GridBagLayout 没有正确对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968105/

有关java - GridBagLayout 没有正确对齐图像的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  6. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  7. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  10. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

随机推荐