单击按钮时,将从另一个类调用JPanel独立类来显示

时间:2013-03-18 17:50:23

标签: java swing jpanel layout-manager

问题是我要调用的另一个类(Clients.java)是一个独立的类,没有在PosFrame.java类中定义。我希望能够在posFrame类中单击按钮时将Clients类调用到PosFrame中 这是我的代码

public PosFrame(){

     gui();
}
public void gui(){
    frame.setSize(xSize,ySize);
    frame.setLayout(new BorderLayout());

    top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    top_panel.setMaximumSize(null);
    top_panel.setBackground(Color.LIGHT_GRAY);
    top_panel.setBorder(BorderFactory.createEtchedBorder());
    int top_height = (int)(Math.round(ySize * 0.05));
    top_panel.setPreferredSize(new Dimension(xSize,top_height));
    add(top_panel, BorderLayout.NORTH);

    center_panel = new JPanel(new BorderLayout());
    center_panel.setMaximumSize(null);
    add(center_panel, BorderLayout.CENTER);


    displayPanel();

    titlePanel = new JPanel();
    titleLabel = new JLabel("Order");
    titleLabel.setFont(tf);
    titleLabel.setForeground(Color.blue);
    //titlePanel.setBackground(Color.SILVER);
    titlePanel.add(titleLabel);
    titlePanel.setBorder(BorderFactory.createEtchedBorder());
    int title_height = (int)(Math.round(xSize * 0.03));
    titlePanel.setPreferredSize(new Dimension(xSize,title_height));
    center_panel.add(titlePanel,BorderLayout.NORTH);


    addTopButton();
}

我的动作执行了代码

public void actionPerformed(ActionEvent evt){
    if(evt.getSource() == newOrder_btn){
        displayPanel.removeAll();
        buttonPanel.removeAll();
        titleLabel.setText("Order");
        displayPanel();
        displayPanel.revalidate();
    }
    if(evt.getSource() == order_btn){
        displayPanel.removeAll();
        titleLabel.setText("Orders");
        showOrderBottomButtons();
        displayPanel.revalidate();
    }
    if(evt.getSource() == client_btn){
        client = new Clients();
        displayPanel.removeAll();
        titleLabel.setText("Client");
        displayPanel.add(client);
        displayPanel.revalidate();
    }
       }

显示JPanels的代码

 public void displayPanel(){
    tp = new JTabbedPane();
    String[] tablenumbers ={"1","2","3"};
    constraints = new GridBagConstraints();
    layout=  new GridBagLayout();
    displayPanel = new JPanel(new BorderLayout());

    topLeft.add(upPanel);
    topLeft.add(downPanel);


    tablePanel = showitemsOrdered();
    middleLeft.add(tablePanel);

    totalPanel = showTotalPanel();        
    bottomLeft.add(totalPanel);

    leftContent.add(topLeft,BorderLayout.NORTH);
    leftContent.add(middleLeft,BorderLayout.CENTER);
    leftContent.add(bottomLeft,BorderLayout.SOUTH);
    rightContent.add(new JPanel());
    tp.addTab("Order",leftContent);
    tp.addTab("Details",rightContent);

    leftPanel.add(tp,BorderLayout.WEST);
    leftPanel.add(panelToShowButtons,BorderLayout.EAST);

    displayPanel.add(leftPanel,BorderLayout.WEST);

    buttonPanel = new JPanel();
    buttonPanel.add(addOrderButtons());
    int button_height = (int)(Math.round(xSize * 0.04));
    buttonPanel.setPreferredSize(new Dimension(xSize,button_height));
    displayPanel.add(buttonPanel,BorderLayout.SOUTH);
    center_panel.add(displayPanel, BorderLayout.CENTER);
}

来自我想要用来替换Jpanel内容的类的代码

public class Clients extends JPanel{
JPanel clientPanel,searchPanel,clientsDisplayPanel,clientsInputPanel;

JPanel cpanel = new JPanel();
   public Clients(){


   cpanel.add(showClientPanel());
    //setPreferredSize(new Dimension(xSize,ySize));
    cpanel.setVisible(true);
}
private JPanel showClientPanel(){
    clientPanel = new JPanel(new GridLayout(3,0));

    clientPanel.add(showSearchPanel());
    return clientPanel;
}
public JPanel showSearchPanel(){
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.setPreferredSize(new Dimension(xSize,40));

    searchForClientsLabel = new JLabel("Search for clients");
    searchForClientsLabel.setPreferredSize(new Dimension(150,20));

    searchClientTextfield = new JTextField();

    findNowBtn = new JButton("Find now");
    clearBtn  = new JButton("Clear");
    panel.add(searchForClientsLabel);
    panel.add(searchClientTextfield);
    panel.add(findNowBtn);
    panel.add(clearBtn);

    return panel;
}

}

单击客户端按钮时,不显示第二个类 任何人都可以帮助我 提前谢谢,如果代码太长,请耐心等待。 joedbug

0 个答案:

没有答案