如何使 JButton 的宽度变小?

huangapple go评论78阅读模式
英文:

How to make a JButton width smaller?

问题

我似乎无法弄清楚如何调整当前处于垂直状态的 JButton 的宽度。当前它看起来是这个样子,我正试图调整按钮的宽度以适应徽标。

JButton jbt2 = createButton("1", "discord.png", "打开官方Skyfire主页。");
JButton jbt3 = createButton("1", "discord.png", "打开官方Skyfire主页。");
JButton jbt4 = createButton("1", "discord.png", "打开官方Skyfire主页。");
JButton jbt1 = createButton("1", "discord.png", "打开官方Skyfire主页。");
Box menuPanel = Box.createVerticalBox();
menuPanel.add(jbt1);
menuPanel.add(jbt2);
menuPanel.add(jbt3);
menuPanel.add(jbt4);

add(menuPanel);  
frame.getContentPane().add(menuPanel, BorderLayout.EAST);
}

private JButton createButton(String name, String image, String tooltip) {
    JButton button = new JButton(name);
    if (image != null)
        button.setIcon(new ImageIcon(ResourceLoader.loadImage(image)));
    button.addActionListener(this);
    if (tooltip != null)
        button.setToolTipText(tooltip);
    

    button.setBorder(BorderFactory.createEmptyBorder());
    button.setContentAreaFilled(false);
    button.setCursor(new Cursor(Cursor.HAND_CURSOR));
    button.setForeground(Color.decode("#4d2600"));
    return button;
}
英文:

I can't seem to figure out how to resize the width of a JButton currently in vertical state. Right now it looks like this, and I'm trying to adjust the width of the button so it fits the logo.

	    JButton jbt2 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
	    JButton jbt3 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
	    JButton jbt4 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
	JButton jbt1 = createButton("1", "discord.png", "Open the official Skyfire homepage.");
  Box menuPanel = Box.createVerticalBox();
	    menuPanel.add(jbt1);
        menuPanel.add(jbt2);
        menuPanel.add(jbt3);
        menuPanel.add(jbt4);

        add(menuPanel);  
	    frame.getContentPane().add(menuPanel, BorderLayout.EAST);
	}

	private JButton createButton(String name, String image, String tooltip) {
		JButton button = new JButton(name);
		if (image != null)
			button.setIcon(new ImageIcon(ResourceLoader.loadImage(image)));
		button.addActionListener(this);
		if (tooltip != null)
			button.setToolTipText(tooltip);
	

		button.setBorder(BorderFactory.createEmptyBorder());
		button.setContentAreaFilled(false);
		button.setCursor(new Cursor(Cursor.HAND_CURSOR));
		button.setForeground(Color.decode("#4d2600"));
		return button;
	}

答案1

得分: 1

所以我认为我找到了,我只需要添加

button.setPreferredSize(new Dimension(29, 40));
英文:

so i think i found it, i just had to add

button.setPreferredSize(new Dimension(29, 40));

huangapple
  • 本文由 发表于 2020年9月29日 04:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64109384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定