我正在尝试制作小型文本编辑器。当有人按下我的菜单中的子项时(文件 - >打开)未在标签面板中显示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