我正在尝试制作一个小文本编辑器。当有人按下我的菜单中的子项目(文件 - >打开)
这是扩展JFrame的主类。在此应用程序中,只要应用程序启动就会创建:
以下是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);
}
}
我期待我的代码是当用户按下打开(文件 - &gt;打开)时,我会看到一个标签显示有一个可滚动的空文本区域。我现在得到的只是一个带有文件名称的标签(很好),但没有textarea。
我的问题是为什么我的textarea不会出现在我的面板中。
谢谢
答案 0 :(得分:3)
为什么有单独的方法将文本区域添加到面板?
我猜测问题是在将选项卡上的文本区域添加到选项卡之前,选项卡是可见的。所以你需要做两件事之一:
将文本区域添加到面板后,在面板上调用revalidate()和repaint()
保留用于在一个位置创建面板的代码。这是同时为面板创建面板和标签以及文本区域。这是代码设计的简单解决方案。
修改
GroupLayout导致问题。我更改了以下内容:
/*
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();
*/
add(tabbedPanel);
setSize(400, 400);
这样框架只使用了默认的BorderLayout,标签窗格将被添加到CENTER。
然后我将面板更改为使用默认的FlowLayout:
//pan.setLayout(new GridLayout(20,20));
现在显示文本区域。我建议你手动构建GUI,以便更好地理解布局管理器的工作方式。