gpt4 book ai didi

Java 刽子手。如何创建多个按钮和 Action 监听器?

转载 作者:行者123 更新时间:2023-12-01 22:21:05 25 4
gpt4 key购买 nike

我正在尝试创建一个刽子手游戏,其中每个字母都有一个按钮。我用绝对定位和多个 Action 监听器以困难的方式做到了这一点。有什么办法可以用 for 循环同时完成这两个任务吗?

另外,如何使用多态数组实现我的刽子手?按照我现在的方式,我将为每个肢体使用 if 语句。我宁愿在他自己的面板上创建这个人,然后将每个肢体的可见性设置为 true,因为使用时无法猜测。

感谢任何帮助。

public class HangmanGui extends JFrame{
private JLabel headerLabel;
private JPanel man;
private Graphics gobj;

private JButton aButton ;
private JButton bButton ;
private JButton cButton ;
private JButton dButton ;
private JButton eButton ;
private JButton fButton ;
private JButton gButton ;
private JButton hButton ;
private JButton iButton ;
private JButton jButton ;
private JButton kButton ;
private JButton lButton ;
private JButton mButton ;
private JButton nButton ;
private JButton oButton ;
private JButton pButton ;
private JButton qButton ;
private JButton rButton ;
private JButton sButton ;
private JButton tButton ;
private JButton uButton ;
private JButton vButton ;
private JButton wButton ;
private JButton xButton ;
private JButton yButton ;
private JButton zButton ;
private JButton newWButton ;
private JButton showWButton ;
private JButton quitButton ;
private JButton startButton ;

private JLabel blankWord;

private JLabel titleWord;

private JFrame frame;
private JPanel hangman;
private FlowLayout layout;
private Container container;



/*
public static void main (String[] args){
GUITest gui = new GUITest();

gui.setSize(800,900);
gui.setVisible(true);

}
*/
//
// public GUITest()
// {



// }



public HangmanGui(){
buildGui();

}





public void buildGui(){
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.setBackground(Color.WHITE);
frame.setBounds(100, 100, 450, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Font lblFont= new Font("Serif", Font.BOLD, 30);
JLabel[] uscore = new JLabel[15];

Man man = new Man();

titleWord = new JLabel("A Game of Hangman.");
titleWord.setBounds(260,10,500,150);
titleWord.setFont(lblFont);


add(titleWord);
// add(blankWord);
//frame.add(man);
this.add(man);
man.setBounds(100,100,400,400);
JPanel panel = new JPanel();


panel.setBounds(6, 232, 400, 400);
frame.getContentPane().add(panel);



layout = new FlowLayout();
container = getContentPane();
setLayout(null);


aButton = new JButton("A");

add(aButton);
aButton.setBounds(30, 520, 50, 29);
aButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);



bButton = new JButton("B");
add(bButton);
bButton.setBounds(80, 520, 50, 29);
bButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
cButton = new JButton("C");
add(cButton);
cButton.setBounds(130, 520, 50, 29);
cButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
dButton = new JButton("D");
add(dButton);
dButton.setBounds(180, 520, 50, 29);
dButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
eButton = new JButton("E");
add(eButton);
eButton.setBounds(230, 520, 50, 29);
eButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
fButton = new JButton("F");
add(fButton);
fButton.setBounds(280, 520, 50, 29);
fButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
gButton = new JButton("G");
add(gButton);
gButton.setBounds(330, 520, 50, 29);
gButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
hButton = new JButton("H");
add(hButton);
hButton.setBounds(380, 520, 50, 29);
hButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
iButton = new JButton("I");
add(iButton);
iButton.setBounds(430, 520, 50, 29);
iButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
jButton = new JButton("J");
add(jButton);
jButton.setBounds(480, 520, 50, 29);
jButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
kButton = new JButton("K");
add(kButton);
kButton.setBounds(530, 520, 50, 29);
kButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
lButton = new JButton("L");
add(lButton);
lButton.setBounds(580, 520, 50, 29);
lButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
mButton = new JButton("M");
add(mButton);
mButton.setBounds(630, 520, 50, 29);
mButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
nButton = new JButton("N");
add(nButton);
nButton.setBounds(680, 520, 50, 29);
nButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
oButton = new JButton("O");
add(oButton);
oButton.setBounds(30, 550, 50, 29);
oButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
pButton = new JButton("P");
add(pButton);
pButton.setBounds(80, 550, 50, 29);
pButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
qButton = new JButton("Q");
add(qButton);
qButton.setBounds(130, 550, 50, 29);
qButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
rButton = new JButton("R");
add(rButton);
rButton.setBounds(180, 550, 50, 29);
rButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
sButton = new JButton("S");
add(sButton);
sButton.setBounds(230, 550, 50, 29);
sButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
tButton = new JButton("T");
add(tButton);
tButton.setBounds(280, 550, 50, 29);
tButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
uButton = new JButton("U");
add(uButton);
uButton.setBounds(330, 550, 50, 29);
uButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
vButton = new JButton("V");
add(vButton);
vButton.setBounds(380, 550, 50, 29);
vButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
wButton = new JButton("W");
add(wButton);
wButton.setBounds(430, 550, 50, 29);
wButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
xButton = new JButton("X");
add(xButton);
xButton.setBounds(480, 550, 50, 29);
xButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
yButton = new JButton("Y");
add(yButton);
yButton.setBounds(530, 550, 50, 29);
yButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
zButton = new JButton("Z");
add(zButton);
zButton.setBounds(580, 550, 50, 29);
zButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);

startButton = new JButton("Start Game");
add(startButton);
startButton.setBounds(100, 700, 120, 29);
startButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);

newWButton = new JButton("New Word");
add(newWButton);
newWButton.setBounds(250, 700, 120, 29);
newWButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);
showWButton = new JButton("Show Word");
add(showWButton);
showWButton.setBounds(400, 700, 120, 29);
showWButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);

quitButton = new JButton("Quit Game");
add(quitButton);
quitButton.setBounds(550, 700, 120, 29);
quitButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{

}
}
);



}

}

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;


public class Man extends JPanel {

protected void paintComponent(Graphics g ){
super.paintComponent(g);


//gallows
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
//head
g.setColor(Color.black);
g.drawOval(82,95,30,30);

//body
g.drawLine(97,125,97,150);
//left leg
g.drawLine(97,150,117,183);
//right leg
g.drawLine(97,150,77,183);
// right arm
g.drawLine(97,125,117,135);
//left arm
g.drawLine(97,125,77,135);




}

}

最佳答案

使用布局管理器来管理按钮的大小,不要使用setBounds()。您当前创建了一个但未设置它,因此未使用它。

layout = new FlowLayout();
setLayout(null); // Why set null and not layout

正确设置布局,然后您可以在 for 循环中使用 char ,例如

for(char c = 'A'; c <= 'Z'; c++)
{
JButton button = new JButton("" + c);
// add action listener also
add(button); // adding will add it using the layout manager
}

您可以了解更多关于layout managers here的信息.

关于Java 刽子手。如何创建多个按钮和 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com