我想编写有2个按钮的java swing应用程序。假设第一个按钮是“开始”按钮,第二个按钮是“停止”按钮。
[我希望每当我单击该“开始”按钮时,都应禁用开始按钮,并应启用“停止”按钮,反之亦然。
使用addActionListener
事件
jButton1.addActionListener((ActionEvent e) -> {
jButton2.setEnabled(true);
this.setEnabled(false);
});
jButton2.addActionListener((ActionEvent e) -> {
jButton1.setEnabled(true);
this.setEnabled(false);
});
Swing有那个。它们称为import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonToggler extends JDialog {
// This gets called on startup
public static void main(String ... args) {
// Ensure it happens on the UI thread
SwingUtilities.invokeLater(() -> {
JDialog dialog = new ButtonToggler();
dialog.setVisible(true);
});
}
public ButtonToggler() {
JButton firstButton = new JButton("First");
JButton secondButton = new JButton("Second");
// Make them toggle each other
firstButton.addActionListener(e -> {
firstButton.setEnabled(false);
secondButton.setEnabled(true);});
secondButton.addActionListener(e -> {
secondButton.setEnabled(false);
firstButton.setEnabled(true);
});
// Start one off as disabled
secondButton.setEnabled(false);
// Use a simple layout
getContentPane().setLayout(new FlowLayout());
// Add them to our main window
getContentPane().add(firstButton);
getContentPane().add(secondButton);
// Size to fit
pack();
// Close it all down
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
}
。在Java教程的ButtonGroups
中进行检查。
这里是使用单选按钮的一个非常小的示例。它所做的只是演示您正在描述的功能。它可以轻松缩放为无限数量的按钮。
ButtonGroups
非常感谢所有回答的人。我强迫自己使用if else函数来执行此操作,因此如果有人偶然发现了这个问题,这是我的代码:
导入javax.swing。*;
import java.awt。*;
导入java.awt.event。*;
公共类的类名扩展了JFrame实现的ActionListener
{
私有JButton b1,b2,b3;
私有容器c;
私有最终int宽度= 250;
私有最终int高度= 200;
公共类名()
{
public class ButtonGroupsDemo extends JPanel {
JFrame frame = new JFrame();
public static void main(String[] args) {
new ButtonGroupsDemo();
}
public ButtonGroupsDemo() {
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(200, 100));
ButtonGroup bg = new ButtonGroup();
ActionListener al = ae -> {
Enumeration<AbstractButton> en = bg.getElements();
while (en.hasMoreElements()) {
AbstractButton b = en.nextElement();
System.out.println(b.getActionCommand()
+ (b.isSelected() ? " is on." : " is off."));
} ;
System.out.println("---------------------");
};
for (int i = 1; i <= 4; i++) {
JRadioButton b = new JRadioButton("Button" + i);
b.addActionListener(al);
bg.add(b); // add to group
add(b); // add to panel.
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
}公共无效actionPerformed(ActionEvent e)
{
setTitle ("xx");
setSize (width, height);
setDefaultCloseOperation (EXIT_ON_CLOSE);
c = getContentPane ();
c.setLayout (null);
b1 = new JButton("button 1");
b1.setSize (200, 25);
b1.setLocation (10, 40);
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("one");
c.add(b1);
b2 = new JButton("button 2");
b2.setSize (200, 25);
b2.setLocation (10, 80);
b2.setMnemonic(KeyEvent.VK_M);
b2.setActionCommand("two");
b2.setEnabled(false);
c.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible (true);
}
public static void main(String [] s]
{
Classname c1 =新的Classname();
}
}