正如标题所说,我正在尝试做。
我有一种有效的方法可以将几个相同的swing对象发布到一个帧中,方法是将它们存储在一个数组中并使用for循环添加它们,如下所示:
JLabel[] contrllabels= new JLabel[8];
contrllabels[0] = new JLabel("SCF Type: ");
contrllabels[1] = new JLabel("Units: ");
contrllabels[2] = new JLabel("Spherical Harmonics: ");
contrllabels[3] = new JLabel("Molecular Charge: ");
contrllabels[4] = new JLabel("PP: ");
contrllabels[5] = new JLabel("DFT Type: ");
contrllabels[6] = new JLabel("Max Iterations: ");
contrllabels[7] = new JLabel("Mult: ");
for(int i = 0;i<contrllabels.length;i++){
c.gridy = i;
frame.add(contrllabels[i],c);
}
但如果有几种不同类型的摆动物怎么办?我有几个组合框和文本字段,我希望以类似的方式添加到框架中。我使用gridbaglayout所以如果我不使用for循环,我最终会得到很多不必要的代码,因为每次我想要添加不同的对象时都会给出约束新值。
是否存在一个引用数组,指向这些不同的对象,然后我可以迭代以添加到框架中?像
这样的东西JTextField tf = new JTextField(5);
JComboBox cb = new JComboBox("example");
Swing[] array = {tf,cb}
for(int i = 0;i<array.length;i++){
c.gridy = i;
frame.add(array[i],c);
}
我知道这样的数组不存在,但有没有办法实现这样的事情?它会大大减少代码中的行数,减少混乱。
谢谢
答案 0 :(得分:7)
您可以使用常见超类型的数组或集合,例如JComponent的数组或ArrayList。我很好奇你是否正在使用GridBagConstraints的并行数组来处理正在添加的每个组件 - 呃。我经常使用组件数组,但通常它们就像JLabel / JTextField对或JRadioButtons集群之类的组件。
顺便说一下,为了我的钱,我尽量避免使用GridBagLayout,而是嵌套使用更加编码器友好布局的容器。
例如,这个小GUI是使用FlowLayout,BoxLayout,BorderLayout和GridLayout的组合制作的:
保存整个GUI的大型JPanel使用BorderLayout,中间的JTextArea放置BorderLayout.CENTER,顶部的Provider JLabel和JTextField位于FlowLayout JPanel中,整体放置BorderLayout.NORTH,底部按钮是在使用GridLayout(1,0,5,0)的JPanel中,它使用另一个使用FlowLayout的JPanel保存在GUI BorderLayout.SOUTH中,而右边的东西使用JPanel在BoxLayout中。
例如:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
@SuppressWarnings("serial")
public class GetLetterTextGui extends JPanel {
private static final int TA_ROWS = 20;
private static final int TA_COLS = 35;
private static final int PROVIDER_FIELD_COLS = 10;
private static final String GUI_TITLE = "Get Letter Text";
private JList letterList;
private JTextArea textarea = new JTextArea(TA_ROWS, TA_COLS);
private JTextField providerField = new JTextField(PROVIDER_FIELD_COLS);
private JCheckBox addValedictionChkBox = new JCheckBox("Add Valediction", true);
public GetLetterTextGui() {
letterList = new JList(new String[]{"Fe", "Fi", "Fo", "Fum"});
providerField.setText("John Smith, MD");
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
JPanel northPanel = new JPanel();
northPanel.add(new JLabel("Provider:"));
northPanel.add(providerField);
JPanel southPanel = new JPanel();
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
btnPanel.add(new JButton("Copy to Clipboard"));
btnPanel.add(new JButton("Clear"));
btnPanel.add(new JButton(new ExitAction()));
southPanel.add(btnPanel);
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
eastPanel.add(new JScrollPane(letterList));
eastPanel.add(new JPanel() {{add(addValedictionChkBox);}});
int eb = 0;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
setLayout(new BorderLayout(eb, eb));
add(northPanel, BorderLayout.PAGE_START);
add(eastPanel, BorderLayout.LINE_END);
add(new JScrollPane(textarea), BorderLayout.CENTER);
add(southPanel, BorderLayout.PAGE_END);
}
private class ExitAction extends AbstractAction {
private final Object MNEMONIC = new Integer(KeyEvent.VK_X);
public ExitAction() {
super("Exit");
putValue(MNEMONIC_KEY, MNEMONIC);
}
@Override
public void actionPerformed(ActionEvent evt) {
Window win = SwingUtilities.getWindowAncestor(GetLetterTextGui.this);
win.dispose();
}
}
private static void createAndShowGui() {
GetLetterTextGui mainPanel = new GetLetterTextGui();
JFrame frame = new JFrame(GUI_TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:4)
所有GUI组件都是JComponent对象。 ArrayList<JComponent>
可以包含对所有这些内容的引用。试一试:
JFrame frame = new JFrame("test");
ArrayList<JComponent> cps = new ArrayList<JComponent>();
cps.add(new JLabel("Hello"));
cps.add(new JPanel());
cps.add(new JButton("OK"));
for (JComponent widget : cps) {
frame.add(widget);
}