我尝试添加自定义文本以响应 j 文本区域中的选择,但参数 "**.setText("");" 不适用于我的:(请帮助我^_^
附言
我是java新手,我真的不知道什么是蠕虫,但我认为它必须对变量“get”做一些事情。这是变量 whit 将用作文本区域中的文本
public static void main(String[] args)
{
Chat frame = new Chat("ChatHelp");
frame.setVisible(true);
frame.setResizable(true);
frame.getContentPane().setBackground(Color.BLACK);
}
public Chat(String title) throws HeadlessException
{
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setSize (600, 300);
setJMenuBar(createMenuBar());
//basic
Container cont = getContentPane();
cont.setLayout(new BorderLayout());
cont.setBackground(Color.BLACK);
//eingabefelder
//greeting
JComboBox mes = new JComboBox(new String[] {"*greetings*", "hi", "wie gehts", "hey", "morgen", "abend"});
JPanel pan = new JPanel(new BorderLayout());
pan.setBackground(Color.BLACK);
cont.add(pan, BorderLayout.WEST);
pan.add(mes, BorderLayout.NORTH);
mes.addItemListener(message);
//TextField get = new TextField("**enter the message you got here**", 20);
//get.setEditable(true);
//cont.add(get, BorderLayout.NORTH);
JTextArea get = new JTextArea();
get = new JTextArea("**NO MATCH FOUND**", 20, 50);
get.setEditable(false);
cont.add(get, BorderLayout.CENTER);
}
//input listener
private ItemListener message = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent mes) {
if(mes.getStateChange() == ItemEvent.SELECTED) {
Object item = mes.getItem();
if("hi".equals(item)) {
get.setForeground(Color.RED);
} else if("wie gehts".equals(item)) {
get.setText("HI");
} else if("hey".equals(item)) {
get.setText("HI");
} else if("morgen".equals(item)) {
get.setText("HI");
} else {
get.setText("HI");
}
}
}};
//menu
private JMenuBar createMenuBar()
{
JMenuBar menu = new JMenuBar();
menu.setBackground(Color.WHITE);
JMenu file = new JMenu("File");
menu.add(file);
JMenuItem exit = new JMenuItem("exit");
file.add(exit);
exit.setAction(actionExit);
return menu;
}
private AbstractAction actionExit = new AbstractAction("exit") {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
};
}