Java GridBagLayout之后,JScrollPane滚动条不起作用
我正在为学校制作一个像Cleverbot这样的自动聊天客户端。我有两个问题。。。1) 由于某种原因,滚动条似乎不起作用。以下是一个屏幕截图:Java GridBagLayout之后,JScrollPane滚动条不起作用,java,mysql,database,jscrollpane,gridbaglayout,Java,Mysql,Database,Jscrollpane,Gridbaglayout,我正在为学校制作一个像Cleverbot这样的自动聊天客户端。我有两个问题。。。1) 由于某种原因,滚动条似乎不起作用。以下是一个屏幕截图: import javax.swing.*; 导入java.awt.*; 导入java.awt.event.KeyListener; 导入java.awt.event.KeyEvent; 导入java.lang.Math; 公共类ChatBot扩展了JFrame,实现了KeyListener{ //主要方法 公共静态void main(字符串[]args)
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.KeyListener;
导入java.awt.event.KeyEvent;
导入java.lang.Math;
公共类ChatBot扩展了JFrame,实现了KeyListener{
//主要方法
公共静态void main(字符串[]args){
新聊天机器人();
}
//回转设置
JPanel窗口=新的JPanel(){
受保护组件(图形g){
超级组件(g);
图像背景=新图像图标(“textfect.png”).getImage();
intx=(window.getWidth()-background.getWidth(null))/2;
int y=(window.getHeight()-background.getHeight(null))/2;
g、 drawImage(背景、x、y、空、此);
}
};
JLabel标签=新的JLabel(“说:”);
JTextArea dialog=新建JTextArea();
JTextField输入=新的JTextField(46);
JScrollPane scroll=新建JScrollPane(
对话
JScrollPane.VERTICAL\u滚动条\u根据需要,
JScrollPane.HORIZONTAL\u滚动条\u从不
);
//创建窗口并启动bot
公共聊天机器人(){
超级(“Pollockorator”);
设置大小(600400);
可设置大小(真);
setDefaultCloseOperation(关闭时退出);
对话框.setEditable(false);
对话框。setLineWrap(true);
对话框.setOpaque(false);
scroll.getViewport().setOpaque(false);
addKeyListener(这个);
添加(滚动);
添加(标签);
窗口。添加(输入);
//背景色;新颜色(97118131)是一种不错的颜色
窗口背景(新颜色(255、255、255));
添加(窗口);
setVisible(真);
//图形用户界面布局
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 填充=GridBagConstraints.HORIZONTAL;
//对话
c、 权重x=1.0;
c、 权重y=1.0;
c、 anchor=GridBagConstraints.PAGE\u开始;
c、 fill=GridBagConstraints.BOTH;
c、 插图=新插图(10,10,0,10);
c、 gridx=0;
c、 gridy=0;
c、 网格宽度=3;
c、 网格高度=2;
添加(对话框,c);
//输入框
c、 权重x=0;
c、 权重=0;
c、 anchor=GridBagConstraints.PAGE\u END;
c、 填充=GridBagConstraints.HORIZONTAL;
c、 插图=新插图(0,0,10,10);
c、 gridx=1;
c、 gridy=2;
c、 gridwidth=GridBagConstraints.rements;
窗口。添加(输入,c);
//标签
c、 权重x=0;
c、 权重=0;
c、 anchor=GridBagConstraints.PAGE\u END;
c、 填充=GridBagConstraints.HORIZONTAL;
c、 插图=新插图(0,10,10,0);
c、 gridx=0;
c、 gridy=2;
c、 网格宽度=1;
添加(标签,c);
input.requestFocus();
}
//知识库
字符串[][]知识库={
{“嗨”,“你好”,“你好”,“嘿”},
{“嗨”,“你好”,“嗨”},
{“你好”、“你好”、“你好”、“你好”、“你好”},
{“好”,“做得好”},
{“闭嘴”、“noob”、“别说话”}
};
//按enter键时要做什么
按下公共无效键(按键事件e){
如果(例如getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(假);
//获取用户输入
String quote=input.getText();
input.setText(“”);
如果(!quote.equals(“”){
添加文本(“您:\t”+引号);
quote.trim();
而(quote.charAt(quote.length()-1)='!'| | quote.charAt(quote.length()-1)='。| | quote.charAt(quote.length()-1)='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
字节响应=0;
int j=0;
//检查知识库中的匹配或更改主题
while(响应==0){
//如果找到匹配项,请回答
if(inArray(quote.toLowerCase(),知识库[j*2])){
响应=2;
int r=(int)Math.floor(Math.random()*知识库[(j*2)+1].length);
addText(“\nPollockorator:\t”+知识库[(j*2)+1][r]);
}
j++;
//如果未找到匹配项,请转到“更改主题”
如果(j*2==knowledgeBase.length-1&&response==0){
响应=1;
}
}
//如果bot丢失,请更改主题
如果(响应==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText(“\nPollockorator:\t”+knowledgeBase[knowledgeBase.length-1][r]);
}
添加文本(“\n”);
}
}
}
//其他活动
public void keyTyped(KeyEvent e){}
公共无效密钥已释放(密钥事件e){
如果(例如getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(真);
}
}
//格式化输入
公共void addText(字符串str){
dialog.setText(dialog.getText()+str);
}
//检查知识库是否匹配
公共布尔数组(字符串输入,字符串[]str){
布尔匹配=假;
对于(int i=0;i
)滚动条似乎不起作用
您需要将滚动窗格添加到窗口中,而不是dialaog。此外,在创建对话框时,您应该使用类似以下内容:
JTextArea dialog = new JTextArea(5, 30);
因此,可以以合理的大小创建文本区域
如果(例如getKeyCode()==KeyEvent.VK_ENTER){
不要使用KeyListener来侦听Enter键。而是将ActionListener添加到文本字段中。按下Enter键时将调用ActionListener。另外,为什么要切换文本字段的可编辑状态
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
//Main method
public static void main(String[] args){
new ChatBot();
}
//Swing settings
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Image background = new ImageIcon("textEffect.png").getImage();
int x = (window.getWidth() - background.getWidth(null)) / 2;
int y = (window.getHeight() - background.getHeight(null)) / 2;
g.drawImage(background,x,y,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(5,30);
JTextField input=new JTextField(46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//Makes window and starts bot
public ChatBot(){
super("Pollockoraptor");
setSize(600,400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addKeyListener(this);
window.add(scroll);
//background color; new Color(97, 118, 131) is a nice color
window.setBackground(new Color(255, 255, 255));
add(window);
setVisible(true);
//Gui Layout
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(scroll, c);
//Input box
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 10, 10);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
window.add(input, c);
//Label
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 10, 0);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
window.add(label, c);
input.requestFocus();
}
//knowledgeBase
String[][] knowledgeBase={
{"hi","hello","howdy","hey"},
{"hi","hello","hey"},
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"},
{"shut up","noob","stop talking"}
};
//What to do when enter is pressed
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//get the user input
String quote=input.getText();
input.setText("");
if(!quote.equals("")){
addText("You:\t"+quote);
quote.trim();
while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
//check the knowledgeBase for a match or change topic
while(response==0){
//if a match is found, reply with the answer
if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
}
j++;
//if a match is not found, go to change topic
if(j*2==knowledgeBase.length-1 && response==0){
response=1;
}
}
//change topic if bot is lost
if(response==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
}
addText("\n");
}
}
}
//other events
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
//format the input
public void addText(String str){
dialog.append(str);
}
//check the knowledgeBase for a match
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements ActionListener{
//Main method
public static void main(String[] args){
new ChatBot();
}
//Swing settings
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Image background = new ImageIcon("textEffect.png").getImage();
int x = (window.getWidth() - background.getWidth(null)) / 2;
int y = (window.getHeight() - background.getHeight(null)) / 2;
g.drawImage(background,x,y,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(5,30);
JTextField input=new JTextField(46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//Makes window and starts bot
public ChatBot(){
super("Pollockoraptor");
setSize(600,400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addActionListener(this);
window.add(scroll);
//background color; new Color(97, 118, 131) is a nice color
window.setBackground(new Color(255, 255, 255));
add(window);
setVisible(true);
//Gui Layout
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(scroll, c);
//Input box
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 10, 10);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
window.add(input, c);
//Label
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 10, 0);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
window.add(label, c);
input.requestFocus();
}
//knowledgeBase
String[][] knowledgeBase={
{"hi","hello","howdy","hey"},
{"hi","hello","hey"},
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"},
{"shut up","noob","stop talking"}
};
//What to do when enter is pressed
public void actionPerformed(ActionEvent e){
//get the user input
String quote=input.getText();
input.setText("");
if(!quote.equals("")){
addText("You:\t"+quote);
quote.trim();
while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
//check the knowledgeBase for a match or change topic
while(response==0){
//if a match is found, reply with the answer
if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
}
j++;
//if a match is not found, go to change topic
if(j*2==knowledgeBase.length-1 && response==0){
response=1;
}
}
//change topic if bot is lost
if(response==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
}
addText("\n");
}
}
//format the input
public void addText(String str){
dialog.append(str);
}
//check the knowledgeBase for a match
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
window.add(dialog, c);
JTextArea dialog = new JTextArea(5, 30);
public ChatBot() {
super("Pollockoraptor");
setSize(600, 400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addKeyListener(this);
// **** adding a bunch of junk **without** constraings??
window.add(scroll); // *** add scrollpane *with* dialog here
window.add(label);
window.add(input);
// .....
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
// Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(dialog, c); // *** then add dialog by itself*** ???? WTF???