gpt4 book ai didi

java - 在没有frame.setJMenuBar的JPanel中显示JMenu

转载 作者:行者123 更新时间:2023-12-02 12:10:09 27 4
gpt4 key购买 nike

我想将 JMenuBar 添加到 Jpanel。我尝试过:

 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);
}

}

关于java - 在没有frame.setJMenuBar的JPanel中显示JMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com