使用方法在Jpanel中添加JButton如何在视图中添加最后一个按钮?

时间:2012-10-14 15:41:38

标签: java swing jpanel

每当我调用我的添加corePanel或添加electivePanel方法时,他们只需替换添加的最后一个按钮,而不是在我在Jpanel中设置的4列中添加一个新按钮。

我知道我已经将变量Panel命名为真正的按钮,但这是无关紧要的。

我真的需要一些帮助。

由于

编辑: 现在当我将鼠标移到按钮上时,它们内部的所有信息(Jpanels)都消失了吗?

package Assignment2.view;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import ams.model.Course;

public class CoursePanel extends JPanel 
{
private JButton corePanel = new JButton();
private JButton electivePanel = new JButton();
private JLabel ccLabel = new JLabel("Code: ");
private JLabel titleLabel = new JLabel("Title: ");
private JLabel cpLabel = new JLabel("Credit Points: ");
private JLabel prLabel = new JLabel("Prereqs: ");
private Border blackline;


public CoursePanel()
{   
    setLayout(new GridLayout(0,4));
}



public void addcorePanel(Course createcourse)
{
    // core panel properties
    blackline = BorderFactory.createLineBorder(Color.black,10);

    corePanel.setLayout(new GridLayout(4,1));
    corePanel.setBackground(Color.GRAY);
    corePanel.setBorder(blackline);
    corePanel.add(ccLabel);
    corePanel.add(titleLabel);
    corePanel.add(cpLabel);
    corePanel.add(prLabel);
    corePanel.setAlignmentX(LEFT_ALIGNMENT);
    corePanel.setAlignmentY(TOP_ALIGNMENT);

    //Needed to rebuild the prerequisites for display.
    String[] prereqs = null;
    String result = null;
    prereqs = createcourse.getPreReqs();

    if (prereqs != null)
    {
    StringBuilder builder = new StringBuilder();
    for(String s : prereqs) 
    {
        builder.append(s);
        builder.append("\n");
    }
    result = builder.toString();
    }
    ccLabel.setText("Code: " + createcourse.getCode());
    titleLabel.setText("Title: " + createcourse.getTitle());
    cpLabel.setText("Credit points: " + Integer.toString(createcourse.getCreditPoints()));
    prLabel.setText("Prereqs: " + result);

    add(corePanel);

}


public void addelectivePanel(Course createcourseE)
{


    //elective panel properties
    electivePanel.setLayout(new GridLayout(4,1));
    electivePanel.setBackground(Color.GRAY);
    electivePanel.add(ccLabel);
    electivePanel.add(titleLabel);
    electivePanel.add(cpLabel);
    electivePanel.add(prLabel);
    electivePanel.setAlignmentX(LEFT_ALIGNMENT);
    electivePanel.setAlignmentY(TOP_ALIGNMENT);

    //Needed to rebuild the prerequisites for display.
    String[] prereqs = null;
    String result = null;
    prereqs = createcourseE.getPreReqs();

    if (prereqs != null)
    {
    StringBuilder builder = new StringBuilder();
    for(String s : prereqs) 
    {
        builder.append(s);
        builder.append("\n");
    }
    result = builder.toString();
    }

    ccLabel.setText("Code: " + createcourseE.getCode());
    titleLabel.setText("Title: " + createcourseE.getTitle());
    cpLabel.setText("Credit points: " + Integer.toString(createcourseE.getCreditPoints()));
    prLabel.setText("Prereqs: " + result);


    add(electivePanel);


}

}

2 个答案:

答案 0 :(得分:1)

因为corePanel被定义为成员变量,所以只有该组件的实例。因此,无论调用addcorePanel多少次,它都只会出现在面板的一个位置/列上。组件electivePanel以及两种方法中使用的所有其他组件也是如此。

解决方案是创建这些局部变量,以便创建新组件:

public void addcorePanel(Course createcourse) {
   // core panel properties
   blackline = BorderFactory.createLineBorder(Color.black, 10);

   // new local instances
   JButton corePanel = new JButton(); 
   JLabel ccLabel = new JLabel("Code: ");
   JLabel titleLabel = new JLabel("Title: ");
   JLabel cpLabel = new JLabel("Credit Points: "); 
   // etc.
   ...
   add(corePanel);
}

答案 1 :(得分:1)

这是因为您的标签和按钮(corePanelelectivePanel)是类变量,因此当您调用示例addcorePanel时,而不是创建新的JButton '操纵相同的corePanel按钮。您应该在方法体内移动变量,如下所示:

public void addcorePanel(Course createcourse)
{
    JButton corePanel = new JButton();
    JLabel ccLabel = new JLabel("Code: ");
    JLabel titleLabel = new JLabel("Title: ");
    JLabel cpLabel = new JLabel("Credit Points: ");
    JLabel prLabel = new JLabel("Prereqs: ");

// ....

// ....
}

JLabelsJButtons无法共享,因此每次调用方法时都必须创建新实例。