我寻找一个具有固定高度JButton的LayoutManager,它的宽度会扩展到适合其Container的大小。在JButton上面,应该有一个JLabel本身在“选择文件:”的行上。这旨在作为JFileChooser的附件,让用户选择最近的文件。我无法让它看起来很正确,我尝试过使用多个JPanel和LayoutManagers,比如BoxLayout。当使用BoxLayout时,JButton只会扩展到包含文本的范围;但是我希望所有的JButton都具有相同的宽度,所以它们看起来并不好笑。
注意:我还使用了其他LayoutManagers,例如Border和GridLayout,但那些大多数都忽略了我的尺寸设置并且看起来不够复杂。我必须手动执行此操作,Netbeans等不是一种选择。
工作示例,但视觉上不正确
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);
}
}
答案 0 :(得分:6)
GridLayout嵌套在BorderLayout中的实际情况(实际上是在JScrollPane中)...
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);
}
}
示例2,带有一个简单的JList,将其选择放在文件选择器中:
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);
}
}