0
我嘗試添加自定義文本作爲對j文本區域中的選擇項的響應,但參數「** .setText(」「);」不適合我的工作:(請幫助我^ _^如何將文本應用於文本區域
PS
我是新來的Java,我真的不知道是什麼蟲子,但我認爲它必須做一些事情蒙山變量「得到。」這是whitch應作爲文本區域
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);
}
};
}