我是 Java GUI 的初学者。
我正在尝试使用 JList 构建用户界面,其中用户从已定义的列表中选择一个项目,并且与该特定项目相关的面板出现在列表的右侧。那是我的目标。不过,我首先要实现的是能够在选择某些菜单项时显示某些面板,但由于我的 . . .
这就是我到目前为止所做的。. .
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class MainGUI extends JFrame{
JList list = new JList(
new String[]{"Create Account","Borrow Book","Return Book",
"Add Book","Delete Book","Display Details"}
);
public MainGUI()
{
JPanel panel = new JPanel();
JPanel panel1 = 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);
panel.add(list);
add(panel,BorderLayout.WEST);
}
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);
}
}