为什么Jpanels不起作用?

时间:2014-03-03 03:24:44

标签: java swing jpanel grid-layout

我正在尝试用JPanels编写一个程序,但对于我的生活,我似乎无法让JPanels进入正确的位置。我不知道我做错了什么。

以下是我目前的一些代码:

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;
}

显示的唯一Panel是CreateCustomerPanel()。我不知道我需要做些什么来让其他两个面板工作。

如果你能帮助我,那就太棒了!


好吧,我最终通过创建另一个面板并移动我从主构造函数中移出的面板来解决它。

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;

我还有很多工作要做,但感谢所有帮助过我的人!!

2 个答案:

答案 0 :(得分:2)

根据您的示例,不应显示任何内容,因为您尚未向任何内容添加mainPanel

一旦我这样做,我就能得到

Clients

出现......

修改后的代码示例

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;

    }

}

答案 1 :(得分:2)

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

add(mainPanel);