我有一个关于JFrame的问题,还有一个清单。我创建了一个帮助菜单按钮。现在的情况是:
点击时,会弹出一个新窗口。在此窗口中,我希望有一个按名称列出一些公式的列表。单击列表中的公式后,我希望在相同的屏幕上显示公式所代表的内容。
它必须类似于可滚动列表中左边的公式,在右侧的相同屏幕上,在某种类型的文本框中,描述单击的公式。
有人知道怎么做吗?
menuItem = new JMenuItem("Help");
menuItem.setMnemonic(KeyEvent.VK_H);
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame help = new JFrame("HELP");
help.setTitle("Help");
help.setSize(400,200);
help.setLocationRelativeTo(null);
help.setDefaultCloseOperation(help.DISPOSE_ON_CLOSE);
help.setVisible(true);
help.add(label);
String labels[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y"};
JList list = new JList(labels);
JScrollPane scrollPane = new JScrollPane (list);
Container contentPane = help.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
}
});发布于 2013-12-15 20:57:25
下面是一个基本的实现。
第二个(公式)窗口看起来如何像
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Window;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormulaWindow extends JDialog {
public FormulaWindow(final Window parent) {
super(parent, "Formula window", ModalityType.MODELESS);
final Container cp = getContentPane();
cp.setLayout(new BorderLayout());
setSize(500, 400);
final Map<String, String> formulaDescritions = new HashMap<>();
formulaDescritions.put("Formula 1", "<html>How the world works.</html>");
formulaDescritions.put("Formula 2", "<html>How woman work.</html>");
formulaDescritions.put("Formula 3", "<html>How programming works.</html>");
final JList<String> formulaList = new JList<String>(new Vector<String>(formulaDescritions.keySet()));
formulaList.setPreferredSize(new Dimension(100, 100));
final JLabel descriptionLabel = new JLabel();
descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
formulaList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
final JList<String> list = (JList<String>) evt.getSource();
descriptionLabel.setText(formulaDescritions.get(list.getSelectedValue()));
}
});
cp.add(new JScrollPane(formulaList), BorderLayout.WEST);
cp.add(new JScrollPane(descriptionLabel), BorderLayout.CENTER);
}
}如何在主窗口中选择菜单项时打开它
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class MainWindow extends JFrame {
private class ShowFormulaWindowAction extends AbstractAction {
public ShowFormulaWindowAction() {
super("Show formulas...");
}
public void actionPerformed(ActionEvent actionEvent) {
FormulaWindow formulaWindow = new FormulaWindow(MainWindow.this);
formulaWindow.setVisible(true);
}
}
public MainWindow() {
super("Main window");
JMenu fileMenu = new JMenu("Extras");
fileMenu.add(new JMenuItem(new ShowFormulaWindowAction()));
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(final String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainWindow mainWindow = new MainWindow();
mainWindow.setSize(350, 250);
mainWindow.setVisible(true);
}
});
}
}进一步注意到
次要窗口不应该是JFrame,而应该是JDialog。以那种方式
发布于 2013-12-15 20:31:40
建议:
链接:
编辑
你在评论中说,
它必须类似于我在左边有一个滚动条的公式,在屏幕的右边有一个带有公式描述的文本框。
这难道不应该是任何其他相关的信息和限制的一部分,你原来的问题?如果我是你,我会编辑原来的帖子,并提供所有必要的信息,以便我们能够完全理解你的问题,包括相关的代码(最好是一个斯考斯)。所以..。
编辑2
关于您的最新代码帖子:
编辑3
是否有方法将大小设置为滚动窗格BorderLayout.LINE_START的固定大小?
与其尝试设置任何方法的大小,不如考虑在JList上调用一些方法,例如,setPrototypeCellValue(E prototypeCellValue)和setVisibleRowCount(int visibleRowCount),这些方法将允许组件根据合理的数据和初始假设设置自己的preferredSize。有关这些方法和其他JList API方法的详细信息,请查看JList。
https://stackoverflow.com/questions/20599383
复制相似问题