我是java swing的新手。我用java swing编写了一个简单的文本编辑器,我在框架中添加了文本区域,菜单栏但是当我运行程序时它只显示表单,文本区域和菜单栏都看不到,对于同一个程序有时它运行良好。我无法弄清楚这是什么原因。有人可以帮帮我吗?
提前致谢!
以下是代码:-)
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextEditor implements ActionListener {
JFrame frame;
JTextArea textArea;
JScrollPane scrollPane;
JMenuBar menuBar;
public TextEditor() {
frame = new JFrame("Note Pad");
frame.setVisible(true);
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("image.gif"));
textArea = new JTextArea(500, 500);
scrollPane = new JScrollPane(textArea);
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenuItem newItem = new JMenuItem("New");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem close = new JMenuItem("Close");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
JMenuItem delete = new JMenuItem("Delete");
newItem.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
close.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(close);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(delete);
menuBar.add(fileMenu);
menuBar.add(editMenu);
frame.setJMenuBar(menuBar);
frame.add(scrollPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
TextEditor te = new TextEditor();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==menuBar.getMenu(0).getMenuComponent(0)){
JFrame frame2=new JFrame();
frame2.setSize(300,300);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
答案 0 :(得分:3)
发布SSCCE的好电话。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextEditor implements ActionListener {
JFrame frame;
JTextArea textArea;
JScrollPane scrollPane;
JMenuBar menuBar;
public TextEditor() {
frame = new JFrame("Note Pad");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("image.gif"));
textArea = new JTextArea(500, 500);
scrollPane = new JScrollPane(textArea);
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenuItem newItem = new JMenuItem("New");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem close = new JMenuItem("Close");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
JMenuItem delete = new JMenuItem("Delete");
newItem.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
close.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(close);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(delete);
menuBar.add(fileMenu);
menuBar.add(editMenu);
frame.setJMenuBar(menuBar);
frame.add(scrollPane, BorderLayout.CENTER);
// Very important!
frame.pack();
// Sheesh! You kids and your enormous screens!
//frame.setSize(1000, 1000);
frame.setSize(600, 400);
}
public static void main(String[] args) {
// Always a good idea.
Runnable r = new Runnable() {
public void run() {
TextEditor te = new TextEditor();
}
};
SwingUtilities.invokeLater(r);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==menuBar.getMenu(0).getMenuComponent(0)) {
JFrame frame2=new JFrame();
frame2.setSize(300,300);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
答案 1 :(得分:1)
在构造函数的末尾使框架可见。
如果要将组件添加到可见容器中,则必须先validate()
和repaint()
将其更改为可见。