求Java高手帮我编写一个swing界面
界面左面是一个JList,当选中JList中一项时,界面右面显示相应的组件。比如选中JList中的“JButton“,右侧显示一个JButton组件,再选“JTabbed...
界面左面是一个JList,当选中JList中一项时,界面右面显示相应的组件。比如选中JList中的“JButton“,右侧显示一个JButton组件,再选“JTabbedPane”,右侧显示一个JTabbedPane组件,就像MyEclipse中某一个文件的属性页面。
展开
1个回答
展开全部
添加其它组件:
1. 在enum ItemList 中加入其它需要的组件名称,
2. 在 getComponent(ItemList item) 的 switch 中加入生成其它组件的代码.
import java.awt.Container;
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
enum ItemList {
J_BUTTON("JButton"),
J_LABEL("JLabel"),
DEFAULT("Default");
String name;
ItemList (String name) {
this.name = name;
}
}
public class JListSelectionDemo {
private static JFrame mainFrame = new JFrame();
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
initAndShow();
}
});
}
private static void initAndShow() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 300);
addDefaultComponents();
mainFrame.setVisible(true);
}
private static void addDefaultComponents() {
Container content = mainFrame.getContentPane();
content.setLayout(new BorderLayout());
content.add(getItemList(), BorderLayout.LINE_START);
JPanel panel = new JPanel();
panel.add(getDefaultLabel());
content.add(panel, BorderLayout.CENTER);
}
private static JList getItemList() {
Vector<String> itemList = new Vector<String>();
for (ItemList item : ItemList.values()) {
itemList.add(item.name);
}
JList list = new JList(itemList);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.getSelectionModel().addListSelectionListener(
new ListSelectionHandler());
return list;
}
private static JLabel getDefaultLabel() {
return new JLabel("Please select a valid item from list");
}
private static JComponent getComponent(ItemList item) {
JPanel panel = new JPanel();
JComponent newItem;
switch (item) {
case J_BUTTON:
newItem = new JButton("Demo Button");
newItem.setSize(100, 50);
break;
case J_LABEL:
newItem = new JLabel("Demo Label");
break;
default:
newItem = getDefaultLabel();
break;
}
panel.add(newItem);
return panel;
}
static class ListSelectionHandler implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
int position = ((ListSelectionModel) e.getSource())
.getLeadSelectionIndex();
ItemList item = getItem(position);
Container content = mainFrame.getContentPane();
content.remove(1);
JComponent newItem = getComponent(item);
if (newItem != null)
content.add(newItem, BorderLayout.CENTER);
else
content.add(getDefaultLabel(), BorderLayout.CENTER);
mainFrame.setVisible(true);
}
private static ItemList getItem(int position) {
int counter = 0;
for (ItemList item : ItemList.values()) {
if (counter == position)
return item;
counter++;
}
return null;
}
}
}
1. 在enum ItemList 中加入其它需要的组件名称,
2. 在 getComponent(ItemList item) 的 switch 中加入生成其它组件的代码.
import java.awt.Container;
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
enum ItemList {
J_BUTTON("JButton"),
J_LABEL("JLabel"),
DEFAULT("Default");
String name;
ItemList (String name) {
this.name = name;
}
}
public class JListSelectionDemo {
private static JFrame mainFrame = new JFrame();
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
initAndShow();
}
});
}
private static void initAndShow() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 300);
addDefaultComponents();
mainFrame.setVisible(true);
}
private static void addDefaultComponents() {
Container content = mainFrame.getContentPane();
content.setLayout(new BorderLayout());
content.add(getItemList(), BorderLayout.LINE_START);
JPanel panel = new JPanel();
panel.add(getDefaultLabel());
content.add(panel, BorderLayout.CENTER);
}
private static JList getItemList() {
Vector<String> itemList = new Vector<String>();
for (ItemList item : ItemList.values()) {
itemList.add(item.name);
}
JList list = new JList(itemList);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.getSelectionModel().addListSelectionListener(
new ListSelectionHandler());
return list;
}
private static JLabel getDefaultLabel() {
return new JLabel("Please select a valid item from list");
}
private static JComponent getComponent(ItemList item) {
JPanel panel = new JPanel();
JComponent newItem;
switch (item) {
case J_BUTTON:
newItem = new JButton("Demo Button");
newItem.setSize(100, 50);
break;
case J_LABEL:
newItem = new JLabel("Demo Label");
break;
default:
newItem = getDefaultLabel();
break;
}
panel.add(newItem);
return panel;
}
static class ListSelectionHandler implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
int position = ((ListSelectionModel) e.getSource())
.getLeadSelectionIndex();
ItemList item = getItem(position);
Container content = mainFrame.getContentPane();
content.remove(1);
JComponent newItem = getComponent(item);
if (newItem != null)
content.add(newItem, BorderLayout.CENTER);
else
content.add(getDefaultLabel(), BorderLayout.CENTER);
mainFrame.setVisible(true);
}
private static ItemList getItem(int position) {
int counter = 0;
for (ItemList item : ItemList.values()) {
if (counter == position)
return item;
counter++;
}
return null;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询