我正在尝试向我的GUI添加滚动条,但不知道任何想法会有什么效果谢谢。我也没有在我的gui中显示我的图像。
尝试:
将我的图片放在同一个文件夹中,但它仍然无法正常工作。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chatbot;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @auth
private JLabel pic = new JLabel();
private ImageIcon icon = new ImageIcon("chatt.jpg");
//main class
chatbot_display c=new chatbot_display();
//JTextArea
private JTextArea dis=new JTextArea();
//JButton
private final JButton send=new JButton();
private final JButton clear=new JButton();
private final JButton exit=new JButton();
//JTextField
final JTextField message=new JTextField();
//Making the display bar scroll.
public chatbot_gui(){
init();
}
private void init() {
super.setSize(400,400);
super.setTitle("Nigel");
super.setLayout(null);
pic.setBounds(10, 10, 150, 300);
pic.setIcon(icon);
super.getContentPane().add(pic);
//JTextArea Settings
dis.setBounds(50,50, 300, 150);
super.getContentPane().add(dis);
dis.setEditable(false);//to prevent users from entering in the text area
super.getContentPane().setBackground(Color.red);
//JButton Setting
this.send.setText("SEND");
this.clear.setText("CLEAR");
this.exit.setText("EXIT");
send.setBounds(80,325, 75, 25);
clear.setBounds(160,325, 75, 25);
exit.setBounds(240,325, 75, 25);
super.getContentPane().add(send).setBackground(Color.yellow);
super.getContentPane().add(clear).setBackground(Color.yellow);
super.getContentPane().add(exit).setBackground(Color.yellow);
//JTextField Setting
message.setBounds(50,250, 300, 50);
super.getContentPane().add(message);
//ActionListener
clear.addActionListener(this);
exit.addActionListener(this);
send.addActionListener(this);
super.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();//stores the string on the button and passes on to str.
if(str.equalsIgnoreCase("Exit"))//if str equals Exit the JFrame.
System.exit(0);
else if(str.equalsIgnoreCase("CLEAR")){//begin
//goes to the calculation class to do the method
c.clear(message);
}//end
else if(str.equalsIgnoreCase("SEND")){
String quote=message.getText();
message.setText("");
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
// the ArrayList replaces your HashSet.
ArrayList<String> list = new ArrayList<String>(); // I called it list so you we can see the difference :)
ArrayList<String> list1 = new ArrayList<String>(); // I called it list so you we can see the difference :)
Random r = new Random();
list.add("Hello");
list.add("Hi");
list.add("How's you whats up");
list.add("Hey");
list1.add("Am gud");
list1.add("looking to head to bed");
list1.add("now from gym");
list1.add("heading home");
map.put("hi", list);
map.put("hey", list);
map.put("hello", list);
map.put("whats up", list1);
map.put("hows you", list1);
map.put("hows things", list1);
if (map.containsKey(quote)) {
ArrayList<String> tmpList = map.get(quote); // this is just make the step clear, that we now retrieve an ArrayList from the map
int randomNumber = r.nextInt(tmpList.size()); // a random number between 0 and tmpList.size()
addText("---->You:\t"+quote+"\n");
addText("---->Nigel:\t"+tmpList.get(randomNumber)+"\n");
}
System.out.println(map.containsKey(message));
}// end of metthod
}
private void addText(String str) {
dis.setText(dis.getText()+str);
}
}//end of class
}
答案 0 :(得分:2)
“将我的图片放在同一个文件夹中,但它仍然无法使用。”
不是试图通过文件系统通过文件加载图像,而是通过这样做
ImageIcon icon = new ImageIcon("chatt.jpg");
// String passed will be read as file path
通过执行此操作
从类路径中通过URL加载它ImageIcon icon = new ImageIcon(getClass().getResource("chatt.jpg"));
// will be read from class path
您的文件应该与调用它的类在同一个包中
ProjectRoot
src
chatbot
chatbot_gui.java
chatt.jpg
同样如上面的评论中所述,您需要将JTextArea
添加到JScrollPane
并将JScrollPane
添加到容器中。
JTextArea textArea = new JTextArea(10, 30);
JScrollPane scroll = new JScrollPane(textArea);
frame.add(scroll);
旁注
super
子类中的所有继承方法上调用JFrame
。