我正在开发一个记忆游戏程序.我在JPanel上有30个JButton.当用户点击并找到匹配项(意味着具有相同图像的两个按钮)时,我想将JButton上的图像更改为其他图像.但是,在程序运行时不会发生这种情况.
我怎样才能做到这一点?
我这样做:
cards[i].setIcon(cardBack);
Run Code Online (Sandbox Code Playgroud)
cardBack是我已经拥有的ImageIcon.
我有一个类型游戏,你必须在时间限制用完之前输入尽可能快的单词,但每次键入单词时,你必须移动鼠标并单击回车并单击返回到用户输入输入下一个单词.我只是希望有没有办法使用"keyCode.VK_Enter"来发出一个由JButton调用的Action Command.
我的代码的一些片段:
Enter按钮和用户输入和输出:
enter = new JButton("Enter");
enter.setFont(serif); //serif is specified earlier
enter.setActionCommand("Enter");
enter.addActionListener(this);
container.add(enter);
userOutput = new JTextField(50);
userOutput.setFont(serif);
container.add(userOutput);
userOutput.setEditable(false);
userInput = new JTextField(43);
userInput.setFont(serif);
container.add(userInput);
userInput.setEditable(false);
Run Code Online (Sandbox Code Playgroud)
获取enter按钮的action命令的actionPerformed方法:
if(userInput.getText().equals(userOutput.getText())){
userInput.setText("");
score += 100;
Run Code Online (Sandbox Code Playgroud) 我想制作一个可以用网格面板内的键盘箭头移动的JButton.关于如何实现这一目标的任何想法?
这是我到目前为止所做的,但它并没有真正起作用
button.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
System.out.println("KEY ^$$#$#$@#$ " + key);
int position = 0;
int previousPosition = 0;
int counter = 0;
position = Integer.parseInt(button.getName());
int nextPosition = position;
if (key == KeyEvent.VK_LEFT) {
nextPosition = nextPosition - 1;
previousPosition = position;
System.out.println("PREVIOUS POSITION: " + position);
bt[1].setVisible(false);
System.out.println("POSITION: " + position);
button.setName("" + nextPosition);
bt[nextPosition].add(button);
}
else if (key == KeyEvent.VK_DOWN) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试自己学习java,并希望创建一个安全的文本编辑器,您必须登录才能访问该文本.但是,动作监听器不适用于任何按钮,我无法弄清楚出了什么问题.
请注意我只制作了两个按钮,因为第一个按钮不起作用.
我的代码如下:
public class storage extends JFrame{
private JTextField text1;
public JTextArea storearea;
public JButton newsave;
public JButton save;
public storage(UserProfile person) { //constructor with name passed in
super("Safe Storage");
setLayout(new FlowLayout());
JTextField text1 = new JTextField("Using program as: " + (person.getName()) );
text1.setEditable(false);
add(text1);
JTextArea storearea = new JTextArea(person.getText());
add(storearea);
JButton newsave = new JButton("Save");
add(newsave);
JButton save = new JButton("Save Changes");
add(save);
thehandler handler = new thehandler();
save.addActionListener(handler);
newsave.addActionListener(handler);
} //end constructor
public class thehandler implements ActionListener …Run Code Online (Sandbox Code Playgroud) 例如:
当在上JButton1单击JInternalFrame1显示时,JDesktopPane
以及在上JButton2单击JInternalFrame1关闭并JInternalFrame2显示时JDesktopPane。
在此之前
编辑:带有注释中的代码
if (JInternalFrame1 == null) {
JInternalFrame1 = new FJInternalFrame();
Desktop.add(JInternalFrame1);
JInternalFrame1.toFront();
} else {
JInternalFrame1.dispose();
}
Run Code Online (Sandbox Code Playgroud) 好的,这是我到目前为止的相关代码.
for (int i = gameLines - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
JButton jButton = new JButton();
jButton.setSize(buttonWidth, buttonHeight);
jButton.setText("" + line[i].charAt(j));
int x = ((buttonWidth / 2 * gameLines + 1) - (buttonWidth / 2 * i) + (buttonWidth * j));
int y = (gameLines - (i + 1)) * buttonHeight;
jButton.setLocation(x, y);
panel.add(jButton);
button[i][j] = jButton;
button[i][j].setActionCommand(Integer.toString(i) + "." + Integer.toString(j));
button[i][j].addActionListener(this);
}
}
Run Code Online (Sandbox Code Playgroud)
代码创建并将我的所有按钮放在我想要的位置.我花了一段时间才弄明白.我原来是AppleSoft BASIC程序员,对于i&j变量名称我很抱歉.
现在为了好玩.在底部3行按钮的右侧,我想放置一个jLabel.jLabel的右边缘与顶行中最右边按钮的右边缘对齐.每个中的文本都是右对齐的,以:以及最多4位数字结尾. …
我有这个java代码:
public static void main(String[] args) throws IOException
{
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.SOUTH);
panel.add(new Label("south"));
panel.add(new Button("Press here :)"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(loader);
frame.getContentPane().addMouseListener(loader);
//frame.getContentPane().addMouseMotionListener(loader);
frame.pack();
frame.repaint();
frame.setVisible(true);
//Deleted some unimportant content
panel.setVisible(true);
panel.add("south", panel);
t.start();
}
Run Code Online (Sandbox Code Playgroud)
所以我有这个框架有一个按钮,目前什么都不做.我一直在互联网上寻找解决方案,但我无法弄清楚如何将actionlistener添加到按钮,因为它没有名称?就像我怎么告诉使用actionlistener按下了什么按钮?除此之外,我相信我必须实现它,因此我认为在main方法中执行此操作可能是个坏主意?我只想在将它移动到另一个类或方法之前尝试一下.
好的,我希望你能提前给我一些建议或意见,谢谢!
我的代码是:
JButton btnNewButton = new JButton("ok"); //JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource().equals(btnNewButton))
{
}
}
Run Code Online (Sandbox Code Playgroud)
当我写这个仍然得到错误.如果(arg0.getSource().equals(btnNewButton))得到错误请任何一个修复它
我正在尝试绘制背景,然后将按钮放到面板上.如果没有绘制方法,按钮将正确放置在屏幕上,但是当绘制方法存在时,按钮不会显示,直到鼠标悬停在它们上面.我无法弄清楚为什么会这样.谢谢
这是在构造函数中:
setBorder(new EmptyBorder(40, 40, 40, 40));
setSize(1600, 1000);
setLayout(new GridLayout(4, 0, 40, 40));
for(int r = 0; r < rows; r++){
for(int c = 0; c < cols; c++){
levels[r][c] = new JButton(String.valueOf(levelNum));
levels[r][c].setMargin(new Insets(50, 50, 50, 50));
levels[r][c].addActionListener(e);
levels[r][c].setBackground(Color.MAGENTA);
this.add(levels[r][c]);
levelNum++;
}
}
Run Code Online (Sandbox Code Playgroud)
然后是:
@Override
public void paint(Graphics g){
g.setColor(Color.CYAN);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
... (just some basic fillRect()'s and things)
}
Run Code Online (Sandbox Code Playgroud) 是否有原因getText导致error: cannot find symbol代码中显示的动作侦听器内部?如果有,我将如何解决此错误?
class openNewPaneActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String butSrcTxt = e.getSource().getText();
}
}
Run Code Online (Sandbox Code Playgroud)