我正在尝试制作一个允许您创建和玩游戏的应用程序,并且危险!板。在创建GUI时,我似乎遇到了困难。 JPanel(scrollPanel1)似乎没有显示在我的JScrollPane(scrollPanel1)中。我非常确定JScrollPane在那里,因为当我将滚动条策略设置为始终显示滚动条时,滚动条是可见的。但是我不认为JPanel会出现在其中。我将不胜感激。
这是代码:
import java.awt.*;
import javax.swing.*;
public class CreateNewBoardGUI {
private JFrame mainFrame;
private JPanel mainPanel;
private JPanel boardInfo;
private JLabel boardNameLabel;
private JTextField boardNameField;
private JTabbedPane catTab;
private JPanel tabPanel1;
private JPanel catNamePanel1;
private JLabel catNameLabel1;
private JTextField catNameField1;
private JScrollPane scrollPane1;
private JPanel scrollPanel1;
private JPanel qPanel1;
private JLabel qLabel1;
private JTextField qField1;
public void CreateNewBoardGUI(){
mainFrame = new JFrame("Create new Jeopardy! board");
mainPanel = new JPanel();
boardInfo = new JPanel();
boardNameLabel = new JLabel("What would you like this board to be called?");
boardNameField = new JTextField();
catTab = new JTabbedPane();
tabPanel1 = new JPanel();
catNamePanel1 = new JPanel();
catNameLabel1 = new JLabel("What would you like the category to be called?");
catNameField1 = new JTextField();
scrollPane1 = new JScrollPane();
scrollPanel1 = new JPanel();
qPanel1 = new JPanel();
qLabel1 = new JLabel("200 Point Question");
qField1 = new JTextField();
mainPanel.setLayout(new BorderLayout());
boardInfo.setLayout(new BorderLayout());
tabPanel1.setLayout(new BorderLayout());
catNamePanel1.setLayout(new BorderLayout());
scrollPanel1.setLayout(new BorderLayout());
mainFrame.setSize(700, 900);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
qPanel1.add(qLabel1);
qPanel1.add(qField1);
scrollPanel1.add(qPanel1, BorderLayout.NORTH);
scrollPane1.add(scrollPanel1);
tabPanel1.add(scrollPane1, BorderLayout.CENTER);
catNamePanel1.add(catNameField1, BorderLayout.CENTER);
catNamePanel1.add(catNameLabel1, BorderLayout.NORTH);
tabPanel1.add(catNamePanel1, BorderLayout.NORTH);
mainPanel.add(catTab, BorderLayout.CENTER);
boardInfo.add(boardNameField, BorderLayout.CENTER);
boardInfo.add(boardNameLabel, BorderLayout.NORTH);
mainPanel.add(boardInfo, BorderLayout.NORTH);
mainFrame.add(mainPanel);
catTab.addTab("Category 1", tabPanel1);
}
}
提前致谢。
最佳答案
删除scrollPane1 = new JScrollPane();
并添加它(在创建scrollPanel1实例之后):scrollPane1 = new JScrollPane(scrollPanel1);
这将强制scrollPane1将scrollPanel1作为其“视口”。
要查看运行情况,您应该设置背景色:scrollPanel1.setBackground(Color.red);
向面板添加不同的颜色以研究其行为。
一旦将滚动条拉出选项卡面板,它应该立即添加滚动条。
注意:请看一下“空布局”。它应该更容易处理。
让我知道这是否有帮助,或者您仍然遇到任何麻烦。