这是一个小例子,希望对您有所帮助。基本上只需将JButton
s 添加到JPanel
,添加ActionListener
到每个JButton
,添加JPanel
到JFrame
,单击按钮后,println()
将使用 ActionCommand 执行JButton
(+1 到 @Pr0gr4mm3r 以供setActionCommand()
使用):
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ButtonsTest {
public ButtonsTest() {
initComponents();
}
private void initComponents() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(2, 2));//create gridlayput to hold buttons
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
//display action command of jbutton
String ac = ((JButton) ae.getSource()).getActionCommand();
System.out.println(ac);
//display full test in Jbutton
//String text = ((JButton) ae.getSource()).getText();
//System.out.println(text);
}
};
for (int i = 0; i < 4; i++) {
JButton b = new JButton("No: " + String.valueOf((i + 1)));
b.setActionCommand(String.valueOf((i + 1)));
b.addActionListener(al);
panel.add(b);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//set L&F and create UI on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {//set L&F
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
//create UI
new ButtonsTest();
}
});
}
}