如果用户单击 Jbutton,我怎样才能增加分数?
How can I get a score to increase if the user clicked a Jbutton?
如果用户单击一个 Jbutton(所有按钮),我怎样才能增加分数,如果他单击框架中的随机位置,我怎么能减少分数?这是代码
package projet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Letstry {
static int score;
public static void main (String[] args){
JFrame frame = new JFrame("Scity4");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Score inc");
panel.add(button);
button.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
score = score +10;
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
}
});
}
}
好的,每次用户单击 +10
按钮时,这段代码都会增加 +10
。
要使其与 -10
一起使用,或者如果他们单击框架您想要减少的任何数字,您应该选中 JFrame mouse click using JComponent and MouseListener。
正如我所说,下次让您的代码可读时不要使用此标记 (``) 来显示代码,而是使用 4 个空格或单击上面的 { }
按钮。
不要使用 null
布局,而是使用 @BillK 的 Layout Manager. For more information about this, read this question and the answer。
希望对您有所帮助,欢迎来到 Whosebug。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Letstry {
static int score;
static JFrame frame;
static JLabel label;
static JButton button;
public static void main (String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
frame = new JFrame("Scity4");
frame.getContentPane().setLayout(new FlowLayout());
label = new JLabel("Score: ");
button = new JButton("+10");
button.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
score = score + 10;
label.setText("Score: " + score);
frame.pack();
}
});
frame.add(label);
frame.add(button);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如果用户单击一个 Jbutton(所有按钮),我怎样才能增加分数,如果他单击框架中的随机位置,我怎么能减少分数?这是代码
package projet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Letstry {
static int score;
public static void main (String[] args){
JFrame frame = new JFrame("Scity4");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Score inc");
panel.add(button);
button.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
score = score +10;
JLabel lblNewLabel = new JLabel("score : ");
lblNewLabel.setBounds(72, 131, 46, 14);
frame.add(lblNewLabel);
lblNewLabel.setText(String.valueOf(score));
}
});
}
}
好的,每次用户单击 +10
按钮时,这段代码都会增加 +10
。
要使其与 -10
一起使用,或者如果他们单击框架您想要减少的任何数字,您应该选中 JFrame mouse click using JComponent and MouseListener。
正如我所说,下次让您的代码可读时不要使用此标记 (``) 来显示代码,而是使用 4 个空格或单击上面的 { }
按钮。
不要使用 null
布局,而是使用 @BillK 的 Layout Manager. For more information about this, read this question and the answer。
希望对您有所帮助,欢迎来到 Whosebug。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Letstry {
static int score;
static JFrame frame;
static JLabel label;
static JButton button;
public static void main (String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
frame = new JFrame("Scity4");
frame.getContentPane().setLayout(new FlowLayout());
label = new JLabel("Score: ");
button = new JButton("+10");
button.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
score = score + 10;
label.setText("Score: " + score);
frame.pack();
}
});
frame.add(label);
frame.add(button);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}