每个Jbutton的一个Jbutton布局[关闭]

问题描述 投票:-1回答:1

有没有一种方法可以对Jbutton进行分组,以便我可以创建一个布局和一个动作,我的所有Jbutton都可以使用?

java swing jbutton
1个回答
0
投票
import java.awt.*;
import javax.swing.*;

public class JavaButtonGroupExamplePanel extends JPanel {
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JRadioButton button1 = new JRadioButton();
  JRadioButton button2 = new JRadioButton();
  JRadioButton button3 = new JRadioButton();
  ButtonGroup buttons = new ButtonGroup();

  public JavaButtonGroupExamplePanel() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jPanel1.setLayout(null);
    button1.setText("Button 1");
    button1.setBounds(new Rectangle(132, 75, 91, 23));
    button2.setText("Button 2");
    button2.setBounds(new Rectangle(132, 100, 91, 23));
    button3.setText("Button 3");
    button3.setBounds(new Rectangle(132, 124, 91, 23));
    this.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(button1, null);
    jPanel1.add(button2, null);
    jPanel1.add(button3, null);

    // this is where the radio buttons are added to the button group
    buttons.add(button1);
    buttons.add(button2);
    buttons.add(button3);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.