我正在嘗試製作小型文本編輯器。當有人按下我的菜單中的子項時(文件 - >打開)未在標籤面板中顯示TextArea
這是擴展JFrame的主要類。在這個應用程序的應用程序啓動有儘快創建:
- 一個JMenu對象(在編輯菜單)
- 一個JMenu對象(文件菜單,我的用戶去打開文件菜單。)
- 一個JMenuBar的(含有2個JMenus)
- 一個JTabbedPane中(其不顯示應用程序啓動時)
下面是Main.java文件的代碼。這是擴展JFrame的主要文件
import java.awt.Container;
import java.awt.List;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*
*
*
*/
public Main() {
initComponents();
OpenAction action = new OpenAction("Open File Chooser",new Integer(KeyEvent.VK_0),"Open",tabbedPanel);
JMenuItem openFileChooser = new JMenuItem(action);
fileMenu.add(openFileChooser);
}
/**
* 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() {
tabbedPanel = new javax.swing.JTabbedPane();
menu = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
editMenu = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileMenu.setText("File");
menu.add(fileMenu);
editMenu.setText("Edit");
menu.add(editMenu);
setJMenuBar(menu);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tabbedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 781, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tabbedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 437, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenu editMenu;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar menu;
private javax.swing.JTabbedPane tabbedPanel;
// End of variables declaration
}
以下是OpenAction.java文件的代碼。這是一個處理事件:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
public class OpenAction extends AbstractAction {
JScrollPane text_panel;
JTextArea textarea;
JTabbedPane cont;
JComponent pan;
public OpenAction(String desc,int mnemonic,String title,JTabbedPane cont_to)
{
super(title);
cont = cont_to;
putValue(SHORT_DESCRIPTION,desc);
putValue(MNEMONIC_KEY,mnemonic);
}
@Override
public void actionPerformed(ActionEvent ae) {
JFileChooser fs = new JFileChooser();
fs.showOpenDialog(fs);
File fileChoosed = fs.getSelectedFile();
String nameOfFile = fileChoosed.getName();
addToTabbedMenu(nameOfFile);
}
private void addToTabbedMenu(String nameOfFile)
{
pan = new JPanel();
pan.setLayout(new GridLayout(20,20));
textarea = new JTextArea("Random");
text_panel = new JScrollPane(textarea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pan.add(text_panel);
cont.addTab(nameOfFile,pan);
}
}
什麼我從我的代碼期待時開放用戶按下(文件 - >打開),我會看到一個標籤中顯示有一個空的textarea的是滾動。我現在得到的只是一個帶有文件名稱(這很好)但沒有文本區域的選項卡。
我的問題是爲什麼我的textarea不出現在我的面板中。
謝謝
我已重新驗證並重新繪製並將整個代碼放在一個函數中,但textarea不顯示(我編輯了問題中的代碼) – Bula
@Bula,以及使用其他建議時發生的情況,因爲我建議你做什麼?通過引用面板是一種更混亂的方法,是不必要的。嘗試這種方法,如果它不起作用,然後張貼您的[MCVE](http://stackoverflow.com/help/mcve),說明問題。 – camickr
我很抱歉,但我不確定你的第二次消化是什麼。我用一個函數來創建面板textarea – Bula