使用 NetBeans GUI Builder 时,是否有特定的方法来引入 'setText()' JTextArea 方法?
Is there a specific way for introducing a 'setText()' JTextArea method when using the NetBeans GUI Builder?
作为一个学校项目,我被要求以面向对象的方式并使用 GUI 制作一个简单的基于文本的 RPG。我最近发现 NetBeans IDE 12.1 具有一个有用的 GUI 生成器,它帮助我生成基本的 JFrame 和布局。
window 有一个启用了 JScrollPane 的 JTextArea、一个 JTextField 和两个附加的 JTextAreas。代码如下所示:
/**
* Creates new form GUI
*/
public BackupGUI() {
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() {
scrollPrompt = new javax.swing.JScrollPane();
storyPrompt = new javax.swing.JTextArea();
commandLine = new javax.swing.JTextField();
nullScrollPane = new javax.swing.JScrollPane();
enemyInfo = new javax.swing.JTextArea();
nullScrollPane1 = new javax.swing.JScrollPane();
playerInfo = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
setUndecorated(true);
scrollPrompt.setBackground(new java.awt.Color(51, 51, 51));
scrollPrompt.setBorder(null);
scrollPrompt.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPrompt.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPrompt.setViewportBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
scrollPrompt.setAutoscrolls(true);
storyPrompt.setEditable(false);
storyPrompt.setBackground(new java.awt.Color(51, 51, 51));
storyPrompt.setColumns(20);
storyPrompt.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
storyPrompt.setForeground(new java.awt.Color(153, 153, 153));
storyPrompt.setLineWrap(true);
storyPrompt.setRows(5);
storyPrompt.setWrapStyleWord(true);
storyPrompt.setBorder(null);
storyPrompt.setCaretColor(new java.awt.Color(102, 102, 102));
storyPrompt.setSelectedTextColor(new java.awt.Color(51, 51, 51));
storyPrompt.setSelectionColor(new java.awt.Color(153, 153, 153));
scrollPrompt.setViewportView(storyPrompt);
commandLine.setBackground(new java.awt.Color(72, 72, 72));
commandLine.setFont(new java.awt.Font("Yu Gothic UI Light", 0, 18)); // NOI18N
commandLine.setText(">>> ");
commandLine.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
commandLine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
commandLineActionPerformed(evt);
}
});
commandLine.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
commandLineKeyPressed(evt);
}
});
nullScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nullScrollPane.setToolTipText("");
nullScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
enemyInfo.setBackground(new java.awt.Color(102, 102, 102));
enemyInfo.setColumns(20);
enemyInfo.setLineWrap(true);
enemyInfo.setRows(5);
enemyInfo.setWrapStyleWord(true);
enemyInfo.setSelectedTextColor(new java.awt.Color(60, 60, 60));
enemyInfo.setSelectionColor(new java.awt.Color(204, 204, 204));
nullScrollPane.setViewportView(enemyInfo);
nullScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nullScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
playerInfo.setBackground(new java.awt.Color(102, 102, 102));
playerInfo.setColumns(20);
playerInfo.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
playerInfo.setLineWrap(true);
playerInfo.setRows(5);
playerInfo.setWrapStyleWord(true);
playerInfo.setSelectedTextColor(new java.awt.Color(60, 60, 60));
playerInfo.setSelectionColor(new java.awt.Color(204, 204, 204));
nullScrollPane1.setViewportView(playerInfo);
scrollPrompt.getVerticalScrollBar().setBackground(java.awt.Color.BLACK);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(commandLine, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(scrollPrompt))
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(nullScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addComponent(nullScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(scrollPrompt, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(commandLine))
.addGroup(layout.createSequentialGroup()
.addComponent(nullScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(nullScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
);
setSize(new java.awt.Dimension(960, 540));
setLocationRelativeTo(null);
}// </editor-fold>
private void commandLineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void commandLineKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_BACK_SPACE){
String s = commandLine.getText();
if (s.equals(">>> ")){
evt.consume();
}
}
if (!commandLine.getText().startsWith(">>> ")){
commandLine.setText(">>>");
}
}
/**
* @param args the command line arguments
*/
public void create(){
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BackupGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField commandLine;
private javax.swing.JTextArea enemyInfo;
private javax.swing.JScrollPane nullScrollPane;
private javax.swing.JScrollPane nullScrollPane1;
public void setPlayerText(String t){
playerInfo.setText(t);
playerInfo.requestFocusInWindow();
}
private javax.swing.JTextArea playerInfo;
private javax.swing.JScrollPane scrollPrompt;
private javax.swing.JTextArea storyPrompt;
// End of variables declaration
}
在此代码中,我尝试包含一种方法,该方法具有在程序 运行 时更改其中一个文本区域的文本的功能,但是当我构建 运行 程序时说功能似乎不起作用。
我想知道这是我的错,还是 NetBeans GUI 构建器的特性。
感谢任何帮助。
好的,您已经创建了更改 playerInfo
JTextArea 中的文本的方法,但是在您的代码中没有任何地方调用此方法来实际进行文本更改。您可以在 JTextArea 初始化后调用此方法,例如:
setPlayerText("I want this text in my PlayerInfo JTextArea!");
我怀疑您想通过 commandLineActionPerformed 方法执行此操作,该方法从 commandLine actionPerformed 事件调用,该事件在 ENTER 键被击中。在这种情况下,您可能有这样的东西:
private void commandLineActionPerformed(java.awt.event.ActionEvent evt) {
// Make sure something is provided within the Command
// Line JTextField, if not get outta here!.
if (commandLine.getText().trim().isEmpty() || commandLine.getText().trim().equals(">>>")) {
return;
}
// Place NEW (overwrite) text within the playerInfo JTextArea.
// To 'append' text you would need to do something different.
setPlayerText(commandLine.getText().substring(3));
}
作为一个学校项目,我被要求以面向对象的方式并使用 GUI 制作一个简单的基于文本的 RPG。我最近发现 NetBeans IDE 12.1 具有一个有用的 GUI 生成器,它帮助我生成基本的 JFrame 和布局。
window 有一个启用了 JScrollPane 的 JTextArea、一个 JTextField 和两个附加的 JTextAreas。代码如下所示:
/**
* Creates new form GUI
*/
public BackupGUI() {
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() {
scrollPrompt = new javax.swing.JScrollPane();
storyPrompt = new javax.swing.JTextArea();
commandLine = new javax.swing.JTextField();
nullScrollPane = new javax.swing.JScrollPane();
enemyInfo = new javax.swing.JTextArea();
nullScrollPane1 = new javax.swing.JScrollPane();
playerInfo = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
setUndecorated(true);
scrollPrompt.setBackground(new java.awt.Color(51, 51, 51));
scrollPrompt.setBorder(null);
scrollPrompt.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPrompt.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPrompt.setViewportBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
scrollPrompt.setAutoscrolls(true);
storyPrompt.setEditable(false);
storyPrompt.setBackground(new java.awt.Color(51, 51, 51));
storyPrompt.setColumns(20);
storyPrompt.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
storyPrompt.setForeground(new java.awt.Color(153, 153, 153));
storyPrompt.setLineWrap(true);
storyPrompt.setRows(5);
storyPrompt.setWrapStyleWord(true);
storyPrompt.setBorder(null);
storyPrompt.setCaretColor(new java.awt.Color(102, 102, 102));
storyPrompt.setSelectedTextColor(new java.awt.Color(51, 51, 51));
storyPrompt.setSelectionColor(new java.awt.Color(153, 153, 153));
scrollPrompt.setViewportView(storyPrompt);
commandLine.setBackground(new java.awt.Color(72, 72, 72));
commandLine.setFont(new java.awt.Font("Yu Gothic UI Light", 0, 18)); // NOI18N
commandLine.setText(">>> ");
commandLine.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
commandLine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
commandLineActionPerformed(evt);
}
});
commandLine.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
commandLineKeyPressed(evt);
}
});
nullScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nullScrollPane.setToolTipText("");
nullScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
enemyInfo.setBackground(new java.awt.Color(102, 102, 102));
enemyInfo.setColumns(20);
enemyInfo.setLineWrap(true);
enemyInfo.setRows(5);
enemyInfo.setWrapStyleWord(true);
enemyInfo.setSelectedTextColor(new java.awt.Color(60, 60, 60));
enemyInfo.setSelectionColor(new java.awt.Color(204, 204, 204));
nullScrollPane.setViewportView(enemyInfo);
nullScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nullScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
playerInfo.setBackground(new java.awt.Color(102, 102, 102));
playerInfo.setColumns(20);
playerInfo.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
playerInfo.setLineWrap(true);
playerInfo.setRows(5);
playerInfo.setWrapStyleWord(true);
playerInfo.setSelectedTextColor(new java.awt.Color(60, 60, 60));
playerInfo.setSelectionColor(new java.awt.Color(204, 204, 204));
nullScrollPane1.setViewportView(playerInfo);
scrollPrompt.getVerticalScrollBar().setBackground(java.awt.Color.BLACK);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(commandLine, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(scrollPrompt))
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(nullScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addComponent(nullScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(scrollPrompt, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(commandLine))
.addGroup(layout.createSequentialGroup()
.addComponent(nullScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(nullScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
);
setSize(new java.awt.Dimension(960, 540));
setLocationRelativeTo(null);
}// </editor-fold>
private void commandLineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void commandLineKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_BACK_SPACE){
String s = commandLine.getText();
if (s.equals(">>> ")){
evt.consume();
}
}
if (!commandLine.getText().startsWith(">>> ")){
commandLine.setText(">>>");
}
}
/**
* @param args the command line arguments
*/
public void create(){
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BackupGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BackupGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField commandLine;
private javax.swing.JTextArea enemyInfo;
private javax.swing.JScrollPane nullScrollPane;
private javax.swing.JScrollPane nullScrollPane1;
public void setPlayerText(String t){
playerInfo.setText(t);
playerInfo.requestFocusInWindow();
}
private javax.swing.JTextArea playerInfo;
private javax.swing.JScrollPane scrollPrompt;
private javax.swing.JTextArea storyPrompt;
// End of variables declaration
}
在此代码中,我尝试包含一种方法,该方法具有在程序 运行 时更改其中一个文本区域的文本的功能,但是当我构建 运行 程序时说功能似乎不起作用。
我想知道这是我的错,还是 NetBeans GUI 构建器的特性。
感谢任何帮助。
好的,您已经创建了更改 playerInfo
JTextArea 中的文本的方法,但是在您的代码中没有任何地方调用此方法来实际进行文本更改。您可以在 JTextArea 初始化后调用此方法,例如:
setPlayerText("I want this text in my PlayerInfo JTextArea!");
我怀疑您想通过 commandLineActionPerformed 方法执行此操作,该方法从 commandLine actionPerformed 事件调用,该事件在 ENTER 键被击中。在这种情况下,您可能有这样的东西:
private void commandLineActionPerformed(java.awt.event.ActionEvent evt) {
// Make sure something is provided within the Command
// Line JTextField, if not get outta here!.
if (commandLine.getText().trim().isEmpty() || commandLine.getText().trim().equals(">>>")) {
return;
}
// Place NEW (overwrite) text within the playerInfo JTextArea.
// To 'append' text you would need to do something different.
setPlayerText(commandLine.getText().substring(3));
}