gpt4 book ai didi

java - JFrame 布局与 JPanels

转载 作者:行者123 更新时间:2023-11-30 08:24:33 24 4
gpt4 key购买 nike

我在尝试布局 JFrame 时遇到问题。我试图在顶部添加工具栏,然后在其下方添加信息,然后在其下方添加颜色到右侧,然后在中心添加副本,然后在左侧添加打印按钮,然后在底部添加打印机列表。如果有人能在正确的方向上帮助我,那就太好了。

// Declare GUI Components here
// One JToolBar & JButton
private JPanel mainPanel;
private JPanel detailPanel;
private JPanel toolBarPanel;
private JToolBar jToolbar;
private JButton jbtAdmin, jbtHelp;

// A JPanel called infoPanel & JLabel
private JPanel infoPanel;
private JLabel jlblOne;

// A JPanel called colourPanel
private JPanel colourPanel;
private JRadioButton bwRadioButton, colourRadioButton;
private ButtonGroup btg;



// A JPanel called noCopiesPanel
private JPanel noCopiesPanel;
private JLabel jlbCopies;
private JTextField jtfCopies;


// A JPanel called printerPanel
private JPanel printerPanel;
private JComboBox printerBox;

private JButton jbtPrint;

// Constructor - SetLayout & Add Components here...
// Constructor takes in the selected student and assigns it to currentStudent
public StudentFrame(Student studentIn){
// Set up currentStudent
currentStudent=studentIn;

// Set up Toolbar & add jbtAdmin
toolBarPanel = new JPanel();
toolBarPanel.add(jToolbar = new JToolBar());
jToolbar.add(jbtAdmin = new JButton("Admin"));
jToolbar.add(jbtHelp = new JButton("Help"));

// Set up called infoPanel
infoPanel = new JPanel();
infoPanel.add(jlblOne = new JLabel(currentStudent.toString(), JLabel.CENTER));

// Set up colourPanel with radioButtons
colourPanel = new JPanel(new GridLayout(2,1));
colourPanel.add(bwRadioButton = new JRadioButton("Black & White", true));
colourPanel.add(colourRadioButton = new JRadioButton("Colour"));
btg = new ButtonGroup();
btg.add(bwRadioButton);
btg.add(colourRadioButton);
// Put a TitledBorder around it
colourPanel.setBorder(new TitledBorder("Colour"));

// Set up noCopiesPanel
noCopiesPanel = new JPanel(new GridLayout(1,2));
noCopiesPanel.add(jlbCopies = new JLabel("Copies"));
noCopiesPanel.add(jtfCopies = new JTextField(3));
noCopiesPanel.setBorder(new TitledBorder("Print"));

// Set up jbtPrint JButton
jbtPrint = new JButton("Print",new ImageIcon("Images/printerIcon.png"));
jbtPrint.setHorizontalTextPosition(JButton.CENTER);
jbtPrint.setVerticalTextPosition(JButton.TOP);
jbtPrint.setFont(new Font("Helvetica", Font.BOLD, 30));
jbtPrint.setBackground(Color.LIGHT_GRAY);
jbtPrint.setMnemonic('P');

// Set up printerPanel
printerPanel = new JPanel();
String[] printerList = {"Printer 24001", "Printer 24002", "Printer 24003", "Printer 24004"};
printerPanel.add(printerBox = new JComboBox(printerList));
printerPanel.setBorder(new TitledBorder("Printers"));

detailPanel = new JPanel(new GridLayout(2,1));
detailPanel.add(infoPanel, BorderLayout.NORTH);
detailPanel.add(colourPanel, BorderLayout.WEST);
detailPanel.add(noCopiesPanel, BorderLayout.CENTER);
detailPanel.add(jbtPrint, BorderLayout.EAST);
detailPanel.add(printerPanel, BorderLayout.SOUTH);

mainPanel = new JPanel();

mainPanel.add(toolBarPanel, BorderLayout.NORTH);
mainPanel.add(detailPanel, BorderLayout.SOUTH);
this.add(mainPanel);
//this.add(detailPanel);

最佳答案

detailPanel = new JPanel(new GridLayout(2,1));
detailPanel.add(infoPanel, BorderLayout.NORTH);

您的布局为 GridLayout 但您正试图设置 BorderLayout 位置。如果要设置位置,请将 detailPanel 的布局设置为 BorderLayout


mainPanel = new JPanel();
mainPanel.add(toolBarPanel, BorderLayout.NORTH);

与上述情况相同。 JPanel 有一个默认的 FlowLayout。您需要将布局设置为 BorderLayout

您还应该将 detailPanel 添加到 mainPanelCENTER


还应将 JToolBar 添加到具有 BorderLayout 的容器中

toolBarPanel = new JPanel();
toolBarPanel.add(jToolbar = new JToolBar());

toolBarPanel设置为BorderLayout

关于java - JFrame 布局与 JPanels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22884496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com