第一次嘗試得到你想要添加的combobox
位置的Container
。 然後只需將該組合框添加到該Container。
例如,在這裏首先我找到Save
和Cancel
按鈕的Container
面板。
JPanel panel1 = (JPanel)this.getComponent(3);
JPanel panel2 = (JPanel) panel1.getComponent(3);
然後我在加入是Panel2組合框
panel2.add(comboBox);
它看起來像:
全碼:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class TestFilechooser extends JFrame {
private JPanel contentPane;
MyFileChooser jc;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFilechooser frame = new TestFilechooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFilechooser() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
jc = new MyFileChooser();
JButton btnOpen = new JButton("open");
contentPane.add(btnOpen, BorderLayout.NORTH);
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = jc.showOpenDialog(TestFilechooser.this);
}
});
pack();
}
}
class MyFileChooser extends JFileChooser{
public MyFileChooser() {
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
JPanel panel1 = (JPanel)this.getComponent(3);
JPanel panel2 = (JPanel) panel1.getComponent(3);
Component c1=panel2.getComponent(0);//optional used to add the buttons after combobox
Component c2=panel2.getComponent(1);//optional used to add the buttons after combobox
panel2.removeAll();
panel2.add(comboBox);
panel2.add(c1);//optional used to add the buttons after combobox
panel2.add(c2);//optional used to add the buttons after combobox
}
}
簡單的過程:
此解決方案不會在底部添加組合框,但我認爲它可以幫助你。
改變你的類是這樣的:
class MyFileChooser extends JFileChooser{
public MyFileChooser() {
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
JPanel panel = new JPanel();
panel.add(comboBox);
setAccessory(panel);
//add(comboBox, BorderLayout.SOUTH);
}
}
結果:
全部工作守則的例子:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class TestFilechooser extends JFrame {
private JPanel contentPane;
MyFileChooser jc;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFilechooser frame = new TestFilechooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFilechooser() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
jc = new MyFileChooser();
JButton btnOpen = new JButton("open");
contentPane.add(btnOpen, BorderLayout.NORTH);
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = jc.showOpenDialog(TestFilechooser.this);
}
});
pack();
}
}
class MyFileChooser extends JFileChooser{
public MyFileChooser() {
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
JPanel panel = new JPanel();
panel.add(comboBox);
setAccessory(panel);
//add(comboBox, BorderLayout.SOUTH);
}
}
不在你的構造函數中調用'super();'可能沒有幫助。 –
我會避免試圖這樣做,除非你真的知道你在做什麼。我只會使用更多API特定的東西,比如['JFileChooser.setAccessory'](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory)。你可能得不到你想要的確切外觀,但至少你不會破壞任何東西。這裏還有一個[示例](http://stackoverflow.com/a/24291619/2587435) –
*「我想將JComboBox添加到JFileChooser中。」*爲什麼?這個組合是什麼?爲標準文件選擇器沒有的最終用戶做什麼? –