【发布时间】:2016-05-02 19:34:07
【问题描述】:
我是 Java 新手,遇到一个问题。我创建了一个带有 Swing UI 的类,其中包含一个名为“jTextAreaConsole”的 jTextArea。:
public class converterUI extends javax.swing.JFrame {
该类导入另一个名为“dbConverter”的类:
package my.converterui;
import dbToExcel.dbConverter;
dbConverter 是一个简单的类,只有一个方法:
public class dbConverter extends common{
public static void convert(String sourceDB, String sourceQry, String destination, String objectName){
dbConverter converter = new dbConverter();
Connection con = converter.getConnection(sourceDB);
String sql = sourceQry;
ResultSet result = converter.runQuery1(con,sql);
converter.writeOut(result, objectName, destination);
closeConnection(con);
}
public static void main(String[] args) {
}
}
runQuery 和 writeOut 方法在此类扩展的 'common' 类中有详细说明。我想要做的是在公共类中引用 jTextAreaConsole 对象以将文本附加到它。我已经尝试过使用 super.jTextAreaConsole.append(str) 并且它运行但没有做任何事情。
编辑:这里的基本示例:
package myproject;
public class MyProject extends mainForm{
public static void main(String[] args) {
mainForm.main(null);
}
public void clickAction(){
passText();
}
}
package myproject;
class mainForm extends javax.swing.JFrame {
public void passText(){
jTextArea1.append("This is a test");
}
public mainForm() {
initComponents();
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("Click me");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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(40, 40, 40)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(42, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(162, 162, 162))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jButton1)
.addGap(27, 27, 27)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mainForm().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
}
谢谢,
【问题讨论】:
-
文本区域在什么类中声明?
-
看起来您将引用与继承混淆了。我建议你去the basic tutorial。如果您想解决此特定问题,请发帖 MCVE。请务必将您的代码复制粘贴到新项目,并确保在将其发布到此处之前编译并运行。
-
我认为我的问题在于导入类而不是扩展继承它。问题是我不能扩展多个类,因为目前 UI 类已经扩展了 swing 框架。
-
你的句子没有意义——你没有扩展“swing framework”。在任何情况下,多重继承都不是您的问题,只要存在可见性,IDE 就会为您完成导入。我仍然建议您完成教程并发布minimal reproducible example。
-
使用代码编辑您的问题。确保它不包含不必要的部分。
标签: java swing inheritance