0

add corePanelを呼び出すか、electivePanelメソッドを追加するときはいつでも、Jpanelで設定した4つの列に新しいボタンを追加する代わりに、追加された最後のボタンを置き換えるだけです。

変数が実際にはボタンである場合に、変数にPanelという名前を付けたことは承知していますが、それは関係ありません。

本当に助けが必要です。

ありがとう

編集:ボタンの上にマウスを置くと、ボタン内のすべての情報(Jpanel)が消えますか?

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


}

}

4

2 に答える 2

1

corePanelはメンバー変数として定義されているため、このコンポーネントのインスタンスのみがあります。したがって、何度addcorePanel呼び出されても、パネルの1つの場所/列にのみ表示されます。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);
}
于 2012-10-14T16:21:10.633 に答える
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: ");

// ....

// ....
}

JLabels共有できないため、JButtonsメソッドを呼び出すたびに新しいインスタンスを作成する必要があります。

于 2012-10-14T16:22:13.137 に答える