Swing:链接切换按钮以及按钮组以及相应的菜单项 [英] Swing: link toggle buttons together with a button group, along with corresponding menu items

查看:122
本文介绍了Swing:链接切换按钮以及按钮组以及相应的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于学校项目,我需要制作一个可以绘制线条,椭圆和矩形的简单绘画应用程序。



分配指定我需要工具栏按钮<每种形状的em>和菜单项。



我想通过按钮工具栏中的JToggleButtons 和菜单项 JRadioButtonMenuItems 。此外,我想要它,以便当您选择其中一个工具栏按钮时,它取消选择其他按钮,选择适当的菜单项,并取消选择其他菜单项。选择其中一个菜单项也是如此。



我知道我可以用<$ c $对任何 AbstractButton 进行分组c> ButtonGroup ,但我不确定这是否是正确的方法,因为虽然它处理一个组按钮就好了,但我不确定它能处理两个并行组。 / p>

在没有 ButtonGroup 的情况下执行此操作意味着在每个6个事件监听器中我必须手动取消选择其他按钮,并且每对都会调用相同的代码来设置形状类型。



我还可以制作两个 ButtonGroup s,一个用于菜单,一个用于工具栏,但是我还必须复制形状类型设置代码。



在任何一种情况下,我都冒着菜单设置的风险一个设置菜单项的按钮,用于设置按钮,ad infintum。



解决此问题的最佳方法是什么?



(能够解决Netbeans GUI设计师问题的分数;它'更容易)

解决方案

操作 界面是一种有效的方法,如果你有两个或多个执行相同功能的组件,如 如何使用操作 中所述。特别是 Action 将允许您的按钮和菜单项使用相同的代码。



附录:下面的示例显示了如何 JMenu JToolBar 可以共享相同的 操作

  import java.awt.BorderLayout; 
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

/ ** @see http://stackoverflow.com/questions/4038605 * /
公共类FileMenu {

public static void main(String [] args){

EventQueue.invokeLater(new Runnable(){

public void run(){
new FileMenu()。create();
}
});
}

void create(){
文件userDir = new File(System.getProperty(user.dir));
File [] files = userDir.listFiles();

JMenu menu = new JMenu(Recent Files);
JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
JLabel label = new JLabel(,JLabel.CENTER);
for(File f:files){
if(f.isFile()&&!f.isHidden()){
RecentFile rf = new RecentFile(f,label);
menu.add(new JMenuItem(rf.getAction()));
toolBar.add(rf.getAction());
}
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);

JFrame f = new JFrame(FileMenu);
f.setJMenuBar(menuBar);
f.add(toolBar,BorderLayout.CENTER);
f.add(label,BorderLayout.SOUTH);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

类RecentFile扩展AbstractAction {

私有最终文件文件;
私人最终JLabel标签;

public RecentFile(最终文件文件,最终JLabel标签){
this.file = file;
this.label = label;
this.putValue(Action.NAME,file.getName());
this.putValue(Action.SHORT_DESCRIPTION,file.getAbsolutePath());
}

public void actionPerformed(ActionEvent e){
label.setText(file.getName());

}

public Action getAction(){
return this;
}
}


For a school project, I need to make a simple paint application that can draw lines, ovals, and rectangles.

The assignment specifies that I need toolbar buttons and menu items for each type of shape.

I would like to go a little above and beyond, by making the buttons JToggleButtons in the toolbar and the menu items JRadioButtonMenuItems. Furthermore, I want it so that when you select one of the toolbar buttons, it deselects the other ones, selects the appropriate menu item, and deselects the other menu items. Same for selecting one of the menu items.

I know I can group any AbstractButton with a ButtonGroup, but I am not sure if this is the right way to go, because though it handles one "group" of buttons just fine, I am not sure it can handle two parallel groups.

Doing it without ButtonGroup would mean in each of the 6 event listeners I would have to manually deselect the other buttons, and each pair would call the same code to set the shape type.

I could also make two ButtonGroups, one for the menu, one for the toolbar, but then I also have to duplicate shape type setting code.

In either situation, I also run the risk of the menu setting a button which sets a menu item which sets a button, ad infintum.

What is the best way to tackle this problem?

(Bonus points for being able to solve the problem with the Netbeans GUI designer; It's just easier)

解决方案

The Action interface is an effective approach "if you have two or more components that perform the same function," as discussed in How to Use Actions. In particular, an Action would allow your buttons and menu items to use the same code.

Addendum: The example below shows how a JMenu and a JToolBar can share the same Action for each of several files.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

/** @see http://stackoverflow.com/questions/4038605 */
public class FileMenu {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FileMenu().create();
            }
        });
    }

    void create() {
        File userDir = new File(System.getProperty("user.dir"));
        File[] files = userDir.listFiles();

        JMenu menu = new JMenu("Recent Files");
        JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
        JLabel label = new JLabel(" ", JLabel.CENTER);
        for (File f : files) {
            if (f.isFile() && !f.isHidden()) {
                RecentFile rf = new RecentFile(f, label);
                menu.add(new JMenuItem(rf.getAction()));
                toolBar.add(rf.getAction());
            }
        }
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);

        JFrame f = new JFrame("FileMenu");
        f.setJMenuBar(menuBar);
        f.add(toolBar, BorderLayout.CENTER);
        f.add(label, BorderLayout.SOUTH);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class RecentFile extends AbstractAction {

    private final File file;
    private final JLabel label;

    public RecentFile(final File file, final JLabel label) {
        this.file = file;
        this.label = label;
        this.putValue(Action.NAME, file.getName());
        this.putValue(Action.SHORT_DESCRIPTION, file.getAbsolutePath());
    }

    public void actionPerformed(ActionEvent e) {
        label.setText(file.getName());

    }

    public Action getAction() {
        return this;
    }
}

这篇关于Swing:链接切换按钮以及按钮组以及相应的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆