我尝试从数据库中检索数据并使用带有scrollPane的textArea进行显示。这是我的代码:
public class eForumThreadContent {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="74,36"
private JPanel jContentPane = null;
private JLabel jLoginLink = null;
private JTextField jTextField_search = null;
private JButton jButtonSearch = null;
private JLabel jLabel_sessionborder = null;
private JButton jButton_home = null;
private JLabel jLabel_searchBackgroundColor = null;
private JButton jButton_signOut = null;
private JButton jButton_postNewThread = null;
private JButton jButton_back = null;
private JLabel jLabel_replyBy = null;
private JLabel jLabel_username = null;
private JLabel jLabel_reply = null;
private JTextArea jTextArea_reply = null;
private JButton jButton_postReply = null;
private JTable jTableComment = null;
private JScrollPane jScrollPaneComment = null;
private JButton jButton_bookmark = null;
private JButton jButton_myAcc = null;
private String userName; // @jve:decl-index=0:
DBController db = new DBController(); // @jve:decl-index=0:
private int topicId;
private JLabel jLabel_topicTitle = null;
private JLabel jLabel_topicBy = null;
private JLabel jLabel_separator = null;
private JLabel jLabel_title = null;
private JScrollPane jScrollPane_areaScrollPane = null;
private JTextArea jTextArea_content = null;
private JScrollPane jScrollPane_descContent = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(946, 762));
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("eForum");
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel_title = new JLabel();
jLabel_title.setBounds(new Rectangle(75, 167, 77, 51));
jLabel_title.setFont(new Font("Dialog", Font.PLAIN, 24));
jLabel_title.setHorizontalAlignment(SwingConstants.LEFT);
jLabel_title.setText("Title : ");
jLabel_separator = new JLabel();
jLabel_separator.setBounds(new Rectangle(73, 278, 784, 8));
jLabel_separator.setBackground(new Color(102,102,102));
jLabel_separator.setOpaque(true);
jLabel_topicBy = new JLabel();
jLabel_topicBy.setBounds(new Rectangle(761, 217, 175, 41));
jLabel_topicBy.setFont(new Font("Dialog", Font.PLAIN, 18));
jLabel_topicTitle = new JLabel();
jLabel_topicTitle.setBounds(new Rectangle(161, 167, 757, 50));
jLabel_topicTitle.setFont(new Font("Dialog", Font.PLAIN, 24));
jLabel_reply = new JLabel();
jLabel_reply.setBounds(new Rectangle(106, 528, 132, 37));
jLabel_reply.setFont(new Font("Dialog", Font.PLAIN, 24));
jLabel_reply.setText("Comment");
jLabel_username = new JLabel();
jLabel_username.setBounds(new Rectangle(256, 483, 315, 32));
jLabel_username.setFont(new Font("Dialog", Font.PLAIN, 24));
jLabel_username.setText(userName);
jLabel_replyBy = new JLabel();
jLabel_replyBy.setBounds(new Rectangle(106, 481, 137, 38));
jLabel_replyBy.setFont(new Font("Dialog", Font.PLAIN, 24));
jLabel_replyBy.setText("Reply by");
jLabel_searchBackgroundColor = new JLabel();
jLabel_searchBackgroundColor
.setBounds(new Rectangle(2, 64, 930, 44));
jLabel_searchBackgroundColor
.setBackground(new Color(102, 102, 102));
jLabel_searchBackgroundColor.setOpaque(true);
jLabel_sessionborder = new JLabel();
jLabel_sessionborder.setBounds(new Rectangle(-1, 40, 930, 25));
jLabel_sessionborder.setBorder(BorderFactory.createMatteBorder(0,
0, 1, 0, Color.black));
jLabel_sessionborder.setText("");
jLoginLink = new JLabel();
jLoginLink.setBounds(new Rectangle(34, 34, 259, 25));
jLoginLink.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 0,
Color.black));
jLoginLink.setFont(new Font("Dialog", Font.PLAIN, 24));
jLoginLink.setText("Logged in as " + userName);
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(Color.white);
jContentPane.add(jLoginLink, null);
jContentPane.add(getJTextField_search(), null);
jContentPane.add(getJButtonSearch(), null);
jContentPane.add(jLabel_sessionborder, null);
jContentPane.add(getJButton_home(), null);
jContentPane.add(jLabel_searchBackgroundColor, null);
jContentPane.add(getJButton_signOut(), null);
jContentPane.add(getJButton_postNewThread(), null);
jContentPane.add(getJButton_back(), null);
jContentPane.add(jLabel_replyBy, null);
jContentPane.add(jLabel_username, null);
jContentPane.add(jLabel_reply, null);
jContentPane.add(getJTextArea_reply(), null);
jContentPane.add(getJButton_postReply(), null);
jContentPane.add(getJScrollPaneComment(), null);
jContentPane.add(getJButton_bookmark(), null);
jContentPane.add(getJButton_myAcc(), null);
jContentPane.add(jLabel_topicTitle, null);
jContentPane.add(jLabel_topicBy, null);
jContentPane.add(jLabel_separator, null);
jContentPane.add(jLabel_title, null);
jContentPane.add(getJScrollPane_areaScrollPane(), null);
jContentPane.add(getJTextArea_content(), null);
jContentPane.add(getJScrollPane_descContent(), null);
SetUpTopicDetails();
}
return jContentPane;
}
/**
* This constructor take in the userName from eForumLogin page. The username
* will be display in jLoginLink.
*
* @param userName
*/
public eForumThreadContent(String userName, int topicId) {
this.userName = userName;
this.topicId = topicId;
}
/**
* This method initializes jTextField_search
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_search() {
if (jTextField_search == null) {
jTextField_search = new JTextField(50);
jTextField_search.setDocument (new JTextFieldLimit(50));
jTextField_search.setBounds(new Rectangle(500, 75, 285, 29));
jTextField_search.setFont(new Font("Dialog", Font.PLAIN, 24));
}
return jTextField_search;
}
/**
* This method initializes jButtonSearch submitSearch is called when this
* button is on click.
*
* @return javax.swing.JButton
*/
private JButton getJButtonSearch() {
if (jButtonSearch == null) {
jButtonSearch = new JButton();
jButtonSearch.setBounds(new Rectangle(797, 73, 114, 28));
jButtonSearch.setFont(new Font("Dialog", Font.PLAIN, 24));
jButtonSearch.setText("Search");
jButtonSearch
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
submitSearch(e);
}
});
}
return jButtonSearch;
}
public void submitSearch(ActionEvent e) {
String search = jTextField_search.getText();
if (search.equals("")) {
JOptionPane.showMessageDialog(null,
"Please enter keyword to search");
} else {
eForumSearch searchResult = new eForumSearch(userName, search);
if (searchResult.searchKeyword() == true) {
getJFrame().dispose();
eForumSearchResult myWindow = new eForumSearchResult(userName,
search);
myWindow.getJFrame().setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "No results found");
}
}
}
/**
* This method initializes jButton_home
*
* @return javax.swing.JButton
*/
private JButton getJButton_home() {
if (jButton_home == null) {
jButton_home = new JButton();
jButton_home.setBounds(new Rectangle(819, 2, 94, 60));
jButton_home.setBackground(Color.white);
jButton_home.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
jButton_home.setIcon(new ImageIcon(getClass().getResource(
"/Home.png")));
jButton_home
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJFrame().dispose();
kioskHomepage myWindow = new kioskHomepage();
myWindow.getJFrame().setVisible(true);
}
});
}
return jButton_home;
}
private JButton getJButton_signOut() {
if (jButton_signOut == null) {
jButton_signOut = new JButton();
jButton_signOut.setBounds(new Rectangle(297, 35, 139, 24));
jButton_signOut.setText("Sign out");
jButton_signOut.setBorderPainted(false);
jButton_signOut.setOpaque(false);
jButton_signOut.setHorizontalAlignment(SwingConstants.LEFT);
jButton_signOut.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_signOut.setBackground(Color.lightGray);
jButton_signOut
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJFrame().dispose();
eForumSignOut myWindow = new eForumSignOut(userName);
myWindow.getJFrame().setVisible(true);
}
});
}
return jButton_signOut;
}
private JButton getJButton_postNewThread() {
if (jButton_postNewThread == null) {
jButton_postNewThread = new JButton();
jButton_postNewThread.setBounds(new Rectangle(682, 118, 230, 36));
jButton_postNewThread.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_postNewThread.setText("Post New Thread");
jButton_postNewThread
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJFrame().dispose();
eForumPostThread myWindow = new eForumPostThread(
userName);
myWindow.getJFrame().setVisible(true);
}
});
}
return jButton_postNewThread;
}
public void SetUpTopicDetails() {
db.setUp("IT Innovation Project");
String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
jLabel_topicTitle.setText(resultSet.getString("topic_title"));
jTextArea_content.setText(resultSet.getString("topic_description"));
jLabel_topicBy.setText(resultSet.getString("topic_by"));
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
private JButton getJButton_back() {
if (jButton_back == null) {
jButton_back = new JButton();
jButton_back.setBounds(new Rectangle(735, 679, 166, 36));
jButton_back.setText("Back");
jButton_back.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJFrame().dispose();
eForumMainpage myWindow = new eForumMainpage(userName);
myWindow.getJFrame().setVisible(true);
}
});
}
return jButton_back;
}
/**
* This method initializes jTextArea_reply
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea_reply() {
if (jTextArea_reply == null) {
jTextArea_reply = new JTextArea();
jTextArea_reply.setBounds(new Rectangle(256, 525, 573, 130));
jTextArea_reply.setFont(new Font("Dialog", Font.PLAIN, 24));
jTextArea_reply.setLineWrap(true);
}
return jTextArea_reply;
}
private JButton getJButton_postReply() {
if (jButton_postReply == null) {
jButton_postReply = new JButton();
jButton_postReply.setBounds(new Rectangle(567, 678, 155, 36));
jButton_postReply.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_postReply.setText("Post reply");
jButton_postReply
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
submitReply(e);
}
});
}
return jButton_postReply;
}
public void submitReply(ActionEvent e) {
String replyBy = userName;
String reply = jTextArea_reply.getText();
if (reply.equals("")) {
JOptionPane.showMessageDialog(null, "Comment cannot leave blank");
} else {
eForumTopics comment = new eForumTopics(replyBy, reply);
if (comment.createComment() == true) {
JOptionPane
.showMessageDialog(null,
"Reply submitted successfully.");
SetUpJTableComment();
jTextArea_reply.setText("");
}
}
}
private JTable getJTableComment() {
String header[] = { "Comment", "Reply By" };
if (jTableComment == null) {
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
tableModel1.setColumnIdentifiers(header);
jTableComment.getColumnModel().getColumn(0).setMinWidth(685);
jTableComment.getColumnModel().getColumn(0).setMaxWidth(750);
jTableComment.getColumnModel().getColumn(1).setMinWidth(97);
jTableComment.getColumnModel().getColumn(1).setMaxWidth(100);
return jTableComment;
}
public void SetUpJTableComment() {
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
String[] data = new String[3];
db.setUp("IT Innovation Project");
String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
tableModel1.getDataVector().removeAllElements();
try {
while (resultSet.next()) {
data[0] = resultSet.getString("reply_content");
data[1] = resultSet.getString("reply_by");
tableModel1.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
private JScrollPane getJScrollPaneComment() {
if (jScrollPaneComment == null) {
jScrollPaneComment = new JScrollPane();
jScrollPaneComment.setBounds(new Rectangle(75, 290, 785, 175));
jScrollPaneComment.setBorder(BorderFactory.createEmptyBorder());
jScrollPaneComment.setViewportView(getJTableComment());
}
SetUpJTableComment();
return jScrollPaneComment;
}
private JButton getJButton_bookmark() {
if (jButton_bookmark == null) {
jButton_bookmark = new JButton();
jButton_bookmark.setBounds(new Rectangle(487, 119, 186, 36));
jButton_bookmark.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_bookmark.setText("Bookmark");
jButton_bookmark
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
submitBookmark(e);
}
});
}
return jButton_bookmark;
}
public void submitBookmark(ActionEvent e) {
String bookmarkBy = userName;
eForumTopics bookmark = new eForumTopics(bookmarkBy);
if (bookmark.bookmarkThread() == true) {
JOptionPane.showMessageDialog(null, "Thread has been bookmarked.");
}
}
private JButton getJButton_myAcc() {
if (jButton_myAcc == null) {
jButton_myAcc = new JButton();
jButton_myAcc.setBounds(new Rectangle(427, 27, 175, 41));
jButton_myAcc.setText("My account");
jButton_myAcc.setBorderPainted(false);
jButton_myAcc.setOpaque(false);
jButton_myAcc.setHorizontalAlignment(SwingConstants.LEFT);
jButton_myAcc.setFont(new Font("Dialog", Font.PLAIN, 24));
jButton_myAcc.setBackground(Color.lightGray);
jButton_myAcc
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
getJFrame().dispose();
eForumMyAcc myWindow = new eForumMyAcc(userName);
myWindow.getJFrame().setVisible(true);
}
});
}
return jButton_myAcc;
}
private JScrollPane getJScrollPane_areaScrollPane() {
if (jScrollPane_areaScrollPane == null) {
jScrollPane_areaScrollPane = new JScrollPane(jTextArea_reply);
jScrollPane_areaScrollPane.setBounds(new Rectangle(257, 527, 598, 125));
}
return jScrollPane_areaScrollPane;
}
/**
* This method initializes jTextArea_content
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea_content() {
if (jTextArea_content == null) {
jTextArea_content = new JTextArea();
jTextArea_content.setBounds(new Rectangle(75, 224, 678, 47));
return jTextArea_content;
}
/**
* This method initializes jScrollPane_descContent
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane_descContent() {
if (jScrollPane_descContent == null) {
jScrollPane_descContent = new JScrollPane(jTextArea_content);
jScrollPane_descContent.setBounds(new Rectangle(75, 224, 660, 47));
}
return jScrollPane_descContent;
}
}
但是,滚动条会一直捕捉到textArea的底部,而不是前几行textArea。提前谢谢。
答案 0 :(得分:0)
这就是我的所有代码。没有其他的。代码的休息与此无关。
然而,当我使用你提供给我们的代码运行我的SSCCE时,它就像一个魅力:
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TestTextArea3 {
private static final String TEXT = "This \r\n" + " Is\r\n" + " A\r\n" + " Testing\r\n" + " To\r\n" + " Test\r\n"
+ " ScrollPane.";
private JTextArea jTextArea_content;
private JScrollPane jScrollPane_descContent;
private void initUI() {
JFrame frame = new JFrame(TestTextArea3.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getJTextArea_content().setText(TEXT);
frame.add(getJScrollPane_descContent());
frame.pack();
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getJTextArea_content().scrollRectToVisible(new Rectangle());
}
});
}
private JTextArea getJTextArea_content() {
if (jTextArea_content == null) {
// Changed the number of rows so activate scrolling
jTextArea_content = new JTextArea(3, 30);
jTextArea_content.setBounds(new Rectangle(75, 224, 678, 47));
}
return jTextArea_content;
}
private JScrollPane getJScrollPane_descContent() {
if (jScrollPane_descContent == null) {
jScrollPane_descContent = new JScrollPane(getJTextArea_content());
jScrollPane_descContent.setBounds(new Rectangle(75, 224, 660, 47));
}
return jScrollPane_descContent;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTextArea3().initUI();
}
});
}
}