英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论