让JLabel可复制?

时间:2014-03-02 18:43:17

标签: java swing jlabel

我有一个关于使JLabels可复制的问题。我有一个下拉菜单,它接受输入并将其显示为JLabel,当菜单更改时标签会发生变化。但是,我想让JLabel可复制。我听说这是不可能的,所以我将标签更改为字符串并将其作为textarea输出。但是当我这样做时,当我从下拉菜单中选择一个新选项时,字符串不会改变。我是java新手,请尽量描述,谢谢。

以下是我的一些代码。我真的只需要一种方法来使JTextArea / JLabel可以复制并让它能够改变

String[] players = {"Nearest Player", "All Players", "Random Player"};
JComboBox<String> player = new JComboBox<String>(players);
JLabel playernumb = new JLabel ("@p");
JLabel playerprompt = new JLabel("Target Player:");

JTextPane box = new JTextPane();

public static void main(String[] args) {
    mc frame = new mc();
    frame.setVisible(true);
}



player.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == player){
                JComboBox temp1 = (JComboBox)e.getSource();
                String playertxt = (String)temp1.getSelectedItem();
                switch (playertxt){
                    case "Nearest Player": playernumb.setText("@p");
                        break;
                    case "All Players": playernumb.setText("@a");
                        break;
                    case "Random Player": playernumb.setText("@r");
                        break;
                    default: break;
                }
            }
        }
    });


    add(playerprompt);
    add(player);
    box.insertComponent(playernumb);

1 个答案:

答案 0 :(得分:6)

只需使用此代码片段使其看起来像JTextPane看起来像JLabel。

JTextPane f = new JTextPane();
f.setContentType("text/html"); // let the text pane know this is what you want
f.setEditable(false); // as before
f.setBackground(null); // this is the same as a JLabel
f.setBorder(null); // remove the border

礼貌地提出这个问题:Selecting text from a JLabel?