- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我在底部的代码。首先要事。这不是家庭作业。这是我下次考试的学习指南。我在将 JLabel 公开时遇到问题,以便我可以在键盘监听器方法中使用它。您是否也可以看一下,看看当我公开 JLabel 时这是否合适?
public class Driver {
public static void main( String[] args ) {
Driver gui = new Driver();
// No other Java code should be added to this method
}
/**
* Our driver class
*/
public Driver() {
Window win = new Window();
win.setLocation(50, 30);
win.setSize(500, 400);
win.setBackground( Color.DARK_GRAY );
Oval circle;
circle = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
circle.setBackground( Color.MAGENTA );
circle.setLocation(150,100);
win.add(circle);
Triangle t;
t = new Triangle(win.getWidth()/2,win.getHeight()/2,120, 120, 0);
t.setSize(30,40);
t.setBackground( Color.MAGENTA );
t.setLocation(295,110);
win.add( t );
Triangle tt;
tt = new Triangle(win.getWidth()/100, win.getHeight()/2, 120, 120, 0);
tt.setSize(30,40);
tt.setBackground( Color.MAGENTA );
tt.setLocation(175,110);
win.add( tt );
Oval eye;
eye = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
eye.setBackground( Color.BLUE );
eye.setSize(25,25);
eye.setLocation(275,160);
win.add(eye);
Oval eye2;
eye2 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
eye2.setSize(25,25);
eye2.setBackground( Color.BLUE );
eye2.setLocation(210,160);
win.add(eye2);
Oval pupil1;
pupil1 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
pupil1.setSize(10,10);
pupil1.setBackground( Color.BLACK );
pupil1.setLocation(215,170);
win.add(pupil1);
Oval pupil2;
pupil2 = new Oval(win.getWidth()/2,win.getHeight()/2,200,200);
pupil2.setSize(10,10);
pupil2.setBackground( Color.BLACK );
pupil2.setLocation(280,170);
win.add(pupil2);
Color myColor = new Color (12,15,23);
Triangle nose;
nose = new Triangle(win.getWidth()/100, win.getHeight()/2, 120, 120, 0);
nose.setSize(30,40);
nose.setBackground( myColor );
nose.setLocation(240,190);
win.add( nose );
Rectangle mouth = new Rectangle (win.getWidth()/2,win.getHeight()/2,200,200);
mouth.setSize(50,20);
mouth.setBackground(Color.BLACK);
mouth.setLocation(230,250);
win.add(mouth);
Line whisker1;
whisker1 = new Line(280,245,255,240);
whisker1.setBackground( Color.BLACK );
win.add( whisker1 );
Line whisker2;
whisker2 = new Line(280,220,255,240);
whisker2.setBackground( Color.BLACK );
win.add( whisker2 );
Line whisker3;
whisker3 = new Line(220,245,255,240);
whisker3.setBackground( Color.BLACK );
win.add( whisker3 );
Line whisker4;
whisker4 = new Line(220,220,255,240);
whisker4.setBackground( Color.BLACK );
win.add( whisker4 );
final JLabel catName;
catName = new JLabel();
catName.setText("George, he likes buses :(");
catName.setBounds(200, 300, 150, 20);
catName.setForeground(Color.WHITE);
win.add(catName);
Line mouth2nose;
mouth2nose = new Line(255,220,255,250);
mouth2nose.setBackground( Color.BLACK );
win.add( mouth2nose );
// Attach the keyboard listener
KeyboardListener kl = new KeyboardListener(this, win);
// Once all components are added, set visibility to make the window show up
win.setVisible(true);
}
/**
* Event handler for KeyboardListener.
* Handle a keyboard press event (when focused on the window)
*
* @param key The key that was pressed as the code from KeyEvent.
*/
public void handleKeyboardEvent(int key) {
if (key == KeyEvent.VK_UP) {
System.out.println("DEBUG: Key = UP");
Driver.catName(+0,+1,+0,+0);
} else if (key == KeyEvent.VK_DOWN) {
System.out.println("DEBUG: Key = DOWN");
Driver.catName(+0,-1,+0,+0);
} else if (key == KeyEvent.VK_LEFT) {
System.out.println("DEBUG: Key = LEFT");
Driver.catName(-1,+0,+0,+0);
} else if (key == KeyEvent.VK_RIGHT) {
System.out.println("DEBUG: Key = RIGHT");
Driver.catName(+1,+0,+0,+0);
}
}
}
最佳答案
我建议您将JLabel catName定义为类成员并在构造函数中初始化,之后您将能够在该类中的任何方法中使用它。
public class Driver {
private JLabel catName;
public static void main( String[] args ) {
Driver gui = new Driver();
// No other Java code should be added to this method
}
// the constructor
public Driver() {
Window win = new Window();
win.setLocation(50, 30);
....
....
catName = new JLabel();
catName.setText("George, he likes buses :(");
catName.setBounds(200, 300, 150, 20);
catName.setForeground(Color.WHITE);
win.add(catName);
....
关于 java 。全局JLabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522196/
我已经创建了一个蛇梯游戏,它运行良好,但现在我想对其进行一些修改。我希望每个玩家都有两个棋子/玩家而不是一个。现在的新规则变成,同一玩家的两个棋子可以占据同一个方 block ,因此我需要一种方法将它
抱歉标题含糊不清,但我想不出我想在标题中做什么。无论如何,对于图像上的文本,我使用了 JLabel 文本并将其添加到图标中。 JLabel icon = new JLabel(new Imag
我想将我的私有(private) jlabel 对象复制到一个新的 jlabel 对象并将新的 jlabel 对象公开。想法是允许任何人访问 jlabel 的属性,但不允许进行任何将显示在原始界面上的
我有一个由二维 int 数组组成的网格。 基本包含00、01、02等,无需赘述。 我有一种提取网格位置的方法,然后使用字符串生成器操作字符串,将其更改为“jxy”(x = 网格中的 x 位置,y =
我目前正在尝试将 tabIconLabels(即 JLabels)放在更大的 JLabel(tabAreaLabel)上(所有标签都附加有 ImageIcon)。我尝试使用 OverlayLayout
我一直在尝试将一个 JLabel 放在另一个 Roguelike 之上。不幸的是,它似乎不起作用。到目前为止,这是我的代码: public void updateDraw(int direction)
我正在为 Tic Tac Toe 项目开发 GUI,其中我希望在单击相应区域时显示图像(玩家的移动)。但是,我不知道如何在另一个 JLabel(背景)之上显示 JLabel(移动)。 我已经搜索并尝试
public void movePiece(JLabel destination){ JLabel currentPiece = piece[oldIndex[0]][oldIndex[1]];
我在这方面遇到了障碍,希望能得到一些见解。 这是我想要实现的基本要点: -我有一个 piece 类(扩展 JPanel),它有一个 JRadioButton 数组,以及一个 JLabel 根据选择的按
我有一个名为 iconLabel 的类,它扩展 JLabel 来制作按钮。我使用 Font Awesome 字体将 JLabel 的文本设置为图标。我还在构造函数中向 iconLabel 对象添加了另
我对Java真的很陌生,我想制作一个简单的GUI,其中有一个按钮,当你点击它时就会计数,我遵循this YouTube 上的教程即可实现这一目标。一切都很顺利,一切都很顺利。但有一件事没有,JLabe
假设我有一个 JLabel 大小 20x20。现在假设我添加了字符串“The Quick Brown Fox Jumps Over the Lazy Dog”。这句话对于 JLabel 来说太长,因此
我有以下代码: try { File file_background = new File( "C:\\Users\\xxxx\\Desktop\\ba
我使用 netbean 创建了一个 jframe,并向该 jframe 添加了一个 jlabel (changeLbl)。之后,我向该 jframe 添加了一个 jbutton。现在我需要创建一个新的
Game(){ JFrame frame = new JFrame("Display Image"); JPanel panel = (JPanel)frame.getContentP
我在 swing 中使用带有图标的 Jlabel。我从我的计算机中选择了一个图像作为图标,但我找不到一种方法让它自动调整为 Jlabel 的大小。因为我想要的图像比 Jlabel 的大小大得多,所以我
是否可以在另一个 JLabel 之上添加一个 JLabel?谢谢。 最佳答案 简短的回答是肯定的,因为 JLabel是 Container , 所以它可以接受 Component (JLabel 是
我试图在我的 GUI 中显示卡片,我想重叠卡片以节省空间。但是,我似乎无法让它工作。我目前正在使用 MigLayout,尽管感觉它更像是一个 JLabel 问题而不是其他问题。 要从高层次来描述它,请
我正在将布局设置为 null,我有一个具有宽高比的 JFrame,并且在调整大小期间,该框架保持相同的宽高比。 每次我调整框架大小时,我都会调整所有组件的大小,没有任何问题,只是使用 JLabel 调
现在,我正在开发另一个 JPanel与 9 JLabel每个分配有不同的 Color 。应用程序要做的是,当用户单击第一个面板时,除了 JLabel 的颜色发生变化之外,不会发生任何事情。将被注册到实
我是一名优秀的程序员,十分优秀!