端
我有以下带有按钮的GUI代码和JMenuBar的代码。
菜单栏测试.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import MenuBar.*;
public class MenuBarTest extends JFrame implements ActionListener
{
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel();
JButton btnTest = new JButton("Refresh Results");
GuiDMenuBar mbr;
public void runGUI()
{
myMainWindow.setBounds(10, 10, 800, 800);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(firstPanel);
myMainWindow.setJMenuBar(mbr.GuiDMenuBar());
myMainWindow.setVisible(true);
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
btnTest.setBounds(100, 100, 200, 50);
btnTest.addActionListener(this);
firstPanel.add(btnTest);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnTest)
{
System.out.println("Hi");
}
}
public static void main(String[] args)
{
MenuBarTest mBT = new MenuBarTest();
mBT.runGUI();
}
}
GuiDMenuBar.java
package MenuBar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class GuiDMenuBar extends JMenuBar
{
JMenu m_PopUps;
public JCheckBoxMenuItem m_p_AllPopUps,m_p_NamePopUps,m_p_TextPopUps,m_p_SizePopUps,m_p_ToolTipPopUps;
public GuiDMenuBar()
{
setBorderPainted(true);
makePopUpMenu();
}
void makePopUpMenu()
{
m_PopUps = new JMenu("Pop Ups");
m_PopUps.setMnemonic('P');
m_p_AllPopUps = new JCheckBoxMenuItem("Enable All Pop Ups");
m_p_NamePopUps = new JCheckBoxMenuItem("Enable Name Pop Ups");
m_p_SizePopUps = new JCheckBoxMenuItem("Enable Automatic Sizing Pop Ups");
m_p_TextPopUps = new JCheckBoxMenuItem("Enable Add Contents Pop Ups");
m_p_ToolTipPopUps = new JCheckBoxMenuItem("Enable Add ToolTips Pop Ups");
m_p_AllPopUps.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(m_p_AllPopUps.getState())
{
m_p_NamePopUps.setState(true);
m_p_SizePopUps.setState(true);
m_p_TextPopUps.setState(true);
m_p_ToolTipPopUps.setState(true);
}
else
{
m_p_NamePopUps.setState(false);
m_p_SizePopUps.setState(false);
m_p_TextPopUps.setState(false);
m_p_ToolTipPopUps.setState(false);
}
}
});
m_PopUps.add(m_p_AllPopUps);
m_PopUps.addSeparator();
m_PopUps.add(m_p_NamePopUps);
m_PopUps.add(m_p_SizePopUps);
m_PopUps.add(m_p_TextPopUps);
m_PopUps.add(m_p_ToolTipPopUps);
add(m_PopUps);
}
public void checkState()
{
System.out.println(m_p_AllPopUps.getState());
System.out.println(m_p_NamePopUps.getState());
System.out.println(m_p_SizePopUps.getState());
System.out.println(m_p_TextPopUps.getState());
System.out.println(m_p_ToolTipPopUps.getState());
}
}
我的目标是将JMenuBar
添加到JFrame
中。但是当我编译时,我收到一个错误,说它找不到GuiDMenuBar()
。我将非常感谢任何帮助。谢谢
您尝试调用构造函数,就好像它是静态方法一样。不要。而是通过将构造函数作为构造函数调用来创建实例,将实例分配给变量,然后使用它。变化:
// GuiDMenuBar mbr;
GuiDMenuBar mbr = new GuiDMenuBar(); // create instance and assign
public void runGUI()
{
// ....
// myMainWindow.setJMenuBar(mbr.GuiDMenuBar());
myMainWindow.setJMenuBar(mbr); // use instance
myMainWindow.setVisible(true);
}
接下来我们将讨论null
布局和setBounds(...)
的弊