这是菜单栏类,如果将它直接放入它所使用的主类中,但是在它自己的类中它不会因为没有构造函数而且我很难做到这一点我理解构造函数的重要性但是似乎无法得到我的围绕如何将其添加到此类
Memory required for data: 493376512
头等 -
public class MenuBar extends mainClass {
JFrame frame = new JFrame("Assingment");
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JPopupMenu popup;
{
menuBar = new JMenuBar();
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription("File Menu");
menuBar.add(menu);
menuItem = new JMenuItem("Load");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
switch (fileChooser.showOpenDialog(frame)) {
case JFileChooser.APPROVE_OPTION:
break;
}
}
});
menuItem = new JMenuItem("Save", KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Save");
menu.add(menuItem);
menuItem = new JMenuItem("Exit",KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("Exit");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
menu.add(menuItem);
menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription("Help Menu");
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
Mainclass -
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel {
private final static Color BACKGROUND_COL = Color.DARK_GRAY;
private BufferedImage image;
public void drawLine(Color color, int x1, int y1, int x2, int y2) {
Graphics g = image.getGraphics();
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
public void clear() {
Graphics g = image.getGraphics();
g.setColor(BACKGROUND_COL);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
@Override
public void paint(Graphics g) {
// render the image on the panel.
g.drawImage(image, 0, 0, null);
}
GraphicsPanel() {
setPreferredSize(new Dimension(500, 300));
image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);
// Set max size of the panel, so that is matches the max size of the image.
setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
clear();
}
}
答案 0 :(得分:1)
public class TestGui extends JFrame {
public TestGui() {
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
this.getContentPane().setLayout(new BorderLayout());
createMenu(this.getContentPane());
createPanel(this.getContentPane());
this.pack();
this.setPreferredSize(this.getSize());
this.setLocationRelativeTo(null);
}
private void createMenu(Container pane) {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Datei");
menuBar.add(menu);
JMenuItem menuItemClose = new JMenuItem("Schließen");
JMenuItem menuItemEdit = new JMenuItem("Bearbeiten");
JMenuItem menuItemUndo = new JMenuItem("Rückgängig");
menuItemEdit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
menuItemEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
menuItemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItemClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JMenuItem menuItemAdd = new JMenuItem("Einfügen");
menuItemAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
menuItemAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
menuItemUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
menuItemUndo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
menu.add(menuItemAdd);
menu.add(menuItemEdit);
menu.add(menuItemUndo);
menu.add(menuItemClose);
menuBar.setMinimumSize(new Dimension(100, 21));
pane.add(menuBar, BorderLayout.PAGE_START);
}
private void createPanel(Container contentPane) {
JPanel g = new JPanel();
g.setPreferredSize(new Dimension(500, 300));
image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);
// Set max size of the panel, so that is matches the max size of the
// image.
g.setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
clear();
add(g);
}
private final static Color BACKGROUND_COL = Color.DARK_GRAY;
private BufferedImage image;
public void drawLine(Color color, int x1, int y1, int x2, int y2) {
Graphics g = image.getGraphics();
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
public void clear() {
Graphics g = image.getGraphics();
g.setColor(BACKGROUND_COL);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
@Override
public void paint(Graphics g) {
// render the image on the panel.
g.drawImage(image, 0, 0, null);
}
}
此代码将创建一个窗口,左上角有一个菜单栏和一个深灰色的jpanel。唯一的问题是,菜鸟是落后于 jpanel,但如果你用你的courser点击菜单,你会看到它