这是我的代码。我希望按键监听器在按下某个键时创建一个矩形。我无法在填充矩形方法中实现填充矩形方法。
我试图将主要听众分开,但是,我不知道如何将它们相互连接以使用JPanel方法。
import javax.swing.*;
import java.awt.*;
public class Keyboard
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Keyboard");
frame.setLayout(new BorderLayout());
MyPanel displayPanel = new MyPanel();
frame.add(displayPanel, BorderLayout.CENTER);
frame.addKeyListener(displayPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
frame.setVisible(true);
}
}
MyPanel类
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements KeyListener {
private boolean one;
private boolean two;
private boolean three;
private boolean four;
private boolean five;
private boolean six;
private boolean seven;
private boolean eight;
private boolean nine;
private boolean zero;
private boolean dash;
public MyPanel() {
one = false;
two = false;
three = false;
four = false;
five = false;
six = false;
seven = false;
eight = false;
nine = false;
zero = false;
dash = false;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for(int i=0; i<11; i++)
{
if(i%2 == 0)
{
g.setColor(Color.WHITE);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 212, 1000);
}
else
{
g.setColor(Color.BLACK);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 174, 1000);
}
if(one==true)
{
g.setColor(Color.BLUE);
g.fillRect(50,50,50,50);
repaint();
}
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1 && !one)
{
System.out.println("asdasda");
one = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_2 && !two)
{
two = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_3 && !three)
{
three = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_4 && !four)
{
four = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_5 && !five)
{
five = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_6 && !six)
{
six = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_7 && !seven)
{
seven = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_8 && !eight)
{
eight = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_9 && !nine)
{
nine = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_0 && !zero)
{
zero = true;
}
else if(e.getKeyCode() == 189 && !dash)
{
dash = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_2)
{
two = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_3)
{
three = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_4)
{
four = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_5)
{
five = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_6)
{
six = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_7)
{
seven = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_8)
{
eight = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_9)
{
nine = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_0)
{
zero = false;
}
else if(e.getKeyCode() == 189)
{
dash = false;
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("Typed: " + e.getKeyChar());
}
}
答案 0 :(得分:0)
前一段时间我遇到过类似的问题,我通过创建一个像这样的小矩形类来修复它
package notDeafult;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
public class rectangle {
//The basic outline of a rectangle making up the grid
Rectangle rect;
int width;
int height;
public rectangle() {
}
public rectangle(int x, int y) {
this.width = 80; //Setting the size
this.height = 80;
this.rect = new Rectangle(x + 80, y + 30, width, height);//Moving the object to the centre
}
public void draw(Graphics g) {
g.setColor(Color.green);
//.drawRect only draws the outline
g.drawRect(rect.x, rect.y, width, height);
}
}
然后当你准备就绪时,只需在新的Jpanel下调用paint方法, 像这样:
jPanel3 = new javax.swing.JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
rectangle rect = new rectangle(width, height);
rect.draw(g)
}
然后你可以给paintComponent一个自己的方法名,比如paintRectangles或者什么
并在按下按键时在此处调用
else if(e.getKeyCode() ==KeyEvent.VK_5){
paintRectangles();
}
我想这样的事情应该有用。
答案 1 :(得分:0)
我在你班上做了一些改变:
paintRectangle
方法public class MyPanel extends JPanel implements KeyListener {
private boolean one, two, three, four, five, six, seven, eight, nine, zero, dash;
public MyPanel() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for (int i = 0; i < 11; i++) {
if (i % 2 == 0) {
g.setColor(Color.WHITE);
g.fillRect(
i * (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width
/ 11),
450, 212, 1000);
} else {
g.setColor(Color.BLACK);
g.fillRect(
i * (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width
/ 11),
450, 174, 1000);
}
}
paintRectangle(g);
}
private void paintRectangle(Graphics g) {
if (one) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 50, 50);
} else if (two) {
g.setColor(Color.CYAN);
g.fillRect(50, 50, 50, 50);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1 && !one) {
System.out.println("asdasda");
one = true;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_2 && !two) {
two = true;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_3 && !three) {
three = true;
} else if (e.getKeyCode() == KeyEvent.VK_4 && !four) {
four = true;
} else if (e.getKeyCode() == KeyEvent.VK_5 && !five) {
five = true;
} else if (e.getKeyCode() == KeyEvent.VK_6 && !six) {
six = true;
} else if (e.getKeyCode() == KeyEvent.VK_7 && !seven) {
seven = true;
} else if (e.getKeyCode() == KeyEvent.VK_8 && !eight) {
eight = true;
} else if (e.getKeyCode() == KeyEvent.VK_9 && !nine) {
nine = true;
} else if (e.getKeyCode() == KeyEvent.VK_0 && !zero) {
zero = true;
} else if (e.getKeyCode() == 189 && !dash) {
dash = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1) {
one = false;
} else if (e.getKeyCode() == KeyEvent.VK_2) {
two = false;
} else if (e.getKeyCode() == KeyEvent.VK_3) {
three = false;
} else if (e.getKeyCode() == KeyEvent.VK_4) {
four = false;
} else if (e.getKeyCode() == KeyEvent.VK_5) {
five = false;
} else if (e.getKeyCode() == KeyEvent.VK_6) {
six = false;
} else if (e.getKeyCode() == KeyEvent.VK_7) {
seven = false;
} else if (e.getKeyCode() == KeyEvent.VK_8) {
eight = false;
} else if (e.getKeyCode() == KeyEvent.VK_9) {
nine = false;
} else if (e.getKeyCode() == KeyEvent.VK_0) {
zero = false;
} else if (e.getKeyCode() == 189) {
dash = false;
}
}
public void keyTyped(KeyEvent e) {
System.out.println("Typed: " + e.getKeyChar());
}
}
答案 2 :(得分:0)
我弄明白了这个问题。这是您的问题的代码。
import javax.swing.*;
import java.awt.*;
public class Keyboard
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Keyboard");
frame.setLayout(new BorderLayout());
MyPanel displayPanel = new MyPanel();
frame.add(displayPanel, BorderLayout.CENTER);
frame.addKeyListener(displayPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
frame.setVisible(true);
}
}
这是MyPanel类
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements KeyListener {
private boolean one;
private boolean two;
private boolean three;
private boolean four;
private boolean five;
private boolean six;
private boolean seven;
private boolean eight;
private boolean nine;
private boolean zero;
private boolean dash;
Graphics g;
public MyPanel() {
one = false;
two = false;
three = false;
four = false;
five = false;
six = false;
seven = false;
eight = false;
nine = false;
zero = false;
dash = false;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for(int i=0; i<11; i++)
{
if(i%2 == 0)
{
g.setColor(Color.WHITE);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 212, 1000);
}
else
{
g.setColor(Color.BLACK);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 174, 1000);
}
if(one==true)
{
g.setColor(Color.BLUE);
g.drawRect(50, 50, 50, 50);
g.fillRect(50,50,50,50);
repaint();
}
}
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = true;
repaint();
System.out.println("asdasda");
}
else if(e.getKeyCode() ==KeyEvent.VK_2 && !two)
{
two = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_3 && !three)
{
three = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_4 && !four)
{
four = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_5 && !five)
{
five = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_6 && !six)
{
six = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_7 && !seven)
{
seven = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_8 && !eight)
{
eight = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_9 && !nine)
{
nine = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_0 && !zero)
{
zero = true;
}
else if(e.getKeyCode() == 189 && !dash)
{
dash = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_2)
{
two = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_3)
{
three = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_4)
{
four = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_5)
{
five = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_6)
{
six = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_7)
{
seven = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_8)
{
eight = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_9)
{
nine = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_0)
{
zero = false;
}
else if(e.getKeyCode() == 189)
{
dash = false;
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("Typed: " + e.getKeyChar());
}
}
您的代码无效的原因是您在设置 one = true; 后未调用 repaint()方法。现在你的代码正在运行。如果有任何混淆,请检查并告诉我。
注意:这仅适用于密钥1.现在对其他密钥执行类似操作。