swing组件的选择与使用

royya 2004-11-24 09:25:37
我想作一个简单的人机交互界面,运行时只有一个框,user输入一行,计算机答一行,做好了以后
大约就是下面的样子:
U:what is your name?
C:computer
U:what are you doning?
C:computing

我该选用swing组件中的哪一个呢??
应该怎么实现那?
如果想让user和compuer的话用不同颜色显示又该怎么弄呢?

上面三个答哪个都给分!
...全文
162 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snow_oracle 2004-11-26
  • 打赏
  • 举报
回复
学习
royya 2004-11-26
  • 打赏
  • 举报
回复
楼上的各位好厉害!!小妹谢过先!!
zhaohao19853 2004-11-24
  • 打赏
  • 举报
回复
能做这样的组件不要太多哦,你认为那种好就用哪种,哪种简单用哪种
zhangzhenyi 2004-11-24
  • 打赏
  • 举报
回复
要颜色不同,可以设置么。jb里面应该有选项(很久不用了记不住怎么写了)api应该在基类里找
launch401 2004-11-24
  • 打赏
  • 举报
回复
用JTextField就可以实现吧,输入一行后触发时间,在事件处理方法中,调用回答算法,把结果JTextField.setText()到textfield上。
zhangzhenyi 2004-11-24
  • 打赏
  • 举报
回复
用JOptionPane
JOptionPane.showInputDialog()//提问用
JOptionPane.showMessageDialog()//回答用
funcreal 2004-11-24
  • 打赏
  • 举报
回复
搂住是要做qq那种还是谈对话框的那种呢?

用JOptionPane
JOptionPane.showInputDialog()//提问用
JOptionPane.showMessageDialog()//回答用 这个答案是做对话框的。
如果是要做qq那种,只需要JButton JTextArea就行了。
mengxianwei007 2004-11-24
  • 打赏
  • 举报
回复
颜色不是很好实现,这里没有实现。给我一个e-mail,有空把颜色的实现写完给你。


**********************************************************************




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

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class Test extends JFrame {
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JButton okButton = new JButton();
private JTextField textField = new JTextField();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
private BorderLayout borderLayout1 = new BorderLayout();
private JScrollPane scrollPane = new JScrollPane();
private JEditorPane edit = new JEditorPane();
private String answer1="computer";
private String answer2="computing";
private String u="U:";
private String c="C:";
private static String contains="***********************************************************"+"\n";
private JButton exitButton = new JButton();
private JLabel jLabel1 = new JLabel();
public Test() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setBorder(BorderFactory.createEtchedBorder());
panel1.setLayout(borderLayout1);
okButton.setText("发送");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
okButton_actionPerformed(e);
}
});
panel2.setLayout(gridBagLayout1);
panel2.setBorder(BorderFactory.createLoweredBevelBorder());
edit.setBackground(new Color(212, 208, 200));
edit.setEditable(false);
edit.setText(contains);
textField.requestFocus();
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(BorderFactory.createLoweredBevelBorder());
exitButton.setText("退出");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
exitButton_actionPerformed(e);
}
});
jLabel1.setText("命令输入:");
this.getContentPane().add(panel1, BorderLayout.CENTER);
this.getContentPane().add(panel2, BorderLayout.SOUTH);
panel2.add(textField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
panel2.add(okButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 10, 5, 0), 0, 0));
panel2.add(exitButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
panel2.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
panel1.add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(edit, null);

}

void okButton_actionPerformed(ActionEvent e) {

this.appendText(edit.getText(),u+textField.getText());


if(textField.getText().equalsIgnoreCase("what is your name?")){

this.appendText(edit.getText(),c+answer1);
}
if(textField.getText().equalsIgnoreCase("what are you doing?")){
this.appendText(edit.getText(),c+answer2);

}
textField.setText("");
textField.requestFocus();
}
private void appendText(String before_str,String addString){
edit.setText(before_str+"\n"+addString);


}
public static void main(String[] args){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
Test test=new Test();
test.setSize(400,300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = test.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
test.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
test.setVisible(true);
test.setTitle("测试") ;
test.show();

}

void exitButton_actionPerformed(ActionEvent e) {
System.exit(0);
}
}




************************************************************************

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧