【发布时间】:2014-06-28 03:38:58
【问题描述】:
创建像this sample 这样的HTML 链接位置很容易,但现在我要讨论的是Java swing。
假设我创建了 5 个JButtons:firstButton、secondButton、thirdButton、fourthButton 和 fifthButton。
然后我把所有的文字信息放在一个JTextArea txtInform。
当我单击firstButton 时,firstButton 的信息将显示在txtInform 的顶部。
当我单击secondButton 时,secondButton 的信息将显示在txtInform 的顶部。
等下一个按钮。所有按钮都必须像this sample 一样工作。 我该怎么做?
注意: 我知道如何在 java swing 中创建组件(如 JButton、JTextArea 等)。请不要告诉我只看Swing Class API 或其他java docs 的教程。我已经阅读了 Swing Class API 教程和java docs,但我还没有找到任何针对这个问题的具体教程。如果您曾经阅读过像我在这里问的那样的特定教程,请告诉我。
编辑:
更新:我真正需要的是在单击按钮时滚动到文本区域中的某个位置。
以下是我目前的代码,我是在 netbeans 中创建的。我使用来自here 的高光。
/*
* 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 gui_001;
import java.awt.Color;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
/**
*
* @author MyScript
*/
public class sampleFrame extends javax.swing.JFrame {
/**
* Creates new form sampleFrame
*/
public sampleFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
firstButton = new javax.swing.JButton();
secondButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txtInform = new javax.swing.JTextArea();
thirdButton = new javax.swing.JButton();
fourthButton = new javax.swing.JButton();
fifthButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
firstButton.setText("First Button");
firstButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
firstButtonActionPerformed(evt);
}
});
secondButton.setText("Second Button");
secondButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
secondButtonActionPerformed(evt);
}
});
txtInform.setText("*First*");
txtInform.append("\n");
txtInform.append("All first information are here..");
txtInform.append("\n\n\n\n");
txtInform.append("**Second**");
txtInform.append("\n");
txtInform.append("All second information are here..");
txtInform.append("\n\n\n\n");
txtInform.append("***Third***");
txtInform.append("\n");
txtInform.append("All third information are here..");
txtInform.append("\n\n\n\n");
txtInform.append("****Fourth****");
txtInform.append("\n");
txtInform.append("All fourth information are here..");
txtInform.append("\n\n\n\n");
txtInform.append("*****Fifth*****");
txtInform.append("\n");
txtInform.append("All fifth information are here..");
txtInform.setColumns(20);
txtInform.setLineWrap(true);
txtInform.setRows(5);
txtInform.setWrapStyleWord(true);
jScrollPane1.setViewportView(txtInform);
thirdButton.setText("Third Button");
thirdButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
thirdButtonActionPerformed(evt);
}
});
fourthButton.setText("Fourth Button");
fourthButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fourthButtonActionPerformed(evt);
}
});
fifthButton.setText("Fifth Button");
fifthButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fifthButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(secondButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(firstButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(thirdButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fourthButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fifthButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(64, 64, 64)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(71, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(firstButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(secondButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(thirdButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(fourthButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(fifthButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(44, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(90, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(595, 477));
setLocationRelativeTo(null);
}// </editor-fold>
private void secondButtonActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInform.getText();
String second = "**Second**";
int i = text.indexOf(second);
int pos = txtInform.getCaretPosition();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE );
int offset = text.indexOf(second);
int length = second.length();
while ( offset != -1)
{
try
{
txtInform.getHighlighter().addHighlight(offset, offset + length, painter);
offset = text.indexOf(second, offset+1);
}
catch(BadLocationException ble) { System.out.println(ble); }
}
}
private void firstButtonActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInform.getText();
String second = "*First*";
int i = text.indexOf(second);
int pos = txtInform.getCaretPosition();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE );
int offset = text.indexOf(second);
int length = second.length();
while ( offset != -1)
{
try
{
txtInform.getHighlighter().addHighlight(offset, offset + length, painter);
offset = text.indexOf(second, offset+1);
}
catch(BadLocationException ble) { System.out.println(ble); }
}
}
private void fifthButtonActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInform.getText();
String second = "*****Fifth*****";
int i = text.indexOf(second);
int pos = txtInform.getCaretPosition();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE );
int offset = text.indexOf(second);
int length = second.length();
while ( offset != -1)
{
try
{
txtInform.getHighlighter().addHighlight(offset, offset + length, painter);
offset = text.indexOf(second, offset+1);
}
catch(BadLocationException ble) { System.out.println(ble); }
}
}
private void fourthButtonActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInform.getText();
String second = "****Fourth****";
int i = text.indexOf(second);
int pos = txtInform.getCaretPosition();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE );
int offset = text.indexOf(second);
int length = second.length();
while ( offset != -1)
{
try
{
txtInform.getHighlighter().addHighlight(offset, offset + length, painter);
offset = text.indexOf(second, offset+1);
}
catch(BadLocationException ble) { System.out.println(ble); }
}
}
private void thirdButtonActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInform.getText();
String second = "***Third***";
int i = text.indexOf(second);
int pos = txtInform.getCaretPosition();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE );
int offset = text.indexOf(second);
int length = second.length();
while ( offset != -1)
{
try
{
txtInform.getHighlighter().addHighlight(offset, offset + length, painter);
offset = text.indexOf(second, offset+1);
}
catch(BadLocationException ble) { System.out.println(ble); }
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(sampleFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(sampleFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(sampleFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(sampleFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new sampleFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton fifthButton;
private javax.swing.JButton firstButton;
private javax.swing.JButton fourthButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton secondButton;
private javax.swing.JButton thirdButton;
private javax.swing.JTextArea txtInform;
// End of variables declaration
}
【问题讨论】:
-
你知道
ActionListener是什么吗? -
@kdgregory,我当然愿意。我只是混淆了单击按钮时如何将 txtInform 中的文本放在 txtInform 的顶部。好的,我可以在按钮的 ActionListener 中实现任何事件。我的问题:我必须在其中实施什么事件?
-
“我必须在其中实现的事件是什么?”在
actionPerformed中,使用textArea.setText或textArea.append将信息作为参数。我建议您发布一些到目前为止您尝试过的代码。 -
好的,我会尽快发布答案,但请告诉我,您在问题的标题或正文中的哪个位置提到了“我想滚动到文本中的某个位置”之类的内容区域”?