私のコードの何が問題なのですか。
JList を作成し、項目を追加して左にプッシュしました (BorderLayout.WEST)。リスト項目をクリックするたびに、リストの右側にパネルを表示したい。しかし、問題は、リスト項目が選択され、リスナーが実行されると、選択されたインデックスに従って、別のクラス (内部クラスを含む) からそれに関連するパネルが表示されるべき if 選択に到達することです (しかし、そうではありません!) . . .
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class MainGUI extends JFrame{
ListListeners listListeners = new ListListeners();
JList list = new JList(
new String[]{"Create Account","Borrow Book","Return Book","Add Book","Delete Book","Display Details"}
);
JPanel panel1 = new JPanel();
public MainGUI()
{
CardLayout cardLayout = new CardLayout();
JPanel panel = new JPanel();
list.setForeground(Color.RED);
list.setBackground(Color.WHITE);
list.setSelectionForeground(Color.GREEN);
list.setSelectionBackground(Color.LIGHT_GRAY);
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//add(listListeners.new CreateAccount(panel1));
//ListListeners.CreateAccount createAccount = listListeners.new CreateAccount(panel1);
//add(createAccount.createAccountPanel());
//registering the JList listener
list.addListSelectionListener(new ListListener());
panel.add(list);
add(panel,BorderLayout.WEST);
}
class ListListener extends JFrame implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
int index = list.getSelectedIndex();
if(e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
System.out.println("No list item was selected");
} else {
if(index == 0)
{
ListListeners.CreateAccount createAccount = listListeners.new CreateAccount(panel1);
add(createAccount.createAccountPanel());
System.out.println(index);
}
else if(index == 1)
{
System.out.println(index);
}
else if(index == 2)
{
System.out.println(index);
}
else if(index == 3)
{
System.out.println(index);
}
else if(index == 4)
{
System.out.println(index);
}
else if(index == 5)
{
System.out.println(index);
}
}
}
}
}
public static void main(String[] args) {
MainGUI frame = new MainGUI();
frame.setSize(500, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
これは内部クラスを持つ別のクラスです
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ListListeners extends JFrame {
class CreateAccount extends JPanel
{
JLabel label = new JLabel("Enter new members' name : ");
JTextField textField = new JTextField("This is a text field");
JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
public CreateAccount(JPanel panel)
{
panel.add(label);
panel.add(textField);
panel.add(radioButton1,radioButton2);
add(panel,BorderLayout.CENTER);
}
public JPanel createAccountPanel()
{
JPanel panel = new JPanel();;
panel.add(label);
panel.add(textField);
panel.add(radioButton1);
return panel;
}
}
class BorrowBook extends JPanel
{
public BorrowBook(JPanel panel)
{
JLabel label = new JLabel("Just borrow the book and go : ");
JTextField textField = new JTextField("This is a second text field");
JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
panel.add(label);
panel.add(textField);
panel.add(radioButton1);
panel.add(radioButton2);
add(panel,BorderLayout.CENTER);
}
}
class ReturnBook extends JPanel
{
}
class AddBook extends JPanel
{
}
class DeleteBook extends JPanel
{
}
class DisplayDetails extends JPanel
{
}
}