- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法让我的 JMenuBar 出现在我的 paint() 方法旁边。
package Main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Main extends JFrame{
int x, y; // axis position for oval
//JMenuBar Variables
JMenuBar menuBar;
JMenu file;
JMenuItem newGame;
JMenuItem checkScore;
JMenuItem exitGame;
// DOUBLE BUFFERING
private Image dbImage;
private Graphics dbGraphics;
Font font = new Font("Arial", Font.ITALIC, 30);
// KeyListener Class
public class KeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
if(x <=0){ // FRAME COLLISION DETECTION
x=0;
}else
x += -5; //decrement position to the left
}
if(keyCode == e.VK_RIGHT){
if(x >= 380){
x = 380;
}else
x += +5; //incrementing position to the right
}
if(keyCode == e.VK_UP){
if (y <= 25){
y = 25;
}else
y += -5; //decrementing position up
}
if(keyCode == e.VK_DOWN){
if(y >= 380){
y = 380;
}else
y += +5; //incrementing position down
}
}
}
// CONSTRUCTOR
public Main(){
// Window Properties
addKeyListener(new KeyListener()); // creates instance of KeyListener class
setTitle("Tower Defence");
setSize(400, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBackground(Color.CYAN);
// JMenuBar
menuBar = new JMenuBar();
file = new JMenu("File");
newGame = new JMenuItem("New Game");
checkScore = new JMenuItem("Check High Scores");
exitGame = new JMenuItem("Close Game");
menuBar.add(file);
file.add(newGame);
file.add(checkScore);
file.addSeparator();
file.add(exitGame);
setJMenuBar(menuBar);
// Display frame after all components added
setVisible(true);
// default position for oval
x = 150;
y = 150;
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight()); // creates image of screen
dbGraphics = dbImage.getGraphics(); // gets graphics to be drawn in off screen image
paintComponent(dbGraphics); // paints graphics
g.drawImage(dbImage, 0, 0, this); // draw image to the visible screen
}
// PAINT GRAPHICS TO SCREEN
public void paintComponent(Graphics g){
g.setFont(font);
g.drawString("Hello World", 100, 200);
g.setColor(Color.red);
g.drawOval(x, y, 15, 15);
g.fillOval(x, y, 15, 15);
repaint();
}
// MAIN METHOD
public static void main(String[] args) {
new Main();
}
}
我看到了一个不同的问题,解决方案是覆盖 paint 方法并添加 super.paint(g);当我尝试这个时,JMenuBar 出现但框架不断闪烁。
最佳答案
哎呀,我会把我的代码扔进戒指。根据我的意见推荐。另外,不要使用 KeyListeners,而是使用 Key Bindings。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class Main2 extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int OVAL_W = 15;
private int x = 150;
private int y = 150;
private JMenuBar menuBar;
private JMenu file;
private JMenuItem newGame;
private JMenuItem checkScore;
private JMenuItem exitGame;
private Font font = new Font("Arial", Font.ITALIC, 30);
public Main2() {
menuBar = new JMenuBar();
file = new JMenu("File");
newGame = new JMenuItem("New Game");
checkScore = new JMenuItem("Check High Scores");
exitGame = new JMenuItem("Close Game");
menuBar.add(file);
file.add(newGame);
file.add(checkScore);
file.addSeparator();
file.add(exitGame);
addKeyBinding();
}
public JMenuBar getMenuBar() {
return menuBar;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(font);
g.drawString("Hello World", 100, 200);
g.setColor(Color.red);
g.drawOval(x, y, OVAL_W, OVAL_W);
g.fillOval(x, y, OVAL_W, OVAL_W);
}
private void addKeyBinding() {
int condition = WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
for (final MyDirection dir : MyDirection.values()) {
KeyStroke keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0);
inputMap.put(keyStroke, dir.toString());
actionMap.put(dir.toString(), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent evt) {
int newX = x + dir.getxTrans();
int newY = y + dir.getyTrans();
newX = Math.min(newX, PREF_W - 2 * OVAL_W);
newX = Math.max(newX, OVAL_W);
newY = Math.min(newY, PREF_H - 2 * OVAL_W);
newY = Math.max(newY, OVAL_W);
x = newX;
y = newY;
repaint();
}
});
}
}
enum MyDirection {
UP(KeyEvent.VK_UP, 0, -5), DOWN(KeyEvent.VK_DOWN, 0, 5),
LEFT(KeyEvent.VK_LEFT, -5, 0), RIGHT(KeyEvent.VK_RIGHT, 5, 0);
private int keyCode;
private int xTrans;
private int yTrans;
private MyDirection(int keyCode, int xTrans, int yTrans) {
this.keyCode = keyCode;
this.xTrans = xTrans;
this.yTrans = yTrans;
}
public int getKeyCode() {
return keyCode;
}
public int getxTrans() {
return xTrans;
}
public int getyTrans() {
return yTrans;
}
}
private static void createAndShowGui() {
Main2 main = new Main2();
JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(main);
frame.setJMenuBar(main.getMenuBar());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - 带有 paint 方法的 JMenuBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13845922/
我正在用Java编写一个基本的计时器程序,在该程序中我希望在顶部有一个菜单栏。我现在拥有的代码是: public Main() { JMenuBar menubar = new JMenuBa
我不确定为什么我的 JMenuBar 没有显示。我添加的其他 2 个面板显示得很好。我不确定它是否与 borderLayout 挑剔或什么有关。我确信(希望)这是一件小事。我正在学习如何创建 GUI,
我刚刚开始学习 Java,我一直在阅读 this文档。我不喜欢复制一堆代码然后粘贴。因此,我一直在尝试一次一件事情地完成文档。 我已经有了一个可用的 JFrame,并决定从添加一个菜单开始。 这是我的
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有以下 JMenuBar 代码。如何将我的按键监听器 CustomKeyListener 添加到整个 JMenuBar? package UI; import javax.swing.*; impo
我在 jMeun 栏上遇到问题,我的代码可以编译,但是当我尝试在显示中打开它时。出现空指针异常。我不知道为什么会这样。问题指出了我的代码中发生的 3 行。在“createMenuBar”方法中fram
开发一个 Java 应用程序,该应用程序打开一个窗口并允许用户在窗口上绘图,就像 Canvas 一样。 我刚刚添加了一个JMenuBar,最终将允许用户使用不同的绘图工具。问题是 JMenuBar 在
我创建了一个基于 Swing 的小型应用程序。为此,我使用 JMenuBar 类创建了一个菜单栏。但我想为该菜单栏应用光泽颜色。 特别是,我正在查看所看到的效果 here :第五个示例中的渐变颜色和第
我正在尝试将 JMenuBar 的子类添加到用户界面,但由于某种原因它从未显示。我尝试过使用 JFrame.setJMenubar() 和 JFrame.add(),我尝试过从 SwingUtilit
大家好,我已经被动地使用过这个页面很多次了,因为有很多很好的解释,但今天到目前为止,我不得不自己问一个问题,因为在我看来,发生的事情很奇怪。 我使用 Javax.Swing 中的 JFrame 编写了
我正在学习 Java 中的 GUI。我在这里有点困惑。当我像这样放置 window.setVisible(true); 时,如果调整 JMenuBar 的大小,我只会看到它(如果没有某种交互,它不会显
我有以下代码用于带有按钮的 GUI 和 JMenuBar 的代码。 MenuBarTest.java import javax.swing.*; import java.awt.*; import j
我试图让我的 JMenuBar 在 GUI 中显示,但它只是在顶部显示为一条 1 像素的线。 这是我的代码... public LibraryView() { setBounds(100,10
我有一个 GUI,我正在尝试向其中添加一个菜单栏,但每次我尝试添加 JmenuBar 时,它都说它需要一个 java.awt.menubar 并找到了一个它应该检测到的 java.swing.Jmen
我有一个包含 4 个项目的简单 JMenuBar 和一个包含一些文本字段微调器和组合框的面板。 现在,如果我在面板中使用 Spinner 或其他一些组件,Jmenu 项目似乎会在面板后面打开,我看不到
我用 Java 编写了一个简单的绘图程序,允许用户在不同的形状、大小和颜色之间进行选择(在 JMenuBar 中)并用鼠标绘制它们。在克服了程序的许多问题后(我最初是用 AWT 编写的,但后来被说服改
JMenuBar 没有开始显示 JMenuItem选择或显示 JMenu弹出窗口,直到它被第一次点击。单击 JMenuBar 中的某处后, 所有这些项目都会响应鼠标悬停。 我想绕过所需的初始点击,让它
我有以下设置 GUI 的函数。 public void go() { JFrame frame = new JFrame("Simplex RP Client V1.1 Beta");
我似乎做对了一切。我只需要实现一个简单的 JMenuBar 但它似乎不起作用。有人可以帮我解决这个问题吗? import java.awt.*; import java.awt.event.*; im
当我运行以下代码时,菜单栏会在光标移动到窗口的上部时显示。问题是,当我向上移动光标打开菜单但没有选择任何东西,然后将光标移出菜单栏区域时,它变得不可见但菜单的元素留在屏幕上。 我试图实现的是一个“自动
我是一名优秀的程序员,十分优秀!