gpt4 book ai didi

java - 菜单栏未添加到 java 分割 Pane 中

转载 作者:行者123 更新时间:2023-12-01 23:38:43 25 4
gpt4 key购买 nike

我编写了一个小型测试程序,它创建一个拆分 Pane ,其中一个 Pane 是文本区域。我已在 Pane 中添加了 meubar 和 menuitems,但我在创建的 gui 中没有看到它们。

有人可以指出我在下面的程序中做错了什么吗:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JMenuBar;
import java.util.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.io.IOException;


//SplitPaneDemo itself is not a visible component.
public class SplitPaneDemo extends JFrame
implements ActionListener {

private JTextArea ta;
private JMenuBar menuB;
private JMenu dbM;
private JMenuItem cnadb,bsmdb,cdmdb;
private JLabel picture;


private JSplitPane splitPane;
public SplitPaneDemo() {

ta = new JTextArea(); //textarea
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke )
{

int code = ke.getKeyCode();
int modifiers = ke.getModifiers();
if(code == KeyEvent.VK_ENTER && modifiers == KeyEvent.CTRL_MASK)
{
System.out.println("cmd in table:");
}
}
});

JScrollPane taPane = new JScrollPane(ta);
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);

JScrollPane pictureScrollPane = new JScrollPane(picture);

splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
taPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(450);

//Provide minimum sizes for the two components in the split pane.
Dimension minimumSize = new Dimension(100, 100);
taPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);

//Provide a preferred size for the split pane.
splitPane.setPreferredSize(new Dimension(900, 900));

menuB = new JMenuBar(); //menubar
dbM = new JMenu("DB"); //file menu
cnadb = new JMenuItem("CNA");
bsmdb = new JMenuItem("BSM");
cdmdb = new JMenuItem("CDM");
setJMenuBar(menuB);
menuB.add(dbM);
dbM.add(cnadb);
dbM.add(bsmdb);
dbM.add(cdmdb);
cnadb.addActionListener(this);
bsmdb.addActionListener(this);
cdmdb.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
public void valueChanged(ListSelectionEvent e) {
}

public JSplitPane getSplitPane() {
return splitPane;
}


private static void createAndShowGUI() {

//Create and set up the window.
JFrame frame = new JFrame("SplitPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
frame.getContentPane().add(splitPaneDemo.getSplitPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}


}

最佳答案

您添加JMenuBar在你的SplitPaneDemo类,但是当您实际调用 createAndShowGUI 时,您会创建一个新的 JFrame并且只添加 SplitPane调用 getSplitPane 即可。这个新框架不知道菜单栏。

如果您要延长JFrameSplitPaneDemo ,为什么不使用它来为你的 gui 制作框架呢?

关于java - 菜单栏未添加到 java 分割 Pane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18273693/

25 4 0