它可能在java中更改此方法中的属性“Exit”吗?

时间:2018-02-19 20:21:24

标签: java jframe

我尝试更改选项退出中的文本更改,我想把一个标题动态与个性自己,例如可能在西班牙语放“salir”并删除文本默认“退出”我试过用过变量字符串并在

JMenuItem("Exit");
中输入属性,但它给我错误,有人说我该怎么做?

这是完整的代码:

import java.awt.*
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class BordeExample extends JFrame {

    Container frameContainer;

    JPanel panel = new JPanel();

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("Ile");

    JMenuItem fileExit = new JMenuItem("Exit");


    JMenu borderMenu = new JMenu("Border");

    String[] borderTypes = {"Bevel", "Compound", "Empty", "Etched", "Line", "Matte", "SoftBevel",
        "Titled"};

    JRadioButtonMenuItem[] borders = new JRadioButtonMenuItem[borderTypes.length];

    AbstractBorder[] border = {new BevelBorder(BevelBorder.LOWERED),
        new CompoundBorder(new LineBorder(Color.blue, 10), new LineBorder(Color.red, 5)),
        new EmptyBorder(10, 10, 10, 10), new EtchedBorder(), new LineBorder(Color.blue, 10),
        new MatteBorder(new ImageIcon("")), new SoftBevelBorder(BevelBorder.RAISED),
        new TitledBorder("Example")};

    ButtonGroup buttonGroup = new ButtonGroup();

    public BordeExample() {
        super("");
        fileMenu.add(fileExit);
        for (int i = 0; i < borderTypes.length; ++i) {
            borders[i] = new JRadioButtonMenuItem(borderTypes[i]);
            buttonGroup.add(borders[i]);
            borderMenu.add(borders[i]);
        }
        menuBar.add(fileMenu);
        menuBar.add(borderMenu);
        setJMenuBar(menuBar);

        frameContainer = getContentPane();
        frameContainer.setLayout(new BorderLayout());
        frameContainer.add("Center", panel);

        setupEventHandlers();
        setSize(700, 700);
        setVisible(true);
    }

    void setupEventHandlers() {
        addWindowListener(new WindowHandler());
        fileExit.addActionListener(new MenuItemHandler());
        for (int i = 0; i < borders.length; ++i) {
            borders[i].addItemListener(new ItemHandler());
        }
    }

    public static void main(String[] args) {
        BordeExample app = new BordeExample();
    }

    public class WindowHandler extends WindowAdapter {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }

    public class MenuItemHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (cmd.equals("Exit")) {
                System.exit(0);
            }
        }
    }

    public class ItemHandler implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getItem();
            String label = button.getText();
            for (int i = 0; i < borderTypes.length; ++i) {
                if (label.equals(borderTypes[i])) {
                    panel.setBorder(border[i]);
                    repaint();
                }
            }
        }
    }
}

我试过那样做了:

    String textExample = "salir";
    JMenu fileMenu = new JMenu(textExample);

1 个答案:

答案 0 :(得分:0)

“代码正在运行:”

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.border.AbstractBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;

public class BordeExample extends JFrame {

    // It's woring

    String exitString = "slair";
    JMenuItem fileExit = new JMenuItem(exitString);

    //
    Container frameContainer;

    JPanel panel = new JPanel();

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("Ile");

    JMenu borderMenu = new JMenu("Border");

    String[] borderTypes = { "Bevel", "Compound", "Empty", "Etched", "Line",
            "Matte", "SoftBevel", "Titled" };

    JRadioButtonMenuItem[] borders = new JRadioButtonMenuItem[borderTypes.length];

    AbstractBorder[] border = {
            new BevelBorder(BevelBorder.LOWERED),
            new CompoundBorder(new LineBorder(Color.blue, 10), new LineBorder(
                    Color.red, 5)), new EmptyBorder(10, 10, 10, 10),
            new EtchedBorder(), new LineBorder(Color.blue, 10),
            new MatteBorder(new ImageIcon("")),
            new SoftBevelBorder(BevelBorder.RAISED),
            new TitledBorder("Example") };

    ButtonGroup buttonGroup = new ButtonGroup();

    public BordeExample() {

        super("");
        fileMenu.add(fileExit);
        for (int i = 0; i < borderTypes.length; ++i) {
            borders[i] = new JRadioButtonMenuItem(borderTypes[i]);
            buttonGroup.add(borders[i]);
            borderMenu.add(borders[i]);
        }
        menuBar.add(fileMenu);
        menuBar.add(borderMenu);
        setJMenuBar(menuBar);

        frameContainer = getContentPane();
        frameContainer.setLayout(new BorderLayout());
        frameContainer.add("Center", panel);

        setupEventHandlers();
        setSize(700, 700);
        setVisible(true);
    }

    void setupEventHandlers() {
        addWindowListener(new WindowHandler());
        fileExit.addActionListener(new MenuItemHandler());
        for (int i = 0; i < borders.length; ++i) {
            borders[i].addItemListener(new ItemHandler());
        }
    }

    public class WindowHandler extends WindowAdapter {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }

    public class MenuItemHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (cmd.equals("Exit")) {
                System.exit(0);
            }
        }
    }

    public class ItemHandler implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getItem();
            String label = button.getText();
            for (int i = 0; i < borderTypes.length; ++i) {
                if (label.equals(borderTypes[i])) {
                    panel.setBorder(border[i]);
                    repaint();
                }
            }
        }
    }

    public static void main(String[] args) {
         new BordeExample();
    }

}

检查输出屏幕截图:

enter image description here