我正在开发Java应用程序以进行测试(用于研究目的)。花了一半的时间,直到连接到Firebird DB。 现在我在构建GUI时遇到了问题。这是我的代码:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;
public class ExamSettingsWindow extends JFrame {
JDialog examSettingsWindow = new JDialog(MainWindow.mainWindow, "Редагування екзамену", true);
JPanel mainPanel = new JPanel();
ArrayList<Question> questionsList = new ArrayList<Question>(Question.getQuestions());
ArrayList<JPanel> qaPanels = new ArrayList<JPanel>();
ExamSettingsWindow() {
for(Question q : questionsList)
{ // Some weird shit happenning in this loop, can't explain this, but i'm sure it will work someday...
JLabel qID = new JLabel(String.valueOf(q.getID()));
qID.setBorder(new EmptyBorder(2,2,2,2));
JLabel qText = new JLabel(q.getText());
qText.setBorder(new EmptyBorder(2,2,2,2));
JPanel qPanel = new JPanel();
qPanel.setLayout(new BoxLayout(qPanel, BoxLayout.X_AXIS));
qPanel.setBorder(new EmptyBorder(5,5,5,5));
qPanel.setMinimumSize(new Dimension(500,30));
qPanel.add(qID);
qPanel.add(qText);
JPanel aPanels = new JPanel();
aPanels.setLayout(new BoxLayout(aPanels, BoxLayout.Y_AXIS));
aPanels.setBorder(new EmptyBorder(5,5,5,5));
aPanels.setMinimumSize(new Dimension(500,30));
for (Question.Answer a : q.answersList) {
JLabel aID = new JLabel(a.getID());
aID.setBorder(new EmptyBorder(2,2,2,2));
JLabel aText = new JLabel(a.getText());
aText.setBorder(new EmptyBorder(2,2,2,2));
JPanel aPanel = new JPanel();
aPanel.setMinimumSize(new Dimension(500,30));
aPanel.setLayout(new BoxLayout(aPanel, BoxLayout.X_AXIS));
aPanel.add(aID);
aPanel.add(aText);
aPanels.add(aPanel);
}
JPanel qaPanel = new JPanel();
qaPanel.setMinimumSize(new Dimension(500,200));
qaPanel.setLayout(new BoxLayout(qaPanel, BoxLayout.Y_AXIS));
qaPanel.setBorder(new TitledBorder(new LineBorder(Color.black, 2),
"Питання "+String.valueOf(q.getID())));
qaPanels.add(qaPanel);
}
examSettingsWindow.setMinimumSize(new Dimension(500, 500));
examSettingsWindow.setMaximumSize(new Dimension(500, 500));
examSettingsWindow.setResizable(false);
examSettingsWindow.setDefaultCloseOperation(HIDE_ON_CLOSE);
examSettingsWindow.setLocationRelativeTo(null);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setMinimumSize(new Dimension(500,500));
for (JPanel p : qaPanels) {
mainPanel.add(p);
}
JScrollPane sc = new JScrollPane(mainPanel);
sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
examSettingsWindow.add(mainPanel);
}
public void initExamSettingsFrame(boolean vision) {
examSettingsWindow.setVisible(vision);
}
}
这是运行程序后显示的图片: >>click<<
我认为问题可能在ArrayList中,其中包含具有相同名称的JPanel,但我不确定,当我在寻找原因和解决方案时,我的头脑正在爆炸。 请帮忙......给我正确的方向......
P.S。我的代码并不完美,不要因此而责怪我。我只是想学习Java。