简体   繁体   English

Jpanels为什么不起作用?

[英]Why aren't the Jpanels working?

I am trying to write a program with JPanels and for the life of me, I can't seem to get the JPanels to go into the proper positions. 我正在尝试使用JPanels编写程序,为了我的一生,我似乎无法让JPanels进入正确的位置。 I don't have a clue what I am doing wrong. 我不知道我在做什么错。

Here is some of the code I have so far: 这是我到目前为止的一些代码:

package mainGUIWindowFrames;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CustomerWindow extends JFrame
{
//Attribute
private JTextField      textTF;
private JButton         copyButton;
private JLabel          copyLabel;
private Border          panelEdge;

//Constructor
public CustomerWindow()
{
    this.setBounds(100,100,800,600);
    panelEdge = BorderFactory.createEtchedBorder();

    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
    mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
    mainPanel.add(createSearchPanel(), BorderLayout.WEST);
}

//Operational Methods
public JPanel createCustomerPanel()
{
    JPanel customerPanel = new JPanel();

    JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
    customerL.setForeground(Color.blue);
    customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    customerPanel.add(customerL);


    customerPanel.setBorder(panelEdge);

    return customerPanel;
}

public JPanel createCustomerInfoPanel()
{
    JPanel infoPanel = new JPanel();
    infoPanel.setBorder(panelEdge);

    JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
    infoL.setForeground(Color.blue);
    infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    infoPanel.add(infoL);


    //add a text field
    textTF = new JTextField(50);
    infoPanel.add(textTF);

    //add a button
    copyButton = new JButton("Copy Text");
    copyButton.addActionListener(new ButtonListener());
    infoPanel.add(copyButton);

    copyLabel = new JLabel("-----------------");

    infoPanel.add(copyLabel);


    return infoPanel;
}

public JPanel createSearchPanel()
{
    JPanel lowerPanel = new JPanel();
    JLabel label = new JLabel("Text Transferred from JList:");

    //the spot where the data shows up

    lowerPanel.add(label);
    return lowerPanel;
}

The only Panel that shows up is the CreateCustomerPanel(). 显示的唯一面板是CreateCustomerPanel()。 I have no idea what I need to do to get the other two panels to work. 我不知道需要做什么才能使其他两个面板正常工作。

If you could help me out that would be great!! 如果您能帮助我,那就太好了!!


well I eventually wound up solving it by creating another panel and moving the panels I had out of the main constructor. 好吧,我最终通过创建另一个面板并将我拥有的面板移出主要构造函数来解决这个问题。

public CustomerWindow() {

    panelEdge = BorderFactory.createEtchedBorder();




}
public  JPanel createNorthPanel()
{
    JPanel customerPanel = new JPanel();
    JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
    customerL.setForeground(Color.blue);
    customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    customerPanel.add(customerL);


    customerPanel.setBorder(panelEdge);

    return customerPanel;

}   
//Operational Methods
public JPanel createCustomerPanel()
{
    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.add(createNorthPanel(), BorderLayout.NORTH);
    mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
    mainPanel.add(createSearchPanel(), BorderLayout.WEST);

    return mainPanel;


}


public JPanel createCustomerInfoPanel()
{
    JPanel infoPanel = new JPanel();
    Box vBox = Box.createVerticalBox();
    infoPanel.setBorder(panelEdge);


    JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
    infoL.setForeground(Color.blue);
    infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
    infoPanel.add(infoL);


    //add a text field
    clientId = new JTextField(10);
    vBox.add(clientId);
    vBox.add(Box.createVerticalStrut(25));
    fName = new JTextField(10);
    vBox.add(fName);
    vBox.add(Box.createVerticalStrut(25));
    lName = new JTextField(10);
    vBox.add(lName);
    vBox.add(Box.createVerticalStrut(25));
    address = new JTextField(10);
    vBox.add(address);
    vBox.add(Box.createVerticalStrut(25));
    postalCode = new JTextField(10);
    vBox.add(postalCode);
    vBox.add(Box.createVerticalStrut(25));
    number = new JTextField(10);
    vBox.add(number);
    vBox.add(Box.createVerticalStrut(25));
    type = new JTextField(10);
    vBox.add(type);


    infoPanel.add(vBox);


    return infoPanel;

Ive still got a lot of work to do but thanks to all those who helped me out!! 我还有很多工作要做,但要感谢所有帮助我的人!

Based on your example, nothing should show up, as you've not added mainPanel to anything. 根据您的示例,什么都不会出现,因为您尚未将mainPanel添加到任何内容。

Once I did that, I was able to get 一旦做到这一点,我就能

客户端

to show up... 露面...

Modified code example 修改后的代码示例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class CustomerWindow extends JFrame {
//Attribute

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                CustomerWindow frame = new CustomerWindow();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private JTextField textTF;
    private JButton copyButton;
    private JLabel copyLabel;
    private Border panelEdge;

//Constructor
    public CustomerWindow() {
        this.setBounds(100, 100, 800, 600);
        panelEdge = BorderFactory.createEtchedBorder();

        JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
        mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
        mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
        mainPanel.add(createSearchPanel(), BorderLayout.WEST);

        add(mainPanel);
    }

//Operational Methods
    public JPanel createCustomerPanel() {
        JPanel customerPanel = new JPanel();

        JLabel customerL = new JLabel("Clients", SwingConstants.CENTER);
        customerL.setForeground(Color.blue);
        customerL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
        customerPanel.add(customerL);

        customerPanel.setBorder(panelEdge);

        return customerPanel;
    }

    public JPanel createCustomerInfoPanel() {
        JPanel infoPanel = new JPanel();
        infoPanel.setBorder(panelEdge);

        JLabel infoL = new JLabel("Clients", SwingConstants.CENTER);
        infoL.setForeground(Color.blue);
        infoL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
        infoPanel.add(infoL);

        //add a text field
        textTF = new JTextField(50);
        infoPanel.add(textTF);

        //add a button
        copyButton = new JButton("Copy Text");
//        copyButton.addActionListener(new ButtonListener());
        infoPanel.add(copyButton);

        copyLabel = new JLabel("-----------------");

        infoPanel.add(copyLabel);

        return infoPanel;
    }

    public JPanel createSearchPanel() {
        JPanel lowerPanel = new JPanel();
        JLabel label = new JLabel("Text Transferred from JList:");

        //the spot where the data shows up
        lowerPanel.add(label);
        return lowerPanel;

    }

}

您没有在构造函数中添加主面板。

add(mainPanel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM