我想在 Jpanel 中添加一个 JMenuBar。我试过了:
p.add(menubar, BorderLayout.NORTH);
另外:我不想在JFrame上使用JSetMenu方法,因为我目前正在制作一个Astah插件,我希望Jpanel(Jmenubar所在的位置)在Astah中。
如何将 JMenuBar 设置为 Jpanel,使其位于顶部?我想把所有内容放在Jpanel中,然后放在Astah中。
将其放入 Astah 的代码:
import com.change_vision.jude.api.inf.project.ProjectAccessor;
import com.change_vision.jude.api.inf.project.ProjectAccessorFactory;
import com.change_vision.jude.api.inf.project.ProjectEvent;
import com.change_vision.jude.api.inf.project.ProjectEventListener;
import com.change_vision.jude.api.inf.ui.IPluginExtraTabView;
import com.change_vision.jude.api.inf.ui.ISelectionListener;
import javax.swing.*;
import java.awt.*;
public class showobjects extends JPanel implements IPluginExtraTabView, ProjectEventListener {
public showobjects () {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
add(createLabelPane());
addProjectEventListener();
}
private void addProjectEventListener() {
try {
ProjectAccessor projectAccessor = ProjectAccessorFactory.getProjectAccessor();
projectAccessor.addProjectEventListener(this);
} catch (ClassNotFoundException e) {
e.getMessage();
}
}
private Container createLabelPane() {
JLabel label = new JLabel("showobjects ");
JScrollPane pane = new JScrollPane(label);
showobjects app = new showobjects ();
JPanel mainJpanel = app.createJPanel();
return mainJpanel;
}
@Override
public void projectChanged(ProjectEvent e) {
}
@Override
public void projectClosed(ProjectEvent e) {
}
@Override
public void projectOpened(ProjectEvent e) {
}
@Override
public void addSelectionListener(ISelectionListener listener) {
}
@Override
public Component getComponent() {
return this;
}
@Override
public String getDescription() {
return "Show showobjects here";
}
@Override
public String getTitle() {
return "showobjects ";
}
public void activated() {
}
public void deactivated() {
}
}
我做了一个非常简单的测试,它对我来说工作得很好。根据我从您的代码中可以推断出的内容,您正在使用...
mainJPanel.add(AuguSoftModellerobjectsNew.createMenuBar());
这会将JMenuBar
置于面板的CENTRE
(或默认)位置,该位置将被您在那里添加的其他组件隐藏
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("Test"));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mb, BorderLayout.NORTH);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}