java setenabled,在JPanel中调用setEnabled(false)的原因

I am working on Swing for a while now but never had a situation in practice when I had to call setEnabled(false) in JPanel.

Still, I see such code sometimes in some sophisticated gui. But I really don't undarstand why someone wants to use it?

So, please give me some examples of real life common situations when you need to use setEnabled(false) on JPanel.

Also in javadoc it says:

Disabling a component does not disable its children.

actually I had a bug because table inside disabled JPanel didn't show mouse resize cursor when resizing columns. I suspect there are other unpleasant surprises here.

解决方案

One reason is so that getEnabled() will reflect the correct state.

Consider a case where some event handler wants to flag the panel

as no longer enabled and it is not prudent at the time of the event

to iterate over and disable all child components. Other parts of the

app might need to test the state of the panel via getEnabled() to

determine what to do at different points in the app.

I personally never had to do this but now that you asked and got me

thinking I might use this sometime. Thanks. &&+=1 to the question.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java播放wav音频功能的实现代码,播放wav音频,压缩包带有测试音频,是否能播放 MP3,未知。   boolean looping = false; //是否循环播放   String[] choics = { "chimes.wav", "start.wav" }; //声音文件名数组   URL file1 = getClass().getResource(choics[0]); //声音文件1   URL file2 = getClass().getResource(choics[1]); //声音文件2   AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1   AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2   AudioClip chosenClip = sound1; //选择的声音剪辑对象   JComboBox jcbFiles = new JComboBox(choics); //文件选择组合框   JButton playButton = new JButton("播放"); //播放按钮   JButton loopButton = new JButton("循环播放"); //循环播放按钮   JButton stopButton = new JButton("停止"); //停止播放按钮   JLabel status = new JLabel("选择播放文件"); //状态栏标签   JPanel controlPanel = new JPanel(); //控制面板用于包容按钮   Container container = getContentPane(); //获得窗口内容窗格   public AudioPlayDemo() { //构造器    super("声音播放程序"); //调用父类构造器设置窗口标题栏    jcbFiles.setSelectedIndex(0); //设置组合框选择项    jcbFiles.addItemListener(this); //为播放按钮添加项目监听器    //为播放按钮、循环播放按钮、停止播放按钮添加动作监听器    playButton.addActionListener(this);    loopButton.addActionListener(this);    stopButton.addActionListener(this);    stopButton.setEnabled(false); //设置停止播放按钮不可用    //把播放按钮、循环播放按钮、停止播放按钮加入控制面板    controlPanel.add(playButton);    controlPanel.add(loopButton);    controlPanel.add(stopButton);    //把文件选择组合框、控制面板、状态栏标签加入到窗口内容窗格    container.add(jcbFiles, BorderLayout.NORTH);    container.add(controlPanel, BorderLayout.CENTER);    container.add(status, BorderLayout.SOUTH);    setSize(300, 130); //设置窗口大小    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序   }
import WordSegment.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import java.io.File; import java.util.Vector; import javax.swing.*; /** * */ /** * @author Truman * */ public class WordSegDemoFrame extends JFrame implements ActionListener { final static int ALGO_FMM = 1; final static int ALGO_BMM = 2; private JMenuBar menuBar = new JMenuBar(); private JMenuItem openDicItem, closeItem; private JRadioButtonMenuItem fmmItem, bmmItem; private JMenuItem openTrainFileItem, saveDicItem, aboutItem; private JButton btSeg; private JTextField tfInput; private JTextArea taOutput; private JPanel panel; JLabel infoDic, infoAlgo; private WordSegment seger; private DicTrainer trainer = new DicTrainer(); private void initFrame() { setTitle("Mini分词器"); setDefaultCloseOperation(EXIT_ON_CLOSE); setJMenuBar(menuBar); JMenu fileMenu = new JMenu("文件"); JMenu algorithmMenu = new JMenu("分词算法"); JMenu trainMenu = new JMenu("训练语料"); JMenu helpMenu = new JMenu("帮助"); openDicItem = fileMenu.add("载入词典"); fileMenu.addSeparator(); closeItem = fileMenu.add("退出"); algorithmMenu.add(fmmItem = new JRadioButtonMenuItem("正向最大匹配", true)); algorithmMenu.add(bmmItem = new JRadioButtonMenuItem("逆向最大匹配", false)); ButtonGroup algorithms = new ButtonGroup(); algorithms.add(fmmItem); algorithms.add(bmmItem); openTrainFileItem = trainMenu.add("载入并训练语料"); saveDicItem = trainMenu.add("保存词典"); aboutItem = helpMenu.add("关于Word Segment Demo"); menuBar.add(fileMenu); menuBar.add(algorithmMenu); menuBar.add(trainMenu); menuBar.add(helpMenu); openDicItem.addActionListener(this); closeItem.addActionListener(this); openTrainFileItem.addActionListener(this); saveDicItem.addActionListener(this); aboutItem.addActionListener(this); fmmItem.addActionListener(this); bmmItem.addActionListener(this); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout());

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值