import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JDesktopPane;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class Calendar1 extends JFrame implements ActionListener {
private JPanel contentPane;
String[] Months = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
JDesktopPane desktopPane;
ButtonGroup bG = new ButtonGroup();
JRadioButton radioButton, rdbtnNewRadioButton, radioButton_1,
radioButton_2, radioButton_3, radioButton_4, radioButton_5,
radioButton_6, radioButton_7, radioButton_8, radioButton_9,
radioButton_10, radioButton_11, radioButton_12, radioButton_13,
radioButton_14, radioButton_15, radioButton_16, radioButton_17,
radioButton_18, radioButton_19, radioButton_20, radioButton_21,
radioButton_22, radioButton_23, radioButton_24, radioButton_25,
radioButton_26, radioButton_27, radioButton_28, radioButton_29;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calendar1 frame = new Calendar1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
@SuppressWarnings("unchecked")
public Calendar1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 521, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JComboBox comboBox = new JComboBox(Months);
comboBox.setBounds(28, 16, 132, 27);
contentPane.add(comboBox);
comboBox.setSelectedIndex(0);
comboBox.addActionListener(this);
JLabel label = new JLabel("2014");
label.setBounds(350, 20, 61, 16);
contentPane.add(label);
desktopPane = new JDesktopPane();
desktopPane.setBackground(new Color(30, 144, 255));
desktopPane.setBounds(0, 63, 495, 188);
contentPane.add(desktopPane);
GridBagLayout gbl_desktopPane = new GridBagLayout();
gbl_desktopPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 };
gbl_desktopPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
gbl_desktopPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE };
gbl_desktopPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, Double.MIN_VALUE };
desktopPane.setLayout(gbl_desktopPane);
// JLabel lblSun = new JLabel("Sun");
// lblSun.setForeground(Color.RED);
//
// GridBagConstraints gbc_lblSun = new GridBagConstraints();
// gbc_lblSun.insets = new Insets(0, 0, 5, 5);
// gbc_lblSun.gridx = 2;
// gbc_lblSun.gridy = 0;
// desktopPane.add(lblSun, gbc_lblSun);
//
// JLabel lblMon = new JLabel("Mon");
// GridBagConstraints gbc_lblMon = new GridBagConstraints();
// gbc_lblMon.insets = new Insets(0, 0, 5, 5);
// gbc_lblMon.gridx = 4;
// gbc_lblMon.gridy = 0;
// desktopPane.add(lblMon, gbc_lblMon);
//
// JLabel lblTues = new JLabel("Tues");
// GridBagConstraints gbc_lblTues = new GridBagConstraints();
// gbc_lblTues.insets = new Insets(0, 0, 5, 5);
// gbc_lblTues.gridx = 6;
// gbc_lblTues.gridy = 0;
// desktopPane.add(lblTues, gbc_lblTues);
//
// JLabel lblWed = new JLabel("Wed");
// GridBagConstraints gbc_lblWed = new GridBagConstraints();
// gbc_lblWed.insets = new Insets(0, 0, 5, 5);
// gbc_lblWed.gridx = 8;
// gbc_lblWed.gridy = 0;
// desktopPane.add(lblWed, gbc_lblWed);
//
// JLabel lblThurs = new JLabel("Thurs");
// GridBagConstraints gbc_lblThurs = new GridBagConstraints();
// gbc_lblThurs.insets = new Insets(0, 0, 5, 5);
// gbc_lblThurs.gridx = 10;
// gbc_lblThurs.gridy = 0;
// desktopPane.add(lblThurs, gbc_lblThurs);
//
// JLabel lblFri = new JLabel("Friday");
// GridBagConstraints gbc_lblFri = new GridBagConstraints();
// gbc_lblFri.insets = new Insets(0, 0, 5, 5);
// gbc_lblFri.gridx = 12;
// gbc_lblFri.gridy = 0;
// desktopPane.add(lblFri, gbc_lblFri);
//
// JLabel lblSat = new JLabel("Sat");
// lblSat.setForeground(Color.RED);
// GridBagConstraints gbc_lblSat = new GridBagConstraints();
// gbc_lblSat.insets = new Insets(0, 0, 5, 5);
// gbc_lblSat.gridx = 14;
// gbc_lblSat.gridy = 0;
// desktopPane.add(lblSat, gbc_lblSat);
radioButton = new JRadioButton("1");
GridBagConstraints gbc_radioButton = new GridBagConstraints();
gbc_radioButton.insets = new Insets(0, 0, 5, 5);
gbc_radioButton.gridx = 8;
gbc_radioButton.gridy = 1;
desktopPane.add(radioButton, gbc_radioButton);
bG.add(radioButton);
rdbtnNewRadioButton = new JRadioButton("2");
GridBagConstraints gbc_rdbtnNewRadioButton = new GridBagConstraints();
gbc_rdbtnNewRadioButton.insets = new Insets(0, 0, 5, 5);
gbc_rdbtnNewRadioButton.gridx = 10;
gbc_rdbtnNewRadioButton.gridy = 1;
desktopPane.add(rdbtnNewRadioButton, gbc_rdbtnNewRadioButton);
bG.add(rdbtnNewRadioButton);
radioButton_1 = new JRadioButton("3");
GridBagConstraints gbc_radioButton_1 = new GridBagConstraints();
gbc_radioButton_1.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_1.gridx = 12;
gbc_radioButton_1.gridy = 1;
desktopPane.add(radioButton_1, gbc_radioButton_1);
bG.add(radioButton_1);
radioButton_2 = new JRadioButton("4");
GridBagConstraints gbc_radioButton_2 = new GridBagConstraints();
gbc_radioButton_2.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_2.gridx = 14;
gbc_radioButton_2.gridy = 1;
desktopPane.add(radioButton_2, gbc_radioButton_2);
bG.add(radioButton_2);
radioButton_3 = new JRadioButton("5");
GridBagConstraints gbc_radioButton_3 = new GridBagConstraints();
gbc_radioButton_3.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_3.gridx = 2;
gbc_radioButton_3.gridy = 2;
desktopPane.add(radioButton_3, gbc_radioButton_3);
bG.add(radioButton_3);
radioButton_4 = new JRadioButton("6");
GridBagConstraints gbc_radioButton_4 = new GridBagConstraints();
gbc_radioButton_4.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_4.gridx = 4;
gbc_radioButton_4.gridy = 2;
desktopPane.add(radioButton_4, gbc_radioButton_4);
bG.add(radioButton_4);
radioButton_5 = new JRadioButton("7");
GridBagConstraints gbc_radioButton_5 = new GridBagConstraints();
gbc_radioButton_5.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_5.gridx = 6;
gbc_radioButton_5.gridy = 2;
desktopPane.add(radioButton_5, gbc_radioButton_5);
bG.add(radioButton_5);
radioButton_6 = new JRadioButton("8");
GridBagConstraints gbc_radioButton_6 = new GridBagConstraints();
gbc_radioButton_6.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_6.gridx = 8;
gbc_radioButton_6.gridy = 2;
desktopPane.add(radioButton_6, gbc_radioButton_6);
bG.add(radioButton_6);
radioButton_7 = new JRadioButton("9");
GridBagConstraints gbc_radioButton_7 = new GridBagConstraints();
gbc_radioButton_7.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_7.gridx = 10;
gbc_radioButton_7.gridy = 2;
desktopPane.add(radioButton_7, gbc_radioButton_7);
bG.add(radioButton_7);
radioButton_8 = new JRadioButton("10");
GridBagConstraints gbc_radioButton_8 = new GridBagConstraints();
gbc_radioButton_8.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_8.gridx = 12;
gbc_radioButton_8.gridy = 2;
desktopPane.add(radioButton_8, gbc_radioButton_8);
bG.add(radioButton_8);
radioButton_9 = new JRadioButton("11");
GridBagConstraints gbc_radioButton_9 = new GridBagConstraints();
gbc_radioButton_9.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_9.gridx = 14;
gbc_radioButton_9.gridy = 2;
desktopPane.add(radioButton_9, gbc_radioButton_9);
bG.add(radioButton_9);
radioButton_10 = new JRadioButton("12");
GridBagConstraints gbc_radioButton_10 = new GridBagConstraints();
gbc_radioButton_10.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_10.gridx = 2;
gbc_radioButton_10.gridy = 3;
desktopPane.add(radioButton_10, gbc_radioButton_10);
bG.add(radioButton_10);
radioButton_11 = new JRadioButton("13");
GridBagConstraints gbc_radioButton_11 = new GridBagConstraints();
gbc_radioButton_11.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_11.gridx = 4;
gbc_radioButton_11.gridy = 3;
desktopPane.add(radioButton_11, gbc_radioButton_11);
bG.add(radioButton_11);
radioButton_12 = new JRadioButton("14");
GridBagConstraints gbc_radioButton_12 = new GridBagConstraints();
gbc_radioButton_12.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_12.gridx = 6;
gbc_radioButton_12.gridy = 3;
desktopPane.add(radioButton_12, gbc_radioButton_12);
bG.add(radioButton_12);
radioButton_13 = new JRadioButton("15");
GridBagConstraints gbc_radioButton_13 = new GridBagConstraints();
gbc_radioButton_13.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_13.gridx = 8;
gbc_radioButton_13.gridy = 3;
desktopPane.add(radioButton_13, gbc_radioButton_13);
bG.add(radioButton_13);
radioButton_14 = new JRadioButton("16");
GridBagConstraints gbc_radioButton_14 = new GridBagConstraints();
gbc_radioButton_14.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_14.gridx = 10;
gbc_radioButton_14.gridy = 3;
desktopPane.add(radioButton_14, gbc_radioButton_14);
bG.add(radioButton_14);
radioButton_15 = new JRadioButton("17");
GridBagConstraints gbc_radioButton_15 = new GridBagConstraints();
gbc_radioButton_15.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_15.gridx = 12;
gbc_radioButton_15.gridy = 3;
desktopPane.add(radioButton_15, gbc_radioButton_15);
bG.add(radioButton_15);
radioButton_16 = new JRadioButton("18");
GridBagConstraints gbc_radioButton_16 = new GridBagConstraints();
gbc_radioButton_16.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_16.gridx = 14;
gbc_radioButton_16.gridy = 3;
desktopPane.add(radioButton_16, gbc_radioButton_16);
bG.add(radioButton_16);
radioButton_17 = new JRadioButton("19");
GridBagConstraints gbc_radioButton_17 = new GridBagConstraints();
gbc_radioButton_17.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_17.gridx = 2;
gbc_radioButton_17.gridy = 4;
desktopPane.add(radioButton_17, gbc_radioButton_17);
bG.add(radioButton_17);
radioButton_18 = new JRadioButton("20");
GridBagConstraints gbc_radioButton_18 = new GridBagConstraints();
gbc_radioButton_18.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_18.gridx = 4;
gbc_radioButton_18.gridy = 4;
desktopPane.add(radioButton_18, gbc_radioButton_18);
bG.add(radioButton_18);
radioButton_19 = new JRadioButton("21");
GridBagConstraints gbc_radioButton_19 = new GridBagConstraints();
gbc_radioButton_19.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_19.gridx = 6;
gbc_radioButton_19.gridy = 4;
desktopPane.add(radioButton_19, gbc_radioButton_19);
bG.add(radioButton_19);
radioButton_20 = new JRadioButton("22");
GridBagConstraints gbc_radioButton_20 = new GridBagConstraints();
gbc_radioButton_20.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_20.gridx = 8;
gbc_radioButton_20.gridy = 4;
desktopPane.add(radioButton_20, gbc_radioButton_20);
bG.add(radioButton_20);
radioButton_21 = new JRadioButton("23");
GridBagConstraints gbc_radioButton_21 = new GridBagConstraints();
gbc_radioButton_21.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_21.gridx = 10;
gbc_radioButton_21.gridy = 4;
desktopPane.add(radioButton_21, gbc_radioButton_21);
bG.add(radioButton_21);
radioButton_22 = new JRadioButton("24");
GridBagConstraints gbc_radioButton_22 = new GridBagConstraints();
gbc_radioButton_22.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_22.gridx = 12;
gbc_radioButton_22.gridy = 4;
desktopPane.add(radioButton_22, gbc_radioButton_22);
bG.add(radioButton_22);
radioButton_23 = new JRadioButton("25");
GridBagConstraints gbc_radioButton_23 = new GridBagConstraints();
gbc_radioButton_23.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_23.gridx = 14;
gbc_radioButton_23.gridy = 4;
desktopPane.add(radioButton_23, gbc_radioButton_23);
bG.add(radioButton_23);
radioButton_24 = new JRadioButton("26");
GridBagConstraints gbc_radioButton_24 = new GridBagConstraints();
gbc_radioButton_24.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_24.gridx = 2;
gbc_radioButton_24.gridy = 5;
desktopPane.add(radioButton_24, gbc_radioButton_24);
bG.add(radioButton_24);
radioButton_25 = new JRadioButton("27");
GridBagConstraints gbc_radioButton_25 = new GridBagConstraints();
gbc_radioButton_25.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_25.gridx = 4;
gbc_radioButton_25.gridy = 5;
desktopPane.add(radioButton_25, gbc_radioButton_25);
bG.add(radioButton_25);
radioButton_26 = new JRadioButton("28");
GridBagConstraints gbc_radioButton_26 = new GridBagConstraints();
gbc_radioButton_26.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_26.gridx = 6;
gbc_radioButton_26.gridy = 5;
desktopPane.add(radioButton_26, gbc_radioButton_26);
bG.add(radioButton_26);
radioButton_27 = new JRadioButton("29");
GridBagConstraints gbc_radioButton_27 = new GridBagConstraints();
gbc_radioButton_27.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_27.gridx = 8;
gbc_radioButton_27.gridy = 5;
desktopPane.add(radioButton_27, gbc_radioButton_27);
bG.add(radioButton_27);
radioButton_28 = new JRadioButton("30");
GridBagConstraints gbc_radioButton_28 = new GridBagConstraints();
gbc_radioButton_28.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_28.gridx = 10;
gbc_radioButton_28.gridy = 5;
desktopPane.add(radioButton_28, gbc_radioButton_28);
bG.add(radioButton_28);
radioButton_29 = new JRadioButton("31");
GridBagConstraints gbc_radioButton_29 = new GridBagConstraints();
gbc_radioButton_29.insets = new Insets(0, 0, 5, 5);
gbc_radioButton_29.gridx = 12;
gbc_radioButton_29.gridy = 5;
desktopPane.add(radioButton_29, gbc_radioButton_29);
bG.add(radioButton_29);
}
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String months = (String) cb.getSelectedItem();
if (months.equals("February")) {
desktopPane.remove(radioButton_28);
desktopPane.revalidate();
}
}
}
我正在尝试使用组合框删除操作中的单选按钮,但是当我运行程序时,没有任何反应,也无法在执行的操作中启用新按钮。非常感谢你提前帮助我。
答案 0 :(得分:0)
只需在最重要的组件上调用validate()
,在您的情况下,这最终会成为this
。
所以而不是
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String months = (String) cb.getSelectedItem();
if (months.equals("February")) {
desktopPane.remove(radioButton_28);
desktopPane.revalidate();
}
}
使用
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String months = (String) cb.getSelectedItem();
if (months.equals("February")) {
desktopPane.remove(radioButton_28);
this.validate();
}
}
答案 1 :(得分:0)
从可见GUI中删除(或添加)组件时,常规代码为:
panel.remove(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to make sure the component is repainted.
在你的情况下:
desktopPane.remove(radioButton_28);
desktopPane.revalidate();
desktopPane.repaint(); // added