swing组件的选择与使用 用JOptionPaneJOptionPane.showInputDialog()//提问用JOptionPane.showMessageDialog()//回答用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用JTextField就可以实现吧,输入一行后触发时间,在事件处理方法中,调用回答算法,把结果JTextField.setText()到textfield上。 要颜色不同,可以设置么。jb里面应该有选项(很久不用了记不住怎么写了)api应该在基类里找 颜色不是很好实现,这里没有实现。给我一个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); }}************************************************************************ 搂住是要做qq那种还是谈对话框的那种呢?用JOptionPaneJOptionPane.showInputDialog()//提问用JOptionPane.showMessageDialog()//回答用 这个答案是做对话框的。如果是要做qq那种,只需要JButton JTextArea就行了。 怎样能将一个http页面内容写到文件中去? 根据年月计算天数 双精度型转换的问题 JAVA+代码 请问java有isNumber函数吗? 大家快来看看啊,真奇怪! 哪位有连接池的源代码,大的小的都无所谓。刚才网上搜了一下,没找到。顺便散点分,呵呵 举例说明derived class 构造函数无法捕捉其base class构造函数所掷出的异常 各位大瞎侠请问哪有Jbuilder7中文版下载? 学习JBuild,有没有什么好的书?? 请问Properties类如何存数据呢? jdk的帮助哪里下
**********************************************************************
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);
}
}
************************************************************************
JOptionPane.showInputDialog()//提问用
JOptionPane.showMessageDialog()//回答用 这个答案是做对话框的。
如果是要做qq那种,只需要JButton JTextArea就行了。