Java 具有固定高度和可变宽度按钮的布局
我正在寻找一个LayoutManager,它具有固定高度的JButton,这些按钮可以扩展宽度以适应容器的大小。在JButtons上方的行上应该有一个JLabel,上面写着“choosefile:”。这是作为JFileChooser的附件使用的,它允许用户选择最近的文件。我没能让它看起来很正确,我试过使用多个JPanel和布局管理器,比如BoxLayout。当使用BoxLayout时,JButtons只能扩展到包含文本的范围;但是我希望所有的JButtons都是相同的宽度,这样它们看起来就不好笑了 注意:我也使用过其他布局管理器,如Border和GridLayout,但它们大多忽略了我的大小设置,看起来不够复杂。我必须手工操作,不能选择Netbeans等 工作示例,但视觉上不正确Java 具有固定高度和可变宽度按钮的布局,java,swing,layout-manager,Java,Swing,Layout Manager,我正在寻找一个LayoutManager,它具有固定高度的JButton,这些按钮可以扩展宽度以适应容器的大小。在JButtons上方的行上应该有一个JLabel,上面写着“choosefile:”。这是作为JFileChooser的附件使用的,它允许用户选择最近的文件。我没能让它看起来很正确,我试过使用多个JPanel和布局管理器,比如BoxLayout。当使用BoxLayout时,JButtons只能扩展到包含文本的范围;但是我希望所有的JButtons都是相同的宽度,这样它们看起来就不好笑
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Chooser extends JPanel {
public Chooser(){
this.setLayout(new GridLayout(0,1));
JPanel labelPanel = new JPanel();
JLabel label = new JLabel("Choose a file:");
labelPanel.add(label);
labelPanel.setBackground(Color.red);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("long pathname to a file goes here"));
buttonPanel.setBackground(Color.blue);
this.add(labelPanel);
this.add(buttonPanel);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Chooser c = new Chooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAccessory(c);
fileChooser.showOpenDialog(null);
}
}
如果GridLayout嵌套在BorderLayout中(实际上是在JScrollPane中),该怎么办
为什么不用一个最小但足够的例子来展示你的最佳尝试,一个?@Hovercraft,那么我不妨复制粘贴任何Swing JPanel代码的随机例子,因为我已经尝试了很多不同的东西。工作但视觉上不正确的BoxLayout示例有什么用处?为了证明我试过了?不,我相信你已经试过了,但是如果你发布了SSCCE,我们将了解你的代码和问题的当前结构,更好地理解你的问题,如果我们更好地理解它,我们将更好地帮助你。如果我们修改您的代码示例,您将更好地理解我们的代码。坦率地说,这将节省我们的时间,使我们更容易帮助您,因为您将为我们的代码提供初始框架。但是,这取决于你自己,因为这是你要解决的问题。不,这会让读者知道你想要什么,也会让你有机会获得关于你的方法的反馈。请参阅“一般”和“特殊”。@KyleM
以及复制粘贴任何Swing的随机示例
,那么我认为您将问题张贴在了错误的论坛上。非常感谢。这是一个很好的起点。我个人认为JList在JFileChooser中看起来很有趣,但我可能会在每个JListItem中添加一个图标(无论它们被称为什么)并查看它的外观。@KyleM:注意编辑到第二个程序,允许JList将其选择放在JFileChooser的文本字段中。谢谢,非常感谢。我确实已经在工作了。事实上,我所有的功能都能正常工作,我只是在布局方面做得很差劲。我仍在试图弄清楚JFileChooser如何在内部列出它的文件(这样我就可以模仿了),但到目前为止,我还停留在这一点上,涉猎了源代码。@满是鳗鱼的气垫船我和你有大问题(JList和Mouse)示例,稍后会被问到,因为我的问题+1nevermind正在考虑另一个问题。再次感谢你的帮助。标记为答案的。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Chooser extends JPanel {
private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
"Hello Goodbye", "Adios", "This is a long String for WTF" };
public Chooser() {
this.setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
JLabel label = new JLabel("Choose a file:");
labelPanel.add(label);
labelPanel.setBackground(Color.red);
JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < BUTTON_TEXTS.length; i++) {
buttonPanel.add(new JButton(BUTTON_TEXTS[i]));
}
buttonPanel.add(Box.createVerticalGlue()); // to pad the bottom if needed
buttonPanel.setBackground(Color.blue);
this.add(labelPanel, BorderLayout.PAGE_START);
this.add(new JScrollPane(buttonPanel), BorderLayout.CENTER);
}
public static void main(String[] args) {
Chooser c = new Chooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAccessory(c);
fileChooser.showOpenDialog(null);
}
}
import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Chooser extends JPanel {
private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
"Hello Goodbye", "Adios", "This is a long String for WTF", "Hello",
"Goodbye", "Hello Goodbye", "Adios", "This is a long String for WTF" };
public Chooser(final JFileChooser fileChooser) {
setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
JLabel label = new JLabel("Choose a file:");
labelPanel.add(label);
labelPanel.setBackground(Color.red);
final JList list = new JList(BUTTON_TEXTS);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String selectedString = list.getSelectedValue().toString();
fileChooser.setSelectedFile(new File(selectedString));
}
});
add(labelPanel, BorderLayout.PAGE_START);
add(new JScrollPane(list), BorderLayout.CENTER);
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
Chooser c = new Chooser(fileChooser);
fileChooser.setAccessory(c);
fileChooser.showOpenDialog(null);
}
}