package bochung;import java.awt.*;import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class FrmTotalSale extends JDialog {
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable tblShow = new JTable();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JComboBox cboProdouct = new JComboBox();
JRadioButton rdoMonth = new JRadioButton();
JRadioButton rdoSeason = new JRadioButton();
JRadioButton rdoYear = new JRadioButton();
JComboBox cboMonth = new JComboBox();
JComboBox cboSeason = new JComboBox();
JComboBox cboYear = new JComboBox();
ButtonGroup btnGrp = new ButtonGroup();
public FrmTotalSale() {
try {
this.setSize(600,400);
this.setTitle("订单统计");
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setModal(true);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(4, 2, 586, 176));
jPanel2.setLayout(null);
jLabel1.setText("统计方式:");
jLabel1.setBounds(new Rectangle(24, 22, 63, 17));
jLabel2.setText("产品名称:");
jLabel2.setBounds(new Rectangle(406, 114, 63, 15));
cboProdouct.setBounds(new Rectangle(397, 63, 99, 21));
rdoMonth.setSelected(false);
rdoMonth.setText("月");
rdoMonth.setBounds(new Rectangle(94, 22, 45, 23));
rdoSeason.setSelected(false);
rdoSeason.setText("季");
rdoSeason.setBounds(new Rectangle(94, 53, 45, 23));
rdoYear.setText("年");
rdoYear.setBounds(new Rectangle(94, 85, 45, 23)); cboMonth.setBounds(new Rectangle(140, 22, 31, 23));
cboSeason.setBounds(new Rectangle(140, 53, 31, 23));
cboYear.setBounds(new Rectangle(140, 85, 65, 23));
this.getContentPane().add(jPanel2);
jPanel2.add(jLabel1);
jPanel2.add(rdoMonth);
jPanel2.add(rdoSeason);
jPanel2.add(rdoYear);
jPanel2.add(jLabel2);
jPanel2.add(cboProdouct);
jPanel2.add(cboSeason);
jPanel2.add(cboYear);
jPanel2.add(cboMonth);
this.getContentPane().add(jPanel1, null);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(tblShow);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 182, 586, 181));
jPanel1.setLayout(borderLayout1);
this.btnGrp.add(rdoMonth);
this.btnGrp.add(rdoSeason);
this.btnGrp.add(rdoYear);
this.cboSeason.addItem("-");
for(int i=1;i<=4;i++)
{
this.cboSeason.addItem(i);
}
this.cboMonth.addItem("-");
for(int i=1;i<=12;i++)
{
this.cboMonth.addItem(i);
}
this.cboYear.addItem("--");
for(int i=2000;i<=2030;i++)
{
this.cboYear.addItem(i);
}
//季的下拉框不可选
if(this.rdoMonth.isSelected())
{
cboSeason.setEnabled(false);
}
//月的下拉框不可选
else if(this.rdoSeason.isSelected())
{
cboMonth.setEnabled(false);
}
//月、季的下拉框不可选
else
{
cboMonth.setEnabled(false);
cboSeason.setEnabled(false); }
}
}
为什么这个代码执行不了这个效果呢?哪里有问题
import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class FrmTotalSale extends JDialog {
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable tblShow = new JTable();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JComboBox cboProdouct = new JComboBox();
JRadioButton rdoMonth = new JRadioButton();
JRadioButton rdoSeason = new JRadioButton();
JRadioButton rdoYear = new JRadioButton();
JComboBox cboMonth = new JComboBox();
JComboBox cboSeason = new JComboBox();
JComboBox cboYear = new JComboBox();
ButtonGroup btnGrp = new ButtonGroup();
public FrmTotalSale() {
try {
this.setSize(600,400);
this.setTitle("订单统计");
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setModal(true);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(4, 2, 586, 176));
jPanel2.setLayout(null);
jLabel1.setText("统计方式:");
jLabel1.setBounds(new Rectangle(24, 22, 63, 17));
jLabel2.setText("产品名称:");
jLabel2.setBounds(new Rectangle(406, 114, 63, 15));
cboProdouct.setBounds(new Rectangle(397, 63, 99, 21));
rdoMonth.setSelected(false);
rdoMonth.setText("月");
rdoMonth.setBounds(new Rectangle(94, 22, 45, 23));
rdoSeason.setSelected(false);
rdoSeason.setText("季");
rdoSeason.setBounds(new Rectangle(94, 53, 45, 23));
rdoYear.setText("年");
rdoYear.setBounds(new Rectangle(94, 85, 45, 23)); cboMonth.setBounds(new Rectangle(140, 22, 31, 23));
cboSeason.setBounds(new Rectangle(140, 53, 31, 23));
cboYear.setBounds(new Rectangle(140, 85, 65, 23));
this.getContentPane().add(jPanel2);
jPanel2.add(jLabel1);
jPanel2.add(rdoMonth);
jPanel2.add(rdoSeason);
jPanel2.add(rdoYear);
jPanel2.add(jLabel2);
jPanel2.add(cboProdouct);
jPanel2.add(cboSeason);
jPanel2.add(cboYear);
jPanel2.add(cboMonth);
this.getContentPane().add(jPanel1, null);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(tblShow);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 182, 586, 181));
jPanel1.setLayout(borderLayout1);
this.btnGrp.add(rdoMonth);
this.btnGrp.add(rdoSeason);
this.btnGrp.add(rdoYear);
this.cboSeason.addItem("-");
for(int i=1;i<=4;i++)
{
this.cboSeason.addItem(i);
}
this.cboMonth.addItem("-");
for(int i=1;i<=12;i++)
{
this.cboMonth.addItem(i);
}
this.cboYear.addItem("--");
for(int i=2000;i<=2030;i++)
{
this.cboYear.addItem(i);
}
//季的下拉框不可选
if(this.rdoMonth.isSelected())
{
cboSeason.setEnabled(false);
}
//月的下拉框不可选
else if(this.rdoSeason.isSelected())
{
cboMonth.setEnabled(false);
}
//月、季的下拉框不可选
else
{
cboMonth.setEnabled(false);
cboSeason.setEnabled(false); }
}
}
为什么这个代码执行不了这个效果呢?哪里有问题
else
{
cboMonth.setEnabled(false);
cboSeason.setEnabled(false); }
}
之后加一句
setVisible(true);
import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;public class FrmTotalSale extends JDialog implements ChangeListener{
JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); BorderLayout borderLayout1 = new BorderLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JTable tblShow = new JTable(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JComboBox cboProdouct = new JComboBox(); JRadioButton rdoMonth = new JRadioButton(); JRadioButton rdoSeason = new JRadioButton(); JRadioButton rdoYear = new JRadioButton(); JComboBox cboMonth = new JComboBox(); JComboBox cboSeason = new JComboBox(); JComboBox cboYear = new JComboBox(); ButtonGroup btnGrp = new ButtonGroup(); public FrmTotalSale() {
try {
this.setSize(600, 400);
this.setTitle("订单统计");
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(new FlowLayout());
this.setModal(true);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(4, 2, 586, 176));
jPanel2.setLayout(new FlowLayout());
jLabel1.setText("统计方式:");
jLabel1.setBounds(new Rectangle(24, 22, 63, 17));
jLabel2.setText("产品名称:");
jLabel2.setBounds(new Rectangle(406, 114, 63, 15));
cboProdouct.setBounds(new Rectangle(397, 63, 99, 21));
rdoMonth.setSelected(false);
rdoMonth.setText("月");
rdoMonth.setBounds(new Rectangle(94, 22, 45, 23));
rdoMonth.addChangeListener(this);
rdoSeason.setSelected(false);
rdoSeason.setText("季");
rdoSeason.setBounds(new Rectangle(94, 53, 45, 23));
rdoSeason.addChangeListener(this);
rdoYear.setText("年");
rdoYear.setBounds(new Rectangle(94, 85, 45, 23));
rdoYear.addChangeListener(this);
cboMonth.setBounds(new Rectangle(140, 22, 31, 23));
cboSeason.setBounds(new Rectangle(140, 53, 31, 23));
cboYear.setBounds(new Rectangle(140, 85, 65, 23));
this.getContentPane().add(jPanel2);
jPanel2.add(jLabel1);
jPanel2.add(rdoMonth);
jPanel2.add(rdoSeason);
jPanel2.add(rdoYear);
jPanel2.add(jLabel2);
jPanel2.add(cboProdouct);
jPanel2.add(cboSeason);
jPanel2.add(cboYear);
jPanel2.add(cboMonth);
this.getContentPane().add(jPanel1, null);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(tblShow);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 182, 586, 181));
jPanel1.setLayout(borderLayout1);
this.btnGrp.add(rdoMonth);
this.btnGrp.add(rdoSeason);
this.btnGrp.add(rdoYear);
this.cboSeason.addItem("-");
for (int i = 1; i <= 4; i++) {
this.cboSeason.addItem(i + "");
}
this.cboMonth.addItem("-");
for (int i = 1; i <= 12; i++) {
this.cboMonth.addItem(i + "");
}
this.cboYear.addItem("--");
for (int i = 2000; i <= 2030; i++) {
this.cboYear.addItem(i + "");
} setVisible(true);
} public static void main(String[] args) {
new FrmTotalSale();
} public void stateChanged(ChangeEvent arg0) {
JRadioButton btn = (JRadioButton)arg0.getSource();
cboProdouct.setEnabled(true);
cboSeason.setEnabled(true);
cboYear.setEnabled(true);
cboMonth.setEnabled(true);
if(btn.isSelected()){
// 季的下拉框不可选
if ("季".equals(btn.getText())) {
cboSeason.setEnabled(false);
}
// 月的下拉框不可选
else if ("月".equals(btn.getText())) {
cboMonth.setEnabled(false);
}
// 月、季的下拉框不可选
else {
cboMonth.setEnabled(false);
cboSeason.setEnabled(false);
}
System.out.println(btn.isSelected());
System.out.println(btn.getText()); }
}
}
在点击选择季度的时候季度选项条变为不可用/选择月的时候月变为不可用?
stateChanged作了一下小改动。
public void stateChanged(ChangeEvent arg0) { cboProdouct.setEnabled(true);
// cboSeason.setEnabled(true);
cboYear.setEnabled(true);
// cboMonth.setEnabled(true);
JRadioButton btn = (JRadioButton)arg0.getSource();
if(btn.isSelected()){
// 季的下拉框不可选
if ("月".equals(btn.getText())) {
cboSeason.setEnabled(false);
cboMonth.setEnabled(true);
}
// 月的下拉框不可选
else if ("季".equals(btn.getText())) {
cboMonth.setEnabled(false);
cboSeason.setEnabled(true);
}
// 月、季的下拉框不可选
else {
cboMonth.setEnabled(false);
cboSeason.setEnabled(false);
}
// System.out.println(btn.isSelected());
// System.out.println(btn.getText()); }
}